Skip to content
Open
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
16 changes: 16 additions & 0 deletions config/services/query.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQuery">
<argument type="service" id="sylius.manager.product_association" />
<argument type="string">%sylius.model.product_association.class%</argument>
</service>

<service id="CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface" alias="CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQuery" />
</services>
</container>
3 changes: 1 addition & 2 deletions config/services/serializer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
<services>
<service id="CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer\ProductNormalizer" decorates="Sylius\Bundle\ApiBundle\Serializer\ProductNormalizer">
<argument type="service" id="CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer\ProductNormalizer.inner" />
<argument type="service" id="api_platform.item_data_provider" />
<argument type="service" id="CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface" />
<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
20 changes: 5 additions & 15 deletions src/Api/Normalizer/ProductNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Exception\ProductAssociationTypeNotFoundException;
use Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface;
use Sylius\Component\Product\Model\ProductAssociationInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand All @@ -16,17 +16,9 @@ final class ProductNormalizer implements ContextAwareNormalizerInterface, Normal
/** @param ContextAwareNormalizerInterface&NormalizerAwareInterface $decoratedNormalizer */
public function __construct(
private NormalizerInterface $decoratedNormalizer,
private ItemDataProviderInterface $itemDataProvider,
private GetAssociationTypeCodeByAssociationIdQueryInterface $getAssociationTypeCodeByAssociationIdQuery,
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 @@ -45,12 +37,10 @@ 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($this->productAssociationClass, (string) $id);
$associationTypeCode = $associationObject->getType()?->getCode();
$associationTypeCode = $this->getAssociationTypeCodeByAssociationIdQuery->get((int) $id);

if (null === $associationTypeCode) {
continue;
throw new ProductAssociationTypeNotFoundException((int) $id);
}

$object['associations'][$associationTypeCode] = $association;
Expand Down
34 changes: 34 additions & 0 deletions src/Doctrine/Query/GetAssociationTypeCodeByAssociationIdQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query;

use Doctrine\ORM\EntityManagerInterface;

final class GetAssociationTypeCodeByAssociationIdQuery implements GetAssociationTypeCodeByAssociationIdQueryInterface
{
public function __construct(
private EntityManagerInterface $productAssociationManager,
private string $productAssociationClass,
) {
}

public function get(int $associationId): ?string
{
$queryBuilder = $this->productAssociationManager->createQueryBuilder();

/** @var string|null $result */
$result = $queryBuilder
->select('t.code')
->from($this->productAssociationClass, 'pa')
->join('pa.type', 't')
->where('pa.id = :associationId')
->setParameter('associationId', $associationId)
->getQuery()
->getSingleScalarResult()
;

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query;

interface GetAssociationTypeCodeByAssociationIdQueryInterface
{
public function get(int $associationId): ?string;
}
13 changes: 13 additions & 0 deletions src/Exception/ProductAssociationTypeNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Exception;

final class ProductAssociationTypeNotFoundException extends \RuntimeException
{
public function __construct(int $associationId)
{
parent::__construct(sprintf('Product association type not found for association with id "%d".', $associationId));
}
}
69 changes: 35 additions & 34 deletions tests/Unit/Api/Normalizer/ProductNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,30 @@

namespace Tests\CommerceWeavers\SyliusAlsoBoughtPlugin\Unit\Api\Normalizer;

use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Api\Normalizer\ProductNormalizer;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Doctrine\Query\GetAssociationTypeCodeByAssociationIdQueryInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Exception\ProductAssociationTypeNotFoundException;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Sylius\Bundle\ApiBundle\Converter\IriToIdentifierConverterInterface;
use Sylius\Component\Core\Model\Product;
use Sylius\Component\Product\Model\ProductAssociation;
use Sylius\Component\Product\Model\ProductAssociationInterface;
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;

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);
$itemDataProvider = $this->prophesize(ItemDataProviderInterface::class);
$query = $this->prophesize(GetAssociationTypeCodeByAssociationIdQueryInterface::class);
$iriToIdentifierConverter = $this->prophesize(IriToIdentifierConverterInterface::class);

$normalizer = new ProductNormalizer(
$baseNormalizer->reveal(),
$itemDataProvider->reveal(),
$query->reveal(),
$iriToIdentifierConverter->reveal(),
$this->productAssociationClass,
);

$product = $this->prophesize(Product::class);
Expand All @@ -57,20 +39,10 @@ public function testItAddsAssociationsTypesToProductResponse(): void
],
]);

$firstAssociation = $this->prophesize(ProductAssociationInterface::class);
$firstAssociationType = $this->prophesize(ProductAssociationTypeInterface::class);
$firstAssociation->getType()->willReturn($firstAssociationType);
$firstAssociationType->getCode()->willReturn('first_association_type');

$secondAssociation = $this->prophesize(ProductAssociationInterface::class);
$secondAssociationType = $this->prophesize(ProductAssociationTypeInterface::class);
$secondAssociation->getType()->willReturn($secondAssociationType);
$secondAssociationType->getCode()->willReturn('second_association_type');

$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/1')->willReturn('1');
$itemDataProvider->getItem($this->productAssociationClass, '1')->willReturn($firstAssociation);
$query->get(1)->willReturn('first_association_type');
$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/2')->willReturn('2');
$itemDataProvider->getItem($this->productAssociationClass, '2')->willReturn($secondAssociation);
$query->get(2)->willReturn('second_association_type');

self::assertSame(
[
Expand All @@ -83,6 +55,35 @@ public function testItAddsAssociationsTypesToProductResponse(): void
$normalizer->normalize($product->reveal())
);
}

public function testItThrowsExceptionWhenAssociationTypeCodeIsNull(): void
{
$baseNormalizer = $this->prophesize(ProductNormalizerInterface::class);
$query = $this->prophesize(GetAssociationTypeCodeByAssociationIdQueryInterface::class);
$iriToIdentifierConverter = $this->prophesize(IriToIdentifierConverterInterface::class);

$normalizer = new ProductNormalizer(
$baseNormalizer->reveal(),
$query->reveal(),
$iriToIdentifierConverter->reveal(),
);

$product = $this->prophesize(Product::class);
$baseNormalizer->normalize($product->reveal(), null, [])->willReturn([
'code' => 'product_code',
'associations' => [
'/api/v2/product-associations/1',
],
]);

$iriToIdentifierConverter->getIdentifier('/api/v2/product-associations/1')->willReturn('1');
$query->get(1)->willReturn(null);

$this->expectException(ProductAssociationTypeNotFoundException::class);
$this->expectExceptionMessage('Product association type not found for association with id "1".');

$normalizer->normalize($product->reveal());
}
}

interface ProductNormalizerInterface extends ContextAwareNormalizerInterface, NormalizerAwareInterface
Expand Down