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

Do not duplicate product option values #185

Merged
merged 2 commits into from
Nov 7, 2023
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
2 changes: 1 addition & 1 deletion spec/ValueHandler/ProductOptionValueHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Resource\Translation\Provider\TranslationLocaleProviderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Webgriffe\SyliusAkeneoPlugin\ApiClientInterface;
use Webgriffe\SyliusAkeneoPlugin\ValueHandler\ProductOptionValueHandler;
use Webgriffe\SyliusAkeneoPlugin\ValueHandlerInterface;

Expand Down Expand Up @@ -61,6 +60,7 @@ public function let(
): void {
$productVariant->getCode()->willReturn(self::VARIANT_CODE);
$productVariant->getProduct()->willReturn($product);
$productVariant->getOptionValues()->willReturn(new ArrayCollection());
$product->getCode()->willReturn(self::PRODUCT_CODE);
$product->getOptions()->willReturn(new ArrayCollection([$productOption->getWrappedObject()]));
$productOption->getCode()->willReturn(self::OPTION_CODE);
Expand Down
2 changes: 2 additions & 0 deletions src/ValueHandler/ProductOptionValueHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public function handle($productVariant, string $optionCode, array $akeneoValue):
/** @var string|array|bool|int $akeneoValueData */
$akeneoValueData = $akeneoValue[0]['data'];

$productVariant->getOptionValues()->clear();

$productOption = $this->getProductOption($optionCode, $productVariant, $product);

/** @var string $attributeType */
Expand Down
146 changes: 146 additions & 0 deletions tests/InMemory/Client/Api/InMemoryApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api;

use Akeneo\Pim\ApiClient\Api\Operation\CreatableResourceInterface;
use Akeneo\Pim\ApiClient\Api\Operation\DeletableResourceInterface;
use Akeneo\Pim\ApiClient\Api\Operation\GettableResourceInterface;
use Akeneo\Pim\ApiClient\Api\Operation\ListableResourceInterface;
use Akeneo\Pim\ApiClient\Api\Operation\UpsertableResourceInterface;
use Akeneo\Pim\ApiClient\Api\Operation\UpsertableResourceListInterface;
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use ArrayIterator;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Promise\Promise;
use Psr\Http\Message\StreamInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\ResourceInterface;
use Webmozart\Assert\Assert;

/**
* @template T of ResourceInterface
*/
abstract class InMemoryApi implements
ListableResourceInterface,
GettableResourceInterface,
CreatableResourceInterface,
UpsertableResourceInterface,
UpsertableResourceListInterface,
DeletableResourceInterface
{
/**
* @var array<string, T>
*/
public static array $resources = [];

/** @return class-string */
abstract protected function getResourceClass(): string;

public static function addResource(ResourceInterface $resource)
{
self::$resources[$resource->getIdentifier()] = $resource;
}

public function create(string $code, array $data = []): int
{
$class = $this->getResourceClass();
Assert::isInstanceOf($class, ResourceInterface::class);

self::$resources[] = call_user_func([$class, 'create'], [$code, $data]);

return 201;
}

public function delete(string $code): int
{
if (!array_key_exists($code, self::$resources)) {
throw $this->createNotFoundException();
}
unset(self::$resources[$code]);

return 204;
}

public function listPerPage(int $limit = 100, bool $withCount = false, array $queryParameters = []): PageInterface
{
// TODO: Implement listPerPage() method.
}

public function all(int $pageSize = 100, array $queryParameters = []): ResourceCursorInterface
{
return new class(new ArrayIterator(self::$resources), $pageSize) implements ResourceCursorInterface {
public function __construct(private ArrayIterator $iterator, private int $pageSize)
{
}

public function current()
{
return $this->iterator->current();
}

public function next(): void
{
$this->iterator->next();
}

public function key(): mixed
{
return $this->iterator->key();
}

public function valid(): bool
{
return $this->iterator->valid();
}

public function rewind(): void
{
$this->iterator->rewind();
}

public function getPageSize(): ?int
{
return $this->pageSize;
}
};
}

public function get(string $code, array $queryParameters = []): array
{
if (!array_key_exists($code, self::$resources)) {
throw $this->createNotFoundException();
}

return (array) self::$resources[$code];
}

public function upsert(string $code, array $data = []): int
{
// TODO: Implement upsert() method.
}

public function upsertAsync(string $code, array $data = []): PromiseInterface|Promise
{
// TODO: Implement upsertAsync() method.
}

public function upsertList(StreamInterface|array $resources): \Traversable
{
// TODO: Implement upsertList() method.
}

public function upsertAsyncList(StreamInterface|array $resources): PromiseInterface|Promise
{
// TODO: Implement upsertAsyncList() method.
}

private function createNotFoundException(): NotFoundHttpException
{
return new NotFoundHttpException('Resource not found', new Request('GET', '/'), new Response(404));
}
}
16 changes: 16 additions & 0 deletions tests/InMemory/Client/Api/InMemoryAttributeApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api;

use Akeneo\Pim\ApiClient\Api\AttributeApiInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Attribute;

final class InMemoryAttributeApi extends InMemoryApi implements AttributeApiInterface
{
protected function getResourceClass(): string
{
return Attribute::class;
}
}
83 changes: 83 additions & 0 deletions tests/InMemory/Client/Api/InMemoryAttributeOptionApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api;

use Akeneo\Pim\ApiClient\Api\AttributeOptionApiInterface;
use Akeneo\Pim\ApiClient\Exception\NotFoundHttpException;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use GuzzleHttp\Promise\PromiseInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Http\Promise\Promise;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\AttributeOption;

final class InMemoryAttributeOptionApi implements AttributeOptionApiInterface
{
/**
* @var array<string, <string, AttributeOption>>
*/
public static array $attributeOptions = [];

public static function addResource(AttributeOption $attributeOption): void
{
self::$attributeOptions[$attributeOption->attribute][$attributeOption->code] = $attributeOption;
}

public function get($attributeCode, $code): array
{
if (!array_key_exists($attributeCode, self::$attributeOptions)) {
throw $this->createNotFoundException();
}
$attributeOptions = self::$attributeOptions[$attributeCode];
if (!array_key_exists($code, $attributeOptions)) {
throw $this->createNotFoundException();
}

return (array) $attributeOptions[$code];
}

public function create($attributeCode, $attributeOptionCode, array $data = []): int
{
self::$attributeOptions[] = AttributeOption::create($attributeCode, $attributeOptionCode, $data);

return 201;
}

public function listPerPage($attributeCode, $limit = 100, $withCount = false, array $queryParameters = []): PageInterface
{
// TODO: Implement listPerPage() method.
}

public function all($attributeCode, $pageSize = 10, array $queryParameters = []): ResourceCursorInterface
{
// TODO: Implement all() method.
}

public function upsert($attributeCode, $attributeOptionCode, array $data = []): int
{
// TODO: Implement upsert() method.
}

public function upsertAsync($attributeCode, $attributeOptionCode, array $data = []): PromiseInterface|Promise
{
// TODO: Implement upsertAsync() method.
}

public function upsertList($attributeCode, $attributeOptions): \Traversable
{
// TODO: Implement upsertList() method.
}

public function upsertAsyncList($attributeCode, $attributeOptions): PromiseInterface|Promise
{
// TODO: Implement upsertAsyncList() method.
}

private function createNotFoundException(): NotFoundHttpException
{
return new NotFoundHttpException('Attribute option not found', new Request('GET', '/'), new Response(404));
}
}
17 changes: 17 additions & 0 deletions tests/InMemory/Client/Api/InMemoryProductApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api;

use Akeneo\Pim\ApiClient\Api\ProductApiInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Product;

final class InMemoryProductApi extends InMemoryApi implements ProductApiInterface
{

protected function getResourceClass(): string
{
return Product::class;
}
}
Loading
Loading