Skip to content

Commit

Permalink
Continue by fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Nov 7, 2023
1 parent fb01f0e commit 4b1310c
Show file tree
Hide file tree
Showing 15 changed files with 822 additions and 193 deletions.
10 changes: 5 additions & 5 deletions tests/InMemory/Client/Api/InMemoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function create(string $code, array $data = []): int
public function delete(string $code): int
{
if (!array_key_exists($code, self::$resources)) {
throw $this->createNotFoundException();
throw $this->createNotFoundException($code);
}
unset(self::$resources[$code]);

Expand Down Expand Up @@ -113,10 +113,10 @@ public function getPageSize(): ?int
public function get(string $code, array $queryParameters = []): array
{
if (!array_key_exists($code, self::$resources)) {
throw $this->createNotFoundException();
throw $this->createNotFoundException($code);
}

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

public function upsert(string $code, array $data = []): int
Expand All @@ -139,8 +139,8 @@ public function upsertAsyncList(StreamInterface|array $resources): PromiseInterf
// TODO: Implement upsertAsyncList() method.
}

private function createNotFoundException(): NotFoundHttpException
private function createNotFoundException(string $resource): NotFoundHttpException
{
return new NotFoundHttpException('Resource not found', new Request('GET', '/'), new Response(404));
return new NotFoundHttpException(sprintf('Resource %s not found', $resource), new Request('GET', '/'), new Response(404));
}
}
16 changes: 16 additions & 0 deletions tests/InMemory/Client/Api/InMemoryFamilyApi.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\FamilyApiInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\Family;

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

declare(strict_types=1);

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

use Akeneo\Pim\ApiClient\Api\FamilyVariantApiInterface;
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\FamilyVariant;

final class InMemoryFamilyVariantApi implements FamilyVariantApiInterface
{
/**
* @var array<string, <string, FamilyVariant>>
*/
public static array $familyVariants = [];

public static function addResource(string $familyCode, FamilyVariant $familyVariant): void
{
self::$familyVariants[$familyCode][$familyVariant->code] = $familyVariant;
}

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

return $familyVariants[$familyVariantCode]->__serialize();
}

public function create($familyCode, $familyVariantCode, array $data = []): int
{
// TODO: Implement create() method.
}

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

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

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

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

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

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

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

declare(strict_types=1);

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

use Akeneo\Pim\ApiClient\Api\MediaFileApiInterface;
use Akeneo\Pim\ApiClient\Exception\HttpException;
use Akeneo\Pim\ApiClient\Pagination\PageInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\Integration\DataFixtures\DataFixture;

final class InMemoryProductMediaFileApi implements MediaFileApiInterface
{
public function download(string $code): ResponseInterface
{
$path = DataFixture::path . '/Media/' . $code;
if (!file_exists($path)) {
throw new HttpException("File '$path' does not exists.", new Request('GET', '/'), new Response(404));
}

return new Response(200, [], file_get_contents($path));
}

public function get(string $code): array
{
// TODO: Implement get() method.
}

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
{
// TODO: Implement all() method.
}

public function create($mediaFile, array $data): string
{
// TODO: Implement create() method.
}
}
16 changes: 16 additions & 0 deletions tests/InMemory/Client/Api/InMemoryProductModelApi.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\ProductModelApiInterface;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\Model\ProductModel;

final class InMemoryProductModelApi extends InMemoryApi implements ProductModelApiInterface
{
protected function getResourceClass(): string
{
return ProductModel::class;
}
}
1 change: 1 addition & 0 deletions tests/InMemory/Client/Api/Model/AttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ final class AttributeType
{
public const TEXT = 'pim_catalog_text';
public const SIMPLE_SELECT = 'pim_catalog_simpleselect';
public const FILE = 'pim_catalog_file';
}
47 changes: 47 additions & 0 deletions tests/InMemory/Client/Api/Model/Family.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

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

