Skip to content

Commit 1c72854

Browse files
author
DKravtsov
committed
Updated composer dependencies, refactored tests and added a new testing strategy: "Robust Testing: Our 3-Level Approach" (video will be published on our official YouTube channel).
1 parent d8c5676 commit 1c72854

File tree

24 files changed

+861
-694
lines changed

24 files changed

+861
-694
lines changed

.idea/php.xml

Lines changed: 145 additions & 145 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"Elasticsearch"
1515
],
1616
"homepage": "https://github.com/systemsdk/docker-symfony-api",
17-
"version": "v3.7.0",
17+
"version": "v3.8.0",
1818
"license": "MIT",
1919
"authors": [
2020
{
@@ -43,7 +43,7 @@
4343
"ext-pdo_mysql": "*",
4444
"beberlei/doctrineextensions": "1.5.*",
4545
"doctrine/doctrine-bundle": "3.0.*",
46-
"doctrine/doctrine-migrations-bundle": "3.5.*",
46+
"doctrine/doctrine-migrations-bundle": "3.7.*",
4747
"doctrine/orm": "3.5.*",
4848
"friendsofphp/proxy-manager-lts": "^1.0.19",
4949
"dukecity/command-scheduler-bundle": "6.0.*",
@@ -53,9 +53,9 @@
5353
"mark-gerarts/automapper-plus-bundle": "1.5.*",
5454
"matomo/device-detector": "6.4.*",
5555
"matthiasnoback/symfony-console-form": "6.0.*",
56-
"nelmio/api-doc-bundle": "5.6.*",
56+
"nelmio/api-doc-bundle": "5.8.*",
5757
"nelmio/cors-bundle": "2.6.*",
58-
"phpdocumentor/reflection-docblock": "^5.6.3",
58+
"phpdocumentor/reflection-docblock": "^5.6.4",
5959
"ramsey/uuid-doctrine": "2.1.*",
6060
"symfony/amqp-messenger": "7.3.*",
6161
"symfony/asset": "7.3.*",
@@ -66,7 +66,7 @@
6666
"symfony/doctrine-messenger": "7.3.*",
6767
"symfony/dotenv": "7.3.*",
6868
"symfony/expression-language": "7.3.*",
69-
"symfony/flex": "^2.9.0",
69+
"symfony/flex": "^2.10.0",
7070
"symfony/form": "7.3.*",
7171
"symfony/framework-bundle": "7.3.*",
7272
"symfony/http-client": "7.3.*",
@@ -91,7 +91,7 @@
9191
"symfony/validator": "7.3.*",
9292
"symfony/web-link": "7.3.*",
9393
"symfony/yaml": "7.3.*",
94-
"twig/extra-bundle": "^2.12|^3.22",
94+
"twig/extra-bundle": "^2.12|^3.22.1",
9595
"twig/twig": "^2.12|^3.22.0"
9696
},
9797
"conflict": {

composer.lock

Lines changed: 161 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/nelmio_api_doc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ when@dev: &dev
1616
info:
1717
title: Symfony Api
1818
description: This is an symfony api environment example
19-
version: 3.7.0
19+
version: 3.8.0
2020
components:
2121
securitySchemes:
2222
Bearer:

docs/testing.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This environment uses [PHPUnit](https://phpunit.de/) and includes the following
88
* Integration tests
99
* Unit tests
1010

11+
To learn about our practical strategy for applying these tests efficiently in real-world projects, watch our video guide on YouTube: [Robust Testing](https://www.youtube.com/@systemsdk).
12+
1113
Note on naming: In Symfony's terminology, `Application` tests are the same as `Functional` tests. We follow the official Symfony naming convention as described [here](https://symfony.com/doc/current/testing.html#application-tests).
1214

1315
## 🚀 How to Run Tests
@@ -38,13 +40,13 @@ Once inside the container, you can execute phpunit directly.
3840

3941
* To run a single test class:
4042
```bash
41-
./vendor/bin/phpunit ./tests/Application/Controller/ApiKeyControllerTest.php
43+
./vendor/bin/phpunit ./tests/Application/ApiKey/Transport/Controller/Api/V1/ApiKeyControllerTest.php
4244
```
4345

4446
* To run all tests in a directory:
4547

4648
```bash
47-
./vendor/bin/phpunit ./tests/Application/Controller/
49+
./vendor/bin/phpunit ./tests/Application/ApiKey/Transport/Controller/Api/V2/
4850
```
4951

5052
* To run a specific test suite (e.g., Unit, as defined in phpunit.xml.dist):

tests/Application/ApiKey/Transport/Controller/Api/V1/ApiKeyControllerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public function testThatCreateActionForRootUserReturnsSuccessResponse(): void
119119
$responseData = JSON::decode($content, true);
120120
$this->checkBasicFieldsInResponse($responseData);
121121
self::assertEquals($requestData['description'], $responseData['description']);
122+
123+
// let's check db state
124+
$resource = static::getContainer()->get(ApiKeyResource::class);
125+
$apiKeyCreatedEntity = $resource->findOne((string)$responseData['id']);
126+
self::assertInstanceOf(ApiKey::class, $apiKeyCreatedEntity);
127+
self::assertCount(1, $apiKeyCreatedEntity->getUserGroups());
128+
$apiKeyUserGroup = $apiKeyCreatedEntity->getUserGroups()->first();
129+
self::assertInstanceOf(UserGroup::class, $apiKeyUserGroup);
130+
self::assertSame($requestData['userGroups'][0], $apiKeyUserGroup->getId());
122131
}
123132

124133
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Integration\ApiKey\Application\Service\Crypt;
6+
7+
use App\ApiKey\Application\Service\Crypt\Interfaces\OpenSslCryptApiKeyServiceInterface;
8+
use App\ApiKey\Domain\Entity\ApiKey;
9+
use App\Role\Domain\Enum\Role;
10+
use App\User\Domain\Entity\UserGroup;
11+
use App\User\Domain\Repository\Interfaces\UserGroupRepositoryInterface;
12+
use PHPUnit\Framework\Attributes\TestDox;
13+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
14+
use Throwable;
15+
16+
/**
17+
* @package App\Tests
18+
*/
19+
class OpenSslCryptApiKeyServiceTest extends KernelTestCase
20+
{
21+
private readonly OpenSslCryptApiKeyServiceInterface $service;
22+
private readonly ApiKey $apiKeyEntity;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this->service = static::getContainer()->get(OpenSslCryptApiKeyServiceInterface::class);
29+
$userGroupRepository = static::getContainer()->get(UserGroupRepositoryInterface::class);
30+
$userGroup = $userGroupRepository->findOneBy([
31+
'role' => Role::API->value,
32+
]);
33+
self::assertInstanceOf(UserGroup::class, $userGroup);
34+
$this->apiKeyEntity = new ApiKey()->setDescription('Test description')->addUserGroup($userGroup);
35+
}
36+
37+
/**
38+
* @throws Throwable
39+
*/
40+
#[TestDox('Test that encryptToken method will not encrypt token with wrong length.')]
41+
public function testThatEncryptTokenMethodWillNotEncryptTokenWithWrongLength(): void
42+
{
43+
// Add extra characters to the token to make its length invalid.
44+
$token = $this->apiKeyEntity->getToken() . 'test';
45+
$this->apiKeyEntity->setToken($token);
46+
47+
$this->service->encryptToken($this->apiKeyEntity);
48+
49+
// check that the token was not encrypted and other params remains unchanged.
50+
self::assertSame($token, $this->apiKeyEntity->getToken());
51+
self::assertNull($this->apiKeyEntity->getTokenParameters());
52+
self::assertNull($this->apiKeyEntity->getTokenHash());
53+
}
54+
55+
/**
56+
* @throws Throwable
57+
*/
58+
#[TestDox('Test that decryptToken method will not decrypt token with missing token params or already decrypted.')]
59+
public function testThatDecryptTokenMethodWillNotDecryptTokenWithMissingTokenParamsOrAlreadyDecrypted(): void
60+
{
61+
$token = $this->apiKeyEntity->getToken();
62+
63+
$this->service->decryptToken($this->apiKeyEntity);
64+
65+
// Check that the token was not decrypted.
66+
self::assertSame($token, $this->apiKeyEntity->getToken());
67+
}
68+
}

tests/Unit/DateDimension/Domain/Entity/DateDimensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
use DateTimeImmutable;
1010
use DateTimeZone;
1111
use PHPUnit\Framework\Attributes\TestDox;
12-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
12+
use PHPUnit\Framework\TestCase;
1313
use Throwable;
1414

1515
use function floor;
1616

1717
/**
1818
* @package App\Tests
1919
*/
20-
class DateDimensionTest extends KernelTestCase
20+
class DateDimensionTest extends TestCase
2121
{
2222
/**
2323
* @throws Throwable

tests/Unit/ExampleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace App\Tests\Unit;
66

7-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use PHPUnit\Framework\TestCase;
88
use Throwable;
99

1010
/**
1111
* @package App\Tests\Unit
1212
*/
13-
class ExampleTest extends KernelTestCase
13+
class ExampleTest extends TestCase
1414
{
1515
/**
1616
* A basic test example.
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace App\Tests\Integration\General\Application\Validator\Constraints;
5+
namespace App\Tests\Unit\General\Application\Validator\Constraints;
66

77
use App\General\Application\Validator\Constraints\EntityReferenceExists;
88
use App\General\Application\Validator\Constraints\EntityReferenceExistsValidator;
99
use App\General\Domain\Entity\Interfaces\EntityInterface;
10-
use App\Tests\Integration\General\Application\Validator\Constraints\Src\TestEntityReference;
10+
use App\Tests\Unit\General\Application\Validator\Constraints\Src\TestEntityReference;
1111
use App\Tool\Application\Validator\Constraints\Language;
1212
use Generator;
1313
use PHPUnit\Framework\Attributes\DataProvider;
1414
use PHPUnit\Framework\Attributes\TestDox;
15+
use PHPUnit\Framework\TestCase;
1516
use Psr\Log\LoggerInterface;
1617
use stdClass;
17-
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1818
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1919
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
2020
use Symfony\Component\Validator\Exception\UnexpectedValueException;
@@ -26,7 +26,7 @@
2626
/**
2727
* @package App\Tests
2828
*/
29-
class EntityReferenceExistsValidatorTest extends KernelTestCase
29+
class EntityReferenceExistsValidatorTest extends TestCase
3030
{
3131
/**
3232
* @throws Throwable
@@ -41,7 +41,7 @@ public function testThatValidateMethodThrowsUnexpectedTypeException(): void
4141
);
4242
$constraint = new Language();
4343

44-
(new EntityReferenceExistsValidator($loggerMock))->validate('', $constraint);
44+
new EntityReferenceExistsValidator($loggerMock)->validate('', $constraint);
4545
}
4646

4747
/**
@@ -60,7 +60,7 @@ public function testThatValidateMethodThrowsUnexpectedValueException(
6060
$constraint = new EntityReferenceExists();
6161
$constraint->entityClass = $entityClass;
6262

63-
(new EntityReferenceExistsValidator($loggerMock))->validate($value, $constraint);
63+
new EntityReferenceExistsValidator($loggerMock)->validate($value, $constraint);
6464
}
6565

6666
#[TestDox('Test that `validate` method throws an exception if value is `stdClass`.')]

0 commit comments

Comments
 (0)