Skip to content

Commit

Permalink
[API] Less naive default translation checking
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate committed Nov 7, 2023
1 parent 0708088 commit a3420a1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,11 @@ parameters:
count: 1
path: src/Sylius/Bundle/ApiBundle/Serializer/ShippingMethodNormalizer.php

-
message: "#^Method Sylius\\\\Bundle\\\\ApiBundle\\\\Serializer\\\\TranslatableDenormalizer\\:\\:hasDefaultTranslationConfigured\\(\\) has parameter \\$translations with no value type specified in iterable type array\\.$#"
count: 1
path: src/Sylius/Bundle/ApiBundle/Serializer/TranslatableDenormalizer.php

-
message: "#^Method Sylius\\\\Bundle\\\\ApiBundle\\\\Serializer\\\\ZoneDenormalizer\\:\\:getZoneMemberByCode\\(\\) has parameter \\$zoneMembers with generic interface Doctrine\\\\Common\\\\Collections\\\\Selectable but does not specify its types\\: TKey, T$#"
count: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function denormalize(mixed $data, string $type, string $format = null, ar

$defaultLocaleCode = $this->localeProvider->getDefaultLocaleCode();

if (!isset($data['translations'][$defaultLocaleCode])) {
if (!$this->hasDefaultTranslation($data['translations'] ?? [], $defaultLocaleCode)) {
$data['translations'][$defaultLocaleCode] = [
'locale' => $defaultLocaleCode,
];
Expand All @@ -60,4 +60,12 @@ private static function getAlreadyCalledKey(string $class): string
{
return sprintf(self::ALREADY_CALLED, $class);
}

private function hasDefaultTranslation(array $translations, string $defaultLocale): bool

Check failure on line 64 in src/Sylius/Bundle/ApiBundle/Serializer/TranslatableDenormalizer.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.0, Symfony ^5.4, API Platform ^2.7.10

Method Sylius\Bundle\ApiBundle\Serializer\TranslatableDenormalizer::hasDefaultTranslation() has parameter $translations with no value type specified in iterable type array.

Check failure on line 64 in src/Sylius/Bundle/ApiBundle/Serializer/TranslatableDenormalizer.php

View workflow job for this annotation

GitHub Actions / Static checks / PHP 8.2, Symfony ^6.0, API Platform ^2.7.10

Method Sylius\Bundle\ApiBundle\Serializer\TranslatableDenormalizer::hasDefaultTranslation() has parameter $translations with no value type specified in iterable type array.
{
return
isset($translations[$defaultLocale]['locale']) &&
$defaultLocale === $translations[$defaultLocale]['locale']
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function it_does_nothing_when_data_contains_a_translation_in_default_locale(
;
}

function it_adds_default_translation_when_no_translation_passed_in_data(
function it_adds_default_translation_when_no_translations_passed_in_data(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
): void {
Expand All @@ -84,6 +84,86 @@ function it_adds_default_translation_when_no_translation_passed_in_data(
;
}

function it_adds_default_translation_when_no_translation_passed_for_default_locale_in_data(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
): void {
$localeProvider->getDefaultLocaleCode()->willReturn('en');

$originalData = ['translations' => ['en' => []]];
$updatedData = ['translations' => ['en' => ['locale' => 'en']]];

$denormalizer->denormalize($updatedData, TranslatableInterface::class, null, [
'sylius_translatable_denormalizer_already_called_for_Sylius\Component\Resource\Model\TranslatableInterface' => true,
ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST',
])->shouldBeCalled()->willReturn($updatedData);

$this
->denormalize($originalData, TranslatableInterface::class, null, [ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST'])
->shouldReturn($updatedData)
;
}

function it_adds_default_translation_when_passed_default_translation_has_empty_locale(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
): void {
$localeProvider->getDefaultLocaleCode()->willReturn('en');

$originalData = ['translations' => ['en' => ['locale' => '']]];
$updatedData = ['translations' => ['en' => ['locale' => 'en']]];

$denormalizer->denormalize($updatedData, TranslatableInterface::class, null, [
'sylius_translatable_denormalizer_already_called_for_Sylius\Component\Resource\Model\TranslatableInterface' => true,
ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST',
])->shouldBeCalled()->willReturn($updatedData);

$this
->denormalize($originalData, TranslatableInterface::class, null, [ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST'])
->shouldReturn($updatedData)
;
}

function it_adds_default_translation_when_passed_default_translation_has_null_locale(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
): void {
$localeProvider->getDefaultLocaleCode()->willReturn('en');

$originalData = ['translations' => ['en' => ['locale' => null]]];
$updatedData = ['translations' => ['en' => ['locale' => 'en']]];

$denormalizer->denormalize($updatedData, TranslatableInterface::class, null, [
'sylius_translatable_denormalizer_already_called_for_Sylius\Component\Resource\Model\TranslatableInterface' => true,
ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST',
])->shouldBeCalled()->willReturn($updatedData);

$this
->denormalize($originalData, TranslatableInterface::class, null, [ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST'])
->shouldReturn($updatedData)
;
}

function it_adds_default_translation_when_passed_default_translation_has_mismatched_locale(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
): void {
$localeProvider->getDefaultLocaleCode()->willReturn('en');

$originalData = ['translations' => ['en' => ['locale' => 'fr']]];
$updatedData = ['translations' => ['en' => ['locale' => 'en']]];

$denormalizer->denormalize($updatedData, TranslatableInterface::class, null, [
'sylius_translatable_denormalizer_already_called_for_Sylius\Component\Resource\Model\TranslatableInterface' => true,
ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST',
])->shouldBeCalled()->willReturn($updatedData);

$this
->denormalize($originalData, TranslatableInterface::class, null, [ContextKeys::HTTP_REQUEST_METHOD_TYPE => 'POST'])
->shouldReturn($updatedData)
;
}

function it_adds_default_translation_when_no_translation_in_default_locale_passed_in_data(
DenormalizerInterface $denormalizer,
TranslationLocaleProviderInterface $localeProvider,
Expand Down

0 comments on commit a3420a1

Please sign in to comment.