From ff1c8c5ad2bc40c70519e724acdf70a272bdc580 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com> Date: Wed, 24 Apr 2024 05:43:58 -0400 Subject: [PATCH] bug #1525 [make:serializer:encoder] fix interface signature mismatch in template (#1525) --- src/Maker/MakeSerializerEncoder.php | 3 ++ .../skeleton/serializer/Encoder.tpl.php | 8 ++--- tests/Maker/MakeSerializerEncoderTest.php | 35 ++++++++++++++++--- 3 files changed, 37 insertions(+), 9 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..75f1702f7 100644 --- a/tests/Maker/MakeSerializerEncoderTest.php +++ b/tests/Maker/MakeSerializerEncoderTest.php @@ -25,12 +25,10 @@ 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'); + } $runner->runMaker( [ // encoder class name @@ -39,6 +37,33 @@ 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() + ->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')) + ); }), ]; }