From 7863663a5a9da5c2713a36f549035547ad584a8a Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Wed, 24 Apr 2024 04:42:12 -0400 Subject: [PATCH 1/2] [make:serializer:encoder] fix interface signature mismatch in template --- src/Maker/MakeSerializerEncoder.php | 3 ++ .../skeleton/serializer/Encoder.tpl.php | 8 ++--- tests/Maker/MakeSerializerEncoderTest.php | 35 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/Maker/MakeSerializerEncoder.php b/src/Maker/MakeSerializerEncoder.php index f71e4af55..7bdb69f11 100644 --- a/src/Maker/MakeSerializerEncoder.php +++ b/src/Maker/MakeSerializerEncoder.php @@ -19,6 +19,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Serializer\Encoder\DecoderInterface; use Symfony\Component\Serializer\Encoder\EncoderInterface; use Symfony\Component\Serializer\Serializer; @@ -61,12 +62,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen EncoderInterface::class, ]); + /* @legacy - Remove "decoder_return_type" when Symfony 6.4 is no longer supported */ $generator->generateClass( $encoderClassNameDetails->getFullName(), 'serializer/Encoder.tpl.php', [ 'use_statements' => $useStatements, 'format' => $format, + 'use_decoder_return_type' => Kernel::VERSION_ID >= 70000, ] ); diff --git a/src/Resources/skeleton/serializer/Encoder.tpl.php b/src/Resources/skeleton/serializer/Encoder.tpl.php index 620eac0db..584b15db8 100644 --- a/src/Resources/skeleton/serializer/Encoder.tpl.php +++ b/src/Resources/skeleton/serializer/Encoder.tpl.php @@ -8,24 +8,24 @@ class implements EncoderInterface, DecoderInterface { public const FORMAT = ''; - public function encode($data, string $format, array $context = []): string + public function encode(mixed $data, string $format, array $context = []): string { // TODO: return your encoded data return ''; } - public function supportsEncoding(string $format, array $context = []): bool + public function supportsEncoding(string $format): bool { return self::FORMAT === $format; } - public function decode(string $data, string $format, array $context = []) + public function decode(string $data, string $format, array $context = []): mixed { // TODO: return your decoded data return ''; } - public function supportsDecoding(string $format, array $context = []): bool + public function supportsDecoding(string $format): bool { return self::FORMAT === $format; } diff --git a/tests/Maker/MakeSerializerEncoderTest.php b/tests/Maker/MakeSerializerEncoderTest.php index 73bee89af..2a0a0932d 100644 --- a/tests/Maker/MakeSerializerEncoderTest.php +++ b/tests/Maker/MakeSerializerEncoderTest.php @@ -31,6 +31,9 @@ public function getTestDetails(): \Generator // property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063 ->addExtraDependencies('symfony/serializer-pack:*') ->run(function (MakerTestRunner $runner) { + if (70000 >= $runner->getSymfonyVersion()) { + $this->markTestSkipped('Legacy Symfony 6.4 Test'); + } $runner->runMaker( [ // encoder class name @@ -39,6 +42,38 @@ public function getTestDetails(): \Generator 'foobar', ] ); + + self::assertStringContainsString( + needle: 'public function decode(string $data, string $format, array $context = []): mixed', + haystack: file_get_contents($runner->getPath('src/Serializer/FooBarEncoder.php')) + ); + }), + ]; + + /* @legacy - Remove when MakerBundle no longer supports Symfony 6.4 */ + yield 'it_makes_serializer_encoder_legacy' => [$this->createMakerTest() + // serializer-pack 1.1 requires symfony/property-info >= 5.4 + // adding symfony/serializer-pack:* as an extra depends allows + // us to use serializer-pack < 1.1 which does not conflict with + // property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063 + ->addExtraDependencies('symfony/serializer-pack:*') + ->run(function (MakerTestRunner $runner) { + if (70000 < $runner->getSymfonyVersion()) { + $this->markTestSkipped('Legacy Symfony 6.4 Test'); + } + $runner->runMaker( + [ + // encoder class name + 'FooBarEncoder', + // encoder format + 'foobar', + ] + ); + + self::assertStringNotContainsString( + needle: 'public function decode(string $data, string $format, array $context = []): mixed', + haystack: file_get_contents($runner->getPath('src/Serializer/FooBarEncoder.php')) + ); }), ]; } From 6aa327f5a3bed68dbecd2823e80cb9c2b8caa030 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Wed, 24 Apr 2024 05:17:51 -0400 Subject: [PATCH 2/2] remove legacy test dependencies --- tests/Maker/MakeSerializerEncoderTest.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/Maker/MakeSerializerEncoderTest.php b/tests/Maker/MakeSerializerEncoderTest.php index 2a0a0932d..75f1702f7 100644 --- a/tests/Maker/MakeSerializerEncoderTest.php +++ b/tests/Maker/MakeSerializerEncoderTest.php @@ -25,11 +25,6 @@ protected function getMakerClass(): string public function getTestDetails(): \Generator { yield 'it_makes_serializer_encoder' => [$this->createMakerTest() - // serializer-pack 1.1 requires symfony/property-info >= 5.4 - // adding symfony/serializer-pack:* as an extra depends allows - // us to use serializer-pack < 1.1 which does not conflict with - // property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063 - ->addExtraDependencies('symfony/serializer-pack:*') ->run(function (MakerTestRunner $runner) { if (70000 >= $runner->getSymfonyVersion()) { $this->markTestSkipped('Legacy Symfony 6.4 Test'); @@ -52,11 +47,6 @@ public function getTestDetails(): \Generator /* @legacy - Remove when MakerBundle no longer supports Symfony 6.4 */ yield 'it_makes_serializer_encoder_legacy' => [$this->createMakerTest() - // serializer-pack 1.1 requires symfony/property-info >= 5.4 - // adding symfony/serializer-pack:* as an extra depends allows - // us to use serializer-pack < 1.1 which does not conflict with - // property-info < 5.4. E.g. Symfony 5.3 tests. See PR 1063 - ->addExtraDependencies('symfony/serializer-pack:*') ->run(function (MakerTestRunner $runner) { if (70000 < $runner->getSymfonyVersion()) { $this->markTestSkipped('Legacy Symfony 6.4 Test');