Skip to content

Commit

Permalink
[Maintenance] Fix ProductNormalizer not resolving custom ProductAss…
Browse files Browse the repository at this point in the history
…ociation classes
  • Loading branch information
NoResponseMate committed Jun 26, 2024
1 parent 09cd99e commit 9136e49
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/services/serializer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<argument type="service" id="CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer\ProductNormalizer.inner" />
<argument type="service" id="api_platform.item_data_provider" />
<argument type="service" id="Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface" />
<argument type="string">%sylius.model.product_association.class%</argument>
<tag name="serializer.normalizer" priority="64" />
</service>
</services>
Expand Down
11 changes: 9 additions & 2 deletions src/Api/Normalizer/ProductNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface;
use Sylius\Component\Product\Model\ProductAssociation;
use Sylius\Component\Product\Model\ProductAssociationInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
Expand All @@ -19,7 +18,15 @@ public function __construct(
private NormalizerInterface $decoratedNormalizer,
private ItemDataProviderInterface $itemDataProvider,
private IriToIdentifierConverterInterface $iriToIdentifierConverter,
private string $productAssociationClass = '',
) {
if (!is_a($productAssociationClass, ProductAssociationInterface::class, true)) {
throw new \InvalidArgumentException(sprintf(
'The class "%s" must implement "%s".',
$productAssociationClass,
ProductAssociationInterface::class,
));
}
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
Expand All @@ -39,7 +46,7 @@ public function normalize($object, string $format = null, array $context = []):
foreach ($associations as $association) {
$id = $this->iriToIdentifierConverter->getIdentifier($association);
/** @var ProductAssociationInterface $associationObject */
$associationObject = $this->itemDataProvider->getItem(ProductAssociation::class, (string) $id);
$associationObject = $this->itemDataProvider->getItem($this->productAssociationClass, (string) $id);
$associationTypeCode = $associationObject->getType()?->getCode();

if (null === $associationTypeCode) {
Expand Down
22 changes: 19 additions & 3 deletions tests/Unit/Api/Normalizer/ProductNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ final class ProductNormalizerTest extends TestCase
{
use ProphecyTrait;

private string $productAssociationClass = ProductAssociation::class;

public function testItThrowsInvalidArgumentExceptionWhenProductAssociationClassDoesNotImplementProductAssociationInterface(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The class "stdClass" must implement "Sylius\Component\Product\Model\ProductAssociationInterface".');

new ProductNormalizer(
$this->prophesize(ContextAwareNormalizerInterface::class)->reveal(),
$this->prophesize(ItemDataProviderInterface::class)->reveal(),
$this->prophesize(IriToIdentifierConverterInterface::class)->reveal(),
\stdClass::class,
);
}

public function testItAddsAssociationsTypesToProductResponse(): void
{
$baseNormalizer = $this->prophesize(ProductNormalizerInterface::class);
Expand All @@ -29,7 +44,8 @@ public function testItAddsAssociationsTypesToProductResponse(): void
$normalizer = new ProductNormalizer(
$baseNormalizer->reveal(),
$itemDataProvider->reveal(),
$iriToIdentifierConverter->reveal()
$iriToIdentifierConverter->reveal(),
$this->productAssociationClass,
);

$product = $this->prophesize(Product::class);
Expand All @@ -52,9 +68,9 @@ public function testItAddsAssociationsTypesToProductResponse(): void
$secondAssociationType->getCode()->willReturn('second_association_type');

$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/1')->willReturn('1');
$itemDataProvider->getItem(ProductAssociation::class, '1')->willReturn($firstAssociation);
$itemDataProvider->getItem($this->productAssociationClass, '1')->willReturn($firstAssociation);
$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/2')->willReturn('2');
$itemDataProvider->getItem(ProductAssociation::class, '2')->willReturn($secondAssociation);
$itemDataProvider->getItem($this->productAssociationClass, '2')->willReturn($secondAssociation);

self::assertSame(
[
Expand Down

0 comments on commit 9136e49

Please sign in to comment.