-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract product option value normalizer
- Loading branch information
Showing
8 changed files
with
240 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Event/ProductDocumentType/ProductDocumentTypeProductOptionValueNormalizeEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Event\ProductDocumentType; | ||
|
||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Product\Model\ProductOptionValueInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class ProductDocumentTypeProductOptionValueNormalizeEvent extends Event | ||
{ | ||
public function __construct( | ||
private readonly ProductOptionValueInterface $optionValue, | ||
private readonly ChannelInterface $channel, | ||
private array $normalizedProductOptionValue, | ||
) { | ||
} | ||
|
||
public function getOptionValue(): ProductOptionValueInterface | ||
{ | ||
return $this->optionValue; | ||
} | ||
|
||
public function getChannel(): ChannelInterface | ||
{ | ||
return $this->channel; | ||
} | ||
|
||
public function getNormalizedProductOptionValue(): array | ||
{ | ||
return $this->normalizedProductOptionValue; | ||
} | ||
|
||
public function setNormalizedProductOptionValue(array $normalizedProductOptionValue): void | ||
{ | ||
$this->normalizedProductOptionValue = $normalizedProductOptionValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Webgriffe\SyliusElasticsearchPlugin\Serializer; | ||
|
||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Product\Model\ProductOptionValueInterface; | ||
use Sylius\Component\Product\Model\ProductOptionValueTranslationInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Webgriffe\SyliusElasticsearchPlugin\Event\ProductDocumentType\ProductDocumentTypeProductOptionValueNormalizeEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ProductOptionValueNormalizer implements NormalizerInterface | ||
{ | ||
public function __construct( | ||
private readonly EventDispatcherInterface $eventDispatcher, | ||
) { | ||
} | ||
|
||
/** | ||
* @param ProductOptionValueInterface|mixed $object | ||
*/ | ||
public function normalize(mixed $object, ?string $format = null, array $context = []): array | ||
{ | ||
$channel = $context['channel']; | ||
$optionValue = $object; | ||
Assert::isInstanceOf($optionValue, ProductOptionValueInterface::class); | ||
Assert::isInstanceOf($channel, ChannelInterface::class); | ||
|
||
$normalizedOptionValue = [ | ||
'sylius-id' => $optionValue->getId(), | ||
'code' => $optionValue->getCode(), | ||
'value' => $optionValue->getValue(), | ||
'name' => [], | ||
]; | ||
/** @var ProductOptionValueTranslationInterface $optionValueTranslation */ | ||
foreach ($optionValue->getTranslations() as $optionValueTranslation) { | ||
$localeCode = $optionValueTranslation->getLocale(); | ||
Assert::string($localeCode); | ||
$normalizedOptionValue['name'][] = [ | ||
$localeCode => $optionValueTranslation->getValue(), | ||
]; | ||
} | ||
|
||
$event = new ProductDocumentTypeProductOptionValueNormalizeEvent($optionValue, $channel, $normalizedOptionValue); | ||
$this->eventDispatcher->dispatch($event); | ||
|
||
return $event->getNormalizedProductOptionValue(); | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [ProductOptionValueInterface::class => true]; | ||
} | ||
|
||
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool | ||
{ | ||
return $data instanceof ProductOptionValueInterface && | ||
array_key_exists('type', $context) && | ||
$context['type'] === 'webgriffe_sylius_elasticsearch_plugin' | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
tests/Unit/Serializer/ProductOptionValueNormalizerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Webgriffe\SyliusElasticsearchPlugin\Unit\Serializer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Sylius\Component\Core\Model\Channel; | ||
use Sylius\Component\Product\Model\ProductOptionValue; | ||
use Sylius\Component\Product\Model\ProductOptionValueInterface; | ||
use Sylius\Component\Product\Model\ProductOptionValueTranslation; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
use Tests\Webgriffe\SyliusElasticsearchPlugin\App\Entity\Product\ProductOption; | ||
use Webgriffe\SyliusElasticsearchPlugin\Serializer\ProductOptionValueNormalizer; | ||
|
||
class ProductOptionValueNormalizerTest extends TestCase | ||
{ | ||
private ProductOptionValueNormalizer $productOptionValueNormalizer; | ||
private ProductOptionValueInterface $productOptionValueToNormalize; | ||
private Channel $channel; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->productOptionValueNormalizer = new ProductOptionValueNormalizer( | ||
new EventDispatcher(), | ||
); | ||
|
||
$this->productOptionValueToNormalize = new ProductOptionValue(); | ||
|
||
$reflectionProductOptionValue = new \ReflectionClass(ProductOptionValue::class); | ||
$productOptionValueIdProperty = $reflectionProductOptionValue->getProperty('id'); | ||
|
||
$sizeProductOption = new ProductOption(); | ||
|
||
$productOptionValueIdProperty->setValue($this->productOptionValueToNormalize, 21); | ||
$this->productOptionValueToNormalize->setCode('S'); | ||
$this->productOptionValueToNormalize->setOption($sizeProductOption); | ||
$this->productOptionValueToNormalize->setCurrentLocale('it_IT'); | ||
$this->productOptionValueToNormalize->setFallbackLocale('en_US'); | ||
|
||
$sizeProductOptionValueTranslation = new ProductOptionValueTranslation(); | ||
$sizeProductOptionValueTranslation->setLocale('en_US'); | ||
$sizeProductOptionValueTranslation->setValue('Small'); | ||
$this->productOptionValueToNormalize->addTranslation($sizeProductOptionValueTranslation); | ||
|
||
$sizeProductOptionValueTranslation = new ProductOptionValueTranslation(); | ||
$sizeProductOptionValueTranslation->setLocale('it_IT'); | ||
$sizeProductOptionValueTranslation->setValue('Piccola'); | ||
$this->productOptionValueToNormalize->addTranslation($sizeProductOptionValueTranslation); | ||
|
||
$this->channel = new Channel(); | ||
} | ||
|
||
public function testItIsInstantiable(): void | ||
{ | ||
$this->assertInstanceOf(ProductOptionValueNormalizer::class, $this->productOptionValueNormalizer); | ||
} | ||
|
||
public function testItIsAnInstanceOfNormalizer(): void | ||
{ | ||
$this->assertInstanceOf(NormalizerInterface::class, $this->productOptionValueNormalizer); | ||
} | ||
|
||
public function testItSupportProductVariantInterfaceType(): void | ||
{ | ||
$supportedTypes= $this->productOptionValueNormalizer->getSupportedTypes(null); | ||
|
||
$this->assertArrayHasKey(ProductOptionValueInterface::class, $supportedTypes); | ||
$this->assertTrue($supportedTypes[ProductOptionValueInterface::class]); | ||
} | ||
|
||
public function testItSupportNormalizationWithRightType(): void | ||
{ | ||
$this->assertTrue($this->productOptionValueNormalizer->supportsNormalization($this->productOptionValueToNormalize, null, ['type' => 'webgriffe_sylius_elasticsearch_plugin'])); | ||
} | ||
|
||
public function testItDoesNotSupportNormalizationWithRightType(): void | ||
{ | ||
$this->assertFalse($this->productOptionValueNormalizer->supportsNormalization($this->productOptionValueToNormalize, null, ['type' => 'other'])); | ||
} | ||
|
||
public function testItDoesNotSupportNormalizationWithoutType(): void | ||
{ | ||
$this->assertFalse($this->productOptionValueNormalizer->supportsNormalization($this->productOptionValueToNormalize)); | ||
} | ||
|
||
public function testItNormalizeProductVariant(): void | ||
{ | ||
$productOptionValueNormalized = $this->productOptionValueNormalizer->normalize($this->productOptionValueToNormalize, null, ['channel' => $this->channel]); | ||
$this->assertIsArray($productOptionValueNormalized); | ||
|
||
$this->assertEquals(21, $productOptionValueNormalized['sylius-id']); | ||
$this->assertEquals('S', $productOptionValueNormalized['code']); | ||
$this->assertEquals('Piccola', $productOptionValueNormalized['value']); | ||
$this->assertEquals([ | ||
['en_US' => 'Small'], | ||
['it_IT' => 'Piccola'], | ||
], $productOptionValueNormalized['name']); | ||
} | ||
} |
Oops, something went wrong.