final class Family implements ResourceInterface
{
public function __construct(
public string $code,
public array $attributes = [],
public string $attributeAsLabel = '',
public string $attributeAsImage = '',
public array $attributeRequirements = [],
public array $labels = [],
) {
}

public static function create(string $code, array $data = []): ResourceInterface
{
return new self(
$code,
$data['attributes'] ?? [],
$data['attribute_as_label'] ?? '',
$data['attribute_as_image'] ?? '',
$data['attribute_requirements'] ?? [],
$data['labels'] ?? [],
);
}

public function __serialize(): array
{
return [
'code' => $this->code,
'attributes' => $this->attributes,
'attribute_as_label' => $this->attributeAsLabel,
'attribute_as_image' => $this->attributeAsImage,
'attribute_requirements' => $this->attributeRequirements,
'labels' => $this->labels,
];
}

public function getIdentifier(): string
{
return $this->code;
}
}
38 changes: 38 additions & 0 deletions tests/InMemory/Client/Api/Model/FamilyVariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

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

final class FamilyVariant implements ResourceInterface
{
public function __construct(
public string $code,
public array $labels = [],
public array $variantAttributeSets = [],
) {
}

public static function create(string $code, array $data = []): ResourceInterface
{
return new self(
$code,
$data['labels'] ?? [],
$data['variant_attribute_sets'] ?? [],
);
}

public function __serialize(): array
{
return [
'code' => $this->code,
'labels' => $this->labels,
'variant_attribute_sets' => $this->variantAttributeSets,
];
}

public function getIdentifier(): string
{
return $this->code;
}
}
15 changes: 0 additions & 15 deletions tests/InMemory/Client/Api/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,4 @@ public function __serialize(): array
'quantified_associations' => $this->quantifiedAssociations,
];
}

public function __unserialize(array $data): void
{
$this->identifier = $data['identifier'];
$this->enabled = $data['enabled'];
$this->family = $data['family'];
$this->categories = $data['categories'];
$this->groups = $data['groups'];
$this->parent = $data['parent'];
$this->values = $data['values'];
$this->created = $data['created'];
$this->updated = $data['updated'];
$this->associations = $data['associations'];
$this->quantifiedAssociations = $data['quantified_associations'];
}
}
76 changes: 76 additions & 0 deletions tests/InMemory/Client/Api/Model/ProductModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

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

use DateTimeImmutable;
use DateTimeInterface;
use Webmozart\Assert\Assert;

final class ProductModel implements ResourceInterface
{
public DateTimeInterface $created;
public DateTimeInterface $updated;

/**
* @param string[] $categories
* @param array<string, array{'locale': ?string, 'scope': ?string, 'data': mixed}> $values
* @param array<string, array{'products': string[], 'product_models': string[], 'groups': string[]}> $associations
* @param array $quantifiedAssociations
*/
private function __construct(
public string $code,
public ?string $family = null,
public ?string $familyVariant = null,
public ?string $parent = null,
public array $categories = [],
public array $values = [],
public array $associations = [],
public array $quantifiedAssociations = [],
) {
$now = new DateTimeImmutable();
$this->created = $now;
$this->updated = $now;
}

public static function create(string $code, array $data = []): self
{
Assert::keyExists($data, 'family');
Assert::keyExists($data, 'family_variant');
$family = $data['family'];
$familyVariant = $data['family_variant'];

return new self(
$code,
$family,
$familyVariant,
$data['parent'] ?? null,
$data['categories'] ?? [],
$data['values'] ?? [],
$data['associations'] ?? [],
$data['quantifiedAssociations'] ?? [],
);
}

public function getIdentifier(): string
{
return $this->code;
}

public function __serialize(): array
{
return [
'code' => $this->code,
'family' => $this->family,
'family_variant' => $this->familyVariant,
'parent' => $this->parent,
'categories' => $this->categories,
'values' => $this->values,
'created' => $this->created->format('c'),
'updated' => $this->updated->format('c'),
'associations' => $this->associations,
'quantified_associations' => $this->quantifiedAssociations,
];
}
}
Loading

0 comments on commit 4b1310c

Please sign in to comment.