Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[make:serializer:encoder] fix interface signature mismatch in template #1525

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading