Skip to content

Commit

Permalink
Merge pull request #6 from robertvanlienden/feature/unit-tests
Browse files Browse the repository at this point in the history
Wrote first tests and also fixed bug with findAgreement method
  • Loading branch information
robertvanlienden authored Dec 30, 2024
2 parents c31793e + 3ac73f1 commit 47d92ca
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 8 deletions.
7 changes: 4 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
docs export-ignore
development export-ignore
Taskfile export-ignore
docs export-ignore
tests export-ignore
development export-ignore
Taskfile export-ignore
6 changes: 5 additions & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ jobs:
- name: Run codestyle tests
run: |
docker run --rm php-ci vendor/bin/ecs --config=development/ecs.php
docker run --rm php-ci vendor/bin/ecs --config=development/ecs.php
- name: Run unit tests
run: |
docker run --rm php-ci vendor/bin/phpunit --testdox tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
development/.phpunit.result.cache
vendor
composer.lock
20 changes: 19 additions & 1 deletion Taskfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,25 @@ function task:start { ## Start the project in development mode
dockercompose up -d
}

# =========================================================
## Docker
# =========================================================

function task:build { ## Build docker compose file
title "Build docker compose"
dockercompose build
}

function task:restart { ## Docker compose restart
title "Stop docker compose"
dockercompose restart
}

function task:stop { ## Docker compose down
title "Stop docker compose"
dockercompose stop
}

function task:shell { ## Shell into PHP container
dockercompose exec php bash
}
Expand All @@ -39,8 +53,12 @@ function task:codestyle { ## Run codestyle test
dockercompose-exec vendor/bin/ecs --config=development/ecs.php
}

function task:tests { ## Run unit tests
dockercompose-exec vendor/bin/phpunit --testdox tests
}

# =========================================================
## Internal functions
# Internal functions
# =========================================================

function dockercompose {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"symfony/framework-bundle": "^6.4 || ^7.1"
},
"require-dev": {
"symplify/easy-coding-standard": "^12.5"
"symplify/easy-coding-standard": "^12.5",
"phpunit/phpunit": "^11"
},
"license": "MIT",
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion development/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN apk add --no-cache \
WORKDIR /www

RUN set -eux; \
COMPOSER_MEMORY_LIMIT=2G composer install --prefer-dist --no-autoloader --no-scripts --no-progress; \
COMPOSER_MEMORY_LIMIT=2G composer install --prefer-dist --no-scripts --no-progress; \
composer clear-cache

CMD ["sleep", "infinity"]
9 changes: 9 additions & 0 deletions development/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnPhpunitDeprecations="true"
>
</phpunit>
2 changes: 1 addition & 1 deletion src/Service/AgreementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public function findAgreement(string $label): ?array
{
$arrayKey = array_search(strtolower($label), array_map('strtolower', array_column($this->agreements, 'label')));

return $this->agreements[$arrayKey] ?? null;
return $arrayKey !== false ? $this->agreements[$arrayKey] : null;
}
}
56 changes: 56 additions & 0 deletions tests/unit/AgreementServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace RobertvanLienden\UserAgreements\Tests\Service;

use PHPUnit\Framework\TestCase;
use RobertvanLienden\UserAgreements\Service\AgreementService;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

class AgreementServiceTest extends TestCase
{
private AgreementService $agreementService;

protected function setUp(): void
{
$containerBagMock = $this->createMock(ContainerBagInterface::class);

$agreementsData = [
'agreements' => [
['label' => 'Privacy Policy', 'content' => 'Privacy content'],
['label' => 'Terms of Service', 'content' => 'Terms content'],
],
];

$containerBagMock->method('get')
->with('user_agreements')
->willReturn($agreementsData);

$this->agreementService = new AgreementService($containerBagMock);
}

public function testGetAgreements(): void
{
$agreements = $this->agreementService->getAgreements();

$this->assertIsArray($agreements);
$this->assertCount(2, $agreements);
$this->assertArrayHasKey('label', $agreements[0]);
$this->assertArrayHasKey('content', $agreements[0]);
}

public function testFindAgreement(): void
{
$agreement = $this->agreementService->findAgreement('Privacy Policy');

$this->assertNotNull($agreement);
$this->assertEquals('Privacy Policy', $agreement['label']);
$this->assertEquals('Privacy content', $agreement['content']);
}

public function testFindAgreementNotFound(): void
{
$agreement = $this->agreementService->findAgreement('not existing');

$this->assertNull($agreement);
}
}

0 comments on commit 47d92ca

Please sign in to comment.