Skip to content

Commit

Permalink
Fix unit tests (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Dec 13, 2023
1 parent 85cf924 commit 6b7cb64
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 39 deletions.
2 changes: 2 additions & 0 deletions tests/InMemory/Client/Api/InMemoryApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ abstract class InMemoryApi implements
*/
abstract public function getResources(): array;

abstract public static function clear(): void;

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

Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryAttributeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected function getResourceClass(): string
return Attribute::class;
}

public static function clear(): void
{
self::$attributes = [];
}

public function getResources(): array
{
return self::$attributes;
Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryAttributeOptionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public static function addResource(AttributeOption $attributeOption): void
self::$attributeOptions[$attributeOption->attribute][$attributeOption->code] = $attributeOption;
}

public static function clear(): void
{
self::$attributeOptions = [];
}

public function get($attributeCode, $code): array
{
if (!array_key_exists($attributeCode, self::$attributeOptions)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryFamilyApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected function getResourceClass(): string
return Family::class;
}

public static function clear(): void
{
self::$families = [];
}

public function getResources(): array
{
return self::$families;
Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryFamilyVariantApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static function addResource(string $familyCode, FamilyVariant $familyVari
self::$familyVariants[$familyCode][$familyVariant->code] = $familyVariant;
}

public static function clear(): void
{
self::$familyVariants = [];
}

public function get($familyCode, $familyVariantCode): array
{
if (!array_key_exists($familyCode, self::$familyVariants)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryProductApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function getResources(): array
return self::$products;
}

public static function clear(): void
{
self::$products = [];
}

protected function getResourceClass(): string
{
return Product::class;
Expand Down
5 changes: 5 additions & 0 deletions tests/InMemory/Client/Api/InMemoryProductModelApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ protected function getResourceClass(): string
return ProductModel::class;
}

public static function clear(): void
{
self::$productModels = [];
}

public function getResources(): array
{
return self::$productModels;
Expand Down
6 changes: 6 additions & 0 deletions tests/Integration/AttributeOptions/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ protected function setUp(): void
$this->attributeRepository = self::getContainer()->get('sylius.repository.product_attribute');
$this->optionRepository = self::getContainer()->get('sylius.repository.product_option');

/**
* @TODO: Move this methods to a generic class on some events on PHPUnit?
*/
InMemoryAttributeApi::clear();
InMemoryAttributeOptionApi::clear();

InMemoryAttributeApi::addResource(new Attribute('material', AttributeType::SIMPLE_SELECT));
InMemoryAttributeApi::addResource(new Attribute('text_attribute', AttributeType::TEXT));
InMemoryAttributeApi::addResource(Attribute::create('size', [
Expand Down
10 changes: 10 additions & 0 deletions tests/Integration/Product/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ protected function setUp(): void
$this->fixtureLoader = self::getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
$this->filesystem = self::getContainer()->get('filesystem');

/**
* @TODO: Move this methods to a generic class on some events on PHPUnit?
*/
InMemoryFamilyApi::clear();
InMemoryAttributeApi::clear();
InMemoryAttributeOptionApi::clear();
InMemoryFamilyVariantApi::clear();
InMemoryProductModelApi::clear();
InMemoryProductApi::clear();

$this->tShirtFamily = Family::create('t-shirt', [
'attributes' => ['variation_image', 'supplier'],
]);
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/ProductAssociations/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ protected function setUp(): void
$this->productRepository = self::getContainer()->get('sylius.repository.product');
$this->fixtureLoader = self::getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');

/**
* @TODO: Move this methods to a generic class on some events on PHPUnit?
*/
InMemoryProductApi::clear();

InMemoryProductApi::addResource(Product::create(self::MUG_DW_PRODUCT_CODE, [
'family' => 'mugs',
'parent' => null,
Expand Down
65 changes: 26 additions & 39 deletions tests/Integration/ProductModel/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
namespace Tests\Webgriffe\SyliusAkeneoPlugin\Integration\ProductModel;

use DateTime;
use Fidry\AliceDataFixtures\Loader\PurgerLoader;
use Fidry\AliceDataFixtures\Persistence\PurgeMode;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Tests\Webgriffe\SyliusAkeneoPlugin\DataFixtures\DataFixture;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\InMemoryAttributeApi;
use Tests\Webgriffe\SyliusAkeneoPlugin\InMemory\Client\Api\InMemoryAttributeOptionApi;
Expand Down Expand Up @@ -44,37 +42,31 @@ final class ImporterTest extends KernelTestCase

private ChannelRepositoryInterface $channelRepository;

private PurgerLoader $fixtureLoader;

private Filesystem $filesystem;

private Family $tShirtFamily;

private AttributeOption $sizeLAttributeOption;

private ProductModel $starWarsTShirtProductModel;

private Product $startWarsTShirtMAkeneoProduct;

protected function setUp(): void
{
self::bootKernel();
$this->importer = self::getContainer()->get('webgriffe_sylius_akeneo.product_model.importer');
$this->productRepository = self::getContainer()->get('sylius.repository.product');
$this->productVariantRepository = self::getContainer()->get('sylius.repository.product_variant');
$this->channelRepository = self::getContainer()->get('sylius.repository.channel');
$this->fixtureLoader = self::getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
$this->filesystem = self::getContainer()->get('filesystem');

$this->tShirtFamily = Family::create('t-shirt', [
$fixtureLoader = self::getContainer()->get('fidry_alice_data_fixtures.loader.doctrine');
$filesystem = self::getContainer()->get('filesystem');

/**
* @TODO: Move this methods to a generic class on some events on PHPUnit?
*/
InMemoryFamilyApi::clear();
InMemoryAttributeApi::clear();
InMemoryAttributeOptionApi::clear();
InMemoryFamilyVariantApi::clear();
InMemoryProductModelApi::clear();
InMemoryProductApi::clear();

$tShirtFamily = Family::create('t-shirt', [
'attributes' => ['variation_image', 'supplier'],
]);
InMemoryFamilyApi::addResource($this->tShirtFamily);
InMemoryFamilyApi::addResource($tShirtFamily);

$attachmentAttribute = Attribute::create('attachment', [
'type' => AttributeType::FILE,
]);
InMemoryAttributeApi::addResource($attachmentAttribute);
$skuAttribute = Attribute::create('sku', [
'type' => AttributeType::TEXT,
'labels' => ['en_US' => 'SKU'],
Expand All @@ -91,11 +83,6 @@ protected function setUp(): void
]);
InMemoryAttributeOptionApi::addResource($sizeMAttributeOption);

$this->sizeLAttributeOption = AttributeOption::create($this->sizeAttribute->code, 'l', 0, [
'en_US' => 'L', 'it_IT' => 'L',
]);
InMemoryAttributeOptionApi::addResource($this->sizeLAttributeOption);

$tShirtBySizeFamilyVariant = FamilyVariant::create('t-shirt_by_size', [
'variant_attribute_sets' => [
[
Expand All @@ -105,17 +92,17 @@ protected function setUp(): void
],
],
]);
InMemoryFamilyVariantApi::addResource($this->tShirtFamily->code, $tShirtBySizeFamilyVariant);
InMemoryFamilyVariantApi::addResource($tShirtFamily->code, $tShirtBySizeFamilyVariant);

$this->starWarsTShirtProductModel = ProductModel::create(self::STAR_WARS_TSHIRT_MODEL_CODE, [
'family' => $this->tShirtFamily->code,
$starWarsTShirtProductModel = ProductModel::create(self::STAR_WARS_TSHIRT_MODEL_CODE, [
'family' => $tShirtFamily->code,
'family_variant' => $tShirtBySizeFamilyVariant->code,
]);
InMemoryProductModelApi::addResource($this->starWarsTShirtProductModel);
InMemoryProductModelApi::addResource($starWarsTShirtProductModel);

$this->startWarsTShirtMAkeneoProduct = Product::create(self::STAR_WARS_TSHIRT_M_PRODUCT_CODE, [
'family' => $this->tShirtFamily->code,
'parent' => $this->starWarsTShirtProductModel->code,
$startWarsTShirtMAkeneoProduct = Product::create(self::STAR_WARS_TSHIRT_M_PRODUCT_CODE, [
'family' => $tShirtFamily->code,
'parent' => $starWarsTShirtProductModel->code,
'values' => [
$this->sizeAttribute->code => [
[
Expand All @@ -126,21 +113,21 @@ protected function setUp(): void
],
],
]);
InMemoryProductApi::addResource($this->startWarsTShirtMAkeneoProduct);
InMemoryProductApi::addResource($startWarsTShirtMAkeneoProduct);

$ORMResourceFixturePath = DataFixture::path . '/ORM/resources/Importer/ProductModel/' . $this->getName() . '.yaml';
if (file_exists($ORMResourceFixturePath)) {
$this->fixtureLoader->load(
$fixtureLoader->load(
[$ORMResourceFixturePath],
[],
[],
PurgeMode::createDeleteMode(),
);
} else {
$this->fixtureLoader->load([], [], [], PurgeMode::createDeleteMode());
$fixtureLoader->load([], [], [], PurgeMode::createDeleteMode());
}

$this->filesystem->remove(
$filesystem->remove(
self::getContainer()->getParameter('sylius_core.public_dir') . '/media/',
);
}
Expand Down

0 comments on commit 6b7cb64

Please sign in to comment.