From b498fae7d2d3c70c5c9501d8ed92b711e47d82bc Mon Sep 17 00:00:00 2001 From: Artem Henvald Date: Tue, 20 Feb 2024 23:03:29 +0200 Subject: [PATCH] Upgrade to Symfony 7 (#50) * Update ci.yaml * Update composer.json * Update composer.json * Update ConstraintViolationListNormalizer.php * Update DictionaryEnumNormalizer.php * Update JsonSchemaErrorNormalizer.php * Update JsonSchemaErrorNormalizer.php * Update DictionaryEnumNormalizer.php * Update ConstraintViolationListNormalizer.php * Update DictionaryEnumNormalizer.php * Fix ConstraintViolationListNormalizer: Use NormalizerInterface instead of ConstraintViolationListNormalizer final class; Fix tests (#51) --------- Co-authored-by: Yevhen Sidelnyk <41589422+rela589n@users.noreply.github.com> --- .github/workflows/ci.yaml | 15 ++--- .gitignore | 2 + .../ConstraintViolationListNormalizer.php | 18 ++++-- .../Normalizer/DictionaryEnumNormalizer.php | 17 ++++-- .../Normalizer/JsonSchemaErrorNormalizer.php | 12 +++- .../ApiExceptionFormatterListenerTest.php | 5 +- .../ConstraintViolationListNormalizerTest.php | 18 +++--- Tests/Validator/JsonSchemaValidatorTest.php | 2 +- composer.json | 58 +++++++++---------- 9 files changed, 88 insertions(+), 59 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0ab51c6..11f50cd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -2,6 +2,7 @@ name: CI on: pull_request: + workflow_dispatch: ~ push: branches: - 'main' @@ -12,7 +13,7 @@ jobs: name: "Composer Validate" steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Validate composer.json' run: composer validate --no-check-all --strict --no-check-lock @@ -23,7 +24,7 @@ jobs: name: "Composer Install" steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Setup PHP' uses: shivammathur/setup-php@v2 @@ -44,7 +45,7 @@ jobs: fail-fast: false steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Setup PHP' uses: shivammathur/setup-php@v2 @@ -69,7 +70,7 @@ jobs: fail-fast: false steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Setup PHP' uses: shivammathur/setup-php@v2 @@ -96,10 +97,10 @@ jobs: php-version: - '8.2' symfony-version: - - '6.2' + - '7.0' steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Setup PHP' uses: shivammathur/setup-php@v2 @@ -124,7 +125,7 @@ jobs: fail-fast: false steps: - name: 'Checkout Code' - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: 'Setup PHP' uses: shivammathur/setup-php@v2 diff --git a/.gitignore b/.gitignore index 57872d0..3ffdd8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /vendor/ +.phpunit.result.cache +composer.lock diff --git a/Serializer/Normalizer/ConstraintViolationListNormalizer.php b/Serializer/Normalizer/ConstraintViolationListNormalizer.php index 675ca1a..f67583a 100644 --- a/Serializer/Normalizer/ConstraintViolationListNormalizer.php +++ b/Serializer/Normalizer/ConstraintViolationListNormalizer.php @@ -12,8 +12,6 @@ namespace StfalconStudio\ApiBundle\Serializer\Normalizer; -use Symfony\Component\Serializer\Debug\TraceableNormalizer; -use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer as SymfonyConstraintViolationListNormalizer; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -23,20 +21,30 @@ class ConstraintViolationListNormalizer implements NormalizerInterface { /** - * @param SymfonyConstraintViolationListNormalizer|TraceableNormalizer $symfonyConstraintViolationListNormalizer + * @param NormalizerInterface $symfonyConstraintViolationListNormalizer */ - public function __construct(private readonly SymfonyConstraintViolationListNormalizer|TraceableNormalizer $symfonyConstraintViolationListNormalizer) + public function __construct(private readonly NormalizerInterface $symfonyConstraintViolationListNormalizer) { } /** * {@inheritdoc} */ - public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof ConstraintViolationListInterface; } + /** + * {@inheritdoc} + */ + public function getSupportedTypes(?string $format): array + { + return [ + ConstraintViolationListInterface::class => true, + ]; + } + /** * Clear the "detail" field from prefixed property paths. * diff --git a/Serializer/Normalizer/DictionaryEnumNormalizer.php b/Serializer/Normalizer/DictionaryEnumNormalizer.php index a85f648..25be90c 100644 --- a/Serializer/Normalizer/DictionaryEnumNormalizer.php +++ b/Serializer/Normalizer/DictionaryEnumNormalizer.php @@ -24,16 +24,23 @@ class DictionaryEnumNormalizer implements NormalizerInterface use TranslatorTrait; /** - * @param mixed $data - * @param string|null $format - * - * @return bool + * {@inheritdoc} */ - public function supportsNormalization(mixed $data, string $format = null): bool + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof DictionaryEnumInterface; } + /** + * {@inheritdoc} + */ + public function getSupportedTypes(?string $format): array + { + return [ + DictionaryEnumInterface::class => true, + ]; + } + /** * @param DictionaryEnumInterface $object * @param string|null $format diff --git a/Serializer/Normalizer/JsonSchemaErrorNormalizer.php b/Serializer/Normalizer/JsonSchemaErrorNormalizer.php index 3323dd8..5f6dcd4 100644 --- a/Serializer/Normalizer/JsonSchemaErrorNormalizer.php +++ b/Serializer/Normalizer/JsonSchemaErrorNormalizer.php @@ -24,11 +24,21 @@ class JsonSchemaErrorNormalizer implements NormalizerInterface /** * {@inheritdoc} */ - public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof JsonSchemaValidator; } + /** + * {@inheritdoc} + */ + public function getSupportedTypes(?string $format): array + { + return [ + JsonSchemaValidator::class => true, + ]; + } + /** * @param JsonSchemaValidator|object $object * @param string|null $format diff --git a/Tests/EventListener/Kernel/ApiExceptionFormatterListenerTest.php b/Tests/EventListener/Kernel/ApiExceptionFormatterListenerTest.php index 4b2fe70..c984942 100644 --- a/Tests/EventListener/Kernel/ApiExceptionFormatterListenerTest.php +++ b/Tests/EventListener/Kernel/ApiExceptionFormatterListenerTest.php @@ -14,6 +14,7 @@ use Doctrine\ODM\MongoDB\LockException; use Doctrine\ORM\OptimisticLockException; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sentry\ClientInterface; @@ -278,7 +279,7 @@ public function testOnKernelExceptionOnPaymentRequired(): void self::assertInstanceOf(HttpException::class, $exceptionEvent->getThrowable()); } - public function resourceNotFoundExceptionMessageDataProvider(): array + public static function resourceNotFoundExceptionMessageDataProvider(): array { return [ ['"App\\Entity\\Event\\Event\" object not found by \"Symfony\\Bridge\\Doctrine\\ArgumentResolver\\EntityValueResolver\". The expression \"repository.findClosedEventById(id)\" returned null.'], @@ -286,7 +287,7 @@ public function resourceNotFoundExceptionMessageDataProvider(): array ]; } - /** @dataProvider resourceNotFoundExceptionMessageDataProvider */ + #[DataProvider('resourceNotFoundExceptionMessageDataProvider')] public function testOnKernelExceptionWhenResourceNotFoundCausedByMapEntityAttribute(string $exceptionMessage): void { $httpException = new NotFoundHttpException($exceptionMessage); diff --git a/Tests/Serializer/Normalizer/ConstraintViolationListNormalizerTest.php b/Tests/Serializer/Normalizer/ConstraintViolationListNormalizerTest.php index d3934ff..1c79fc5 100644 --- a/Tests/Serializer/Normalizer/ConstraintViolationListNormalizerTest.php +++ b/Tests/Serializer/Normalizer/ConstraintViolationListNormalizerTest.php @@ -16,7 +16,7 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use StfalconStudio\ApiBundle\Serializer\Normalizer\ConstraintViolationListNormalizer; -use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer as SymfonyConstraintViolationListNormalizer; +use Symfony\Component\Serializer\Normalizer\NormalizerInterface as SymfonyConstraintViolationListNormalizer; use Symfony\Component\Validator\ConstraintViolationListInterface; final class ConstraintViolationListNormalizerTest extends TestCase @@ -61,20 +61,20 @@ public function testNormalize(string $originDetail, string $resultDetail): void public static function dataProviderForTestNormalize(): iterable { yield [ - 'origin_detail' => 'field1: Error description.', - 'result_detail' => 'Error description.', + 'originDetail' => 'field1: Error description.', + 'resultDetail' => 'Error description.', ]; yield [ - 'origin_detail' => "field1: Error description 1.\nfield2: Error description 2.", - 'result_detail' => "Error description 1.\nError description 2.", + 'originDetail' => "field1: Error description 1.\nfield2: Error description 2.", + 'resultDetail' => "Error description 1.\nError description 2.", ]; yield [ - 'origin_detail' => 'Error description.', - 'result_detail' => 'Error description.', + 'originDetail' => 'Error description.', + 'resultDetail' => 'Error description.', ]; yield [ - 'origin_detail' => "field1: Error :description 1.\nfield2: Error :description 2.", - 'result_detail' => "Error :description 1.\nError :description 2.", + 'originDetail' => "field1: Error :description 1.\nfield2: Error :description 2.", + 'resultDetail' => "Error :description 1.\nError :description 2.", ]; } diff --git a/Tests/Validator/JsonSchemaValidatorTest.php b/Tests/Validator/JsonSchemaValidatorTest.php index 69610d1..868b404 100644 --- a/Tests/Validator/JsonSchemaValidatorTest.php +++ b/Tests/Validator/JsonSchemaValidatorTest.php @@ -109,7 +109,7 @@ public function testInvalidJsonSchemaException(): void 2 => [$dummyJsonSchema, 'object'] }; }) - ->will(self::onConsecutiveCalls($violations, $normalizedJsonSchema)) + ->willReturn($violations, $normalizedJsonSchema) ; $this->expectException(InvalidJsonSchemaException::class); diff --git a/composer.json b/composer.json index 68ca326..acafadb 100644 --- a/composer.json +++ b/composer.json @@ -22,46 +22,46 @@ "ircmaxell/password-compat": "^v1.0.4", "justinrainbow/json-schema": "^5.2.12", "predis/predis": "^v1.1.10", - "sentry/sentry-symfony": "^4.8.0", - "symfony/config": "^6.1", - "symfony/dependency-injection": "^6.2", - "symfony/flex": "^2.2", - "symfony/http-kernel": "^6.2", - "symfony/http-foundation": "^6.2", - "symfony/framework-bundle": "^6.2", - "symfony/serializer": "^6.2", - "symfony/translation": "^6.2", - "symfony/validator": "^6.2" + "sentry/sentry-symfony": "^4.13.0", + "symfony/config": "^7.0", + "symfony/dependency-injection": "^7.0", + "symfony/flex": "^2.4", + "symfony/http-kernel": "^7.0", + "symfony/http-foundation": "^7.0", + "symfony/framework-bundle": "^7.0", + "symfony/serializer": "^7.0", + "symfony/translation": "^7.0", + "symfony/validator": "^7.0" }, "require-dev": { "doctrine/common": "^3.4", - "doctrine/mongodb-odm": "^2.5", - "doctrine/orm": "^2.15", + "doctrine/mongodb-odm": "^2.6", + "doctrine/orm": "^2.18", "escapestudios/symfony2-coding-standard": "^3.13", "gesdinet/jwt-refresh-token-bundle": "^v1.1.1", "jetbrains/phpstorm-attributes": "^1.0", - "lexik/jwt-authentication-bundle": "^v2.19", - "phpstan/phpstan": "^1.10.15", + "lexik/jwt-authentication-bundle": "^v2.20", + "phpstan/phpstan": "^1.10.58", "phpstan/phpstan-deprecation-rules": "^1.1.1", "phpstan/phpstan-doctrine": "^1.3", "phpstan/phpstan-phpunit": "^1.3", "phpstan/phpstan-symfony": "^1.3", - "phpunit/phpunit": "^10.1", + "phpunit/phpunit": "^11.0", "slam/phpstan-extensions": "^6.0", - "symfony/asset": "^6.2", - "symfony/console": "^6.2", - "symfony/dotenv": "^6.2", - "symfony/event-dispatcher": "^6.2", - "symfony/filesystem": "^6.2", - "symfony/form": "^6.2", - "symfony/http-client": "^6.2", - "symfony/messenger": "^6.2", - "symfony/routing": "^6.2", - "symfony/security-bundle": "^6.2", - "symfony/security-core": "^6.2", - "symfony/security-csrf": "^6.2", - "symfony/workflow": "^6.2", - "twig/twig": "^v3.6" + "symfony/asset": "^7.0", + "symfony/console": "^7.0", + "symfony/dotenv": "^7.0", + "symfony/event-dispatcher": "^7.0", + "symfony/filesystem": "^7.0", + "symfony/form": "^7.0", + "symfony/http-client": "^7.0", + "symfony/messenger": "^7.0", + "symfony/routing": "^7.0", + "symfony/security-bundle": "^7.0", + "symfony/security-core": "^7.0", + "symfony/security-csrf": "^7.0", + "symfony/workflow": "^7.0", + "twig/twig": "^v3.8" }, "autoload": { "psr-4": {