Skip to content

Commit

Permalink
bug #1525 [make:serializer:encoder] fix interface signature mismatch …
Browse files Browse the repository at this point in the history
…in template (#1525)
  • Loading branch information
jrushlow committed Apr 24, 2024
1 parent ed3465b commit ff1c8c5
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Maker/MakeSerializerEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]
);

Expand Down
8 changes: 4 additions & 4 deletions src/Resources/skeleton/serializer/Encoder.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ class <?= $class_name ?> implements EncoderInterface, DecoderInterface
{
public const FORMAT = '<?= $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 = [])<?php if ($use_decoder_return_type): ?>: mixed<?php endif; ?>
{
// TODO: return your decoded data
return '';
}

public function supportsDecoding(string $format, array $context = []): bool
public function supportsDecoding(string $format): bool
{
return self::FORMAT === $format;
}
Expand Down
35 changes: 30 additions & 5 deletions tests/Maker/MakeSerializerEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'))
);
}),
];
}
Expand Down

0 comments on commit ff1c8c5

Please sign in to comment.