Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
test: Product Relation Mapper (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
wfajczyk authored May 13, 2021
1 parent e2d163c commit ee50fa0
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Ergonode Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper;

use Ergonode\Channel\Domain\Entity\Export;
use Ergonode\Core\Domain\ValueObject\Language;
use Ergonode\ExporterShopware6\Domain\Entity\Shopware6Channel;
use Ergonode\ExporterShopware6\Infrastructure\Model\AbstractProductCrossSelling;
use Ergonode\Product\Domain\Entity\AbstractProduct;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

abstract class AbstractProductRelationAttributeCase extends TestCase
{
/**
* @var Shopware6Channel|MockObject
*/
protected Shopware6Channel $channel;

/**
* @var Export|MockObject
*/
protected Export $export;

/**
* @var AbstractProduct|MockObject
*/
protected AbstractProduct $product;

protected function setUp(): void
{
$this->channel = $this->createMock(Shopware6Channel::class);
$this->channel->method('getDefaultLanguage')
->willReturn(new Language('en_GB'));

$this->export = $this->createMock(Export::class);

$this->product = $this->createMock(AbstractProduct::class);
}

protected function getProductCrossSellingClass(): AbstractProductCrossSelling
{
return new class() extends AbstractProductCrossSelling {
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
/**
* Copyright © Ergonode Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\ProductRelationAttribute;

use Ergonode\ExporterShopware6\Domain\Repository\ProductRepositoryInterface;
use Ergonode\ExporterShopware6\Infrastructure\Mapper\ProductRelationAttribute\ChildrenMapper;
use Ergonode\ExporterShopware6\Infrastructure\Model\ProductCrossSelling\AbstractAssignedProduct;
use Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\AbstractProductRelationAttributeCase;
use Ergonode\Product\Domain\Entity\Attribute\ProductRelationAttribute;
use Ergonode\Product\Infrastructure\Calculator\TranslationInheritanceCalculator;
use Ergonode\Value\Domain\ValueObject\StringCollectionValue;
use PHPUnit\Framework\MockObject\MockObject;

class ChildrenMapperTest extends AbstractProductRelationAttributeCase
{
private const TEST_PRODUCT_ID = '0a6ef811-b1d7-4838-a435-bed88b81a951';
private const SHOPWARE_ID = 'shopware_id';

/**
* @var TranslationInheritanceCalculator|MockObject
*/
private TranslationInheritanceCalculator $calculator;

/**
* @var ProductRepositoryInterface|MockObject
*/
private ProductRepositoryInterface $shopware6ProductRepository;

protected function setUp(): void
{
parent::setUp();

$this->calculator = $this->createMock(TranslationInheritanceCalculator::class);
$this->shopware6ProductRepository = $this->createMock(ProductRepositoryInterface::class);
}

public function testNoAttribute(): void
{
$this->product->method('hasAttribute')->willReturn(false);
$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$productCrossSelling = $this->getProductCrossSellingClass();

$mapper = new ChildrenMapper(
$this->calculator,
$this->shopware6ProductRepository,
);
$new = $mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);

self::assertEmpty($new->getAssignedProducts());
}

public function testNoShopwareIdMapper(): void
{
$this->product->expects(self::once())->method('hasAttribute')->willReturn(true);
$this->product->expects(self::once())->method('getAttribute')
->willReturn($this->createMock(StringCollectionValue::class));

$this->calculator->expects(self::once())->method('calculate')->willReturn([self::TEST_PRODUCT_ID]);

$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$this->shopware6ProductRepository->method('load')->willReturn(null);

$productCrossSelling = $this->getProductCrossSellingClass();

$mapper = new ChildrenMapper(
$this->calculator,
$this->shopware6ProductRepository,
);

$new = $mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);

self::assertEmpty($new->getAssignedProducts());
}

public function testCorrectMapper(): void
{
$this->product->expects(self::once())->method('hasAttribute')->willReturn(true);
$this->product->expects(self::once())->method('getAttribute')
->willReturn($this->createMock(StringCollectionValue::class));

$this->calculator->expects(self::once())->method('calculate')->willReturn([ self::TEST_PRODUCT_ID]);

$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$this->shopware6ProductRepository->method('load')->willReturn(self::SHOPWARE_ID);

$productCrossSelling = $this->getProductCrossSellingClass();

$mapper = new ChildrenMapper(
$this->calculator,
$this->shopware6ProductRepository,
);

$new = $mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);
self::assertIsArray($new->getAssignedProducts());
self::assertNotEmpty($new->getAssignedProducts());
self::assertEquals(self::SHOPWARE_ID, $new->getAssignedProducts()[0]->getProductId());
self::assertEquals(1, $new->getAssignedProducts()[0]->getPosition());
}

public function testCorrectPositionMapper(): void
{
$this->product->expects(self::once())->method('hasAttribute')->willReturn(true);
$this->product->expects(self::once())->method('getAttribute')
->willReturn($this->createMock(StringCollectionValue::class));

$this->calculator->expects(self::once())->method('calculate')->willReturn([ self::TEST_PRODUCT_ID]);

$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$this->shopware6ProductRepository->method('load')->willReturn(self::SHOPWARE_ID);

$productCrossSelling = $this->getProductCrossSellingClass();
$assigned = $this->createMock(AbstractAssignedProduct::class);
$assigned->method('getPosition')->willReturn(10);
$productCrossSelling->setAssignedProducts([$assigned]);

$mapper = new ChildrenMapper(
$this->calculator,
$this->shopware6ProductRepository,
);

$new = $mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);
self::assertEquals(self::SHOPWARE_ID, $new->getAssignedProducts()[1]->getProductId());
self::assertEquals(11, $new->getAssignedProducts()[1]->getPosition());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Ergonode Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\ProductRelationAttribute;

use Ergonode\Attribute\Domain\ValueObject\AttributeCode;
use Ergonode\Core\Domain\ValueObject\TranslatableString;
use Ergonode\ExporterShopware6\Infrastructure\Mapper\ProductRelationAttribute\NameMapper;
use Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\AbstractProductRelationAttributeCase;
use Ergonode\Product\Domain\Entity\Attribute\ProductRelationAttribute;

class NameMapperTest extends AbstractProductRelationAttributeCase
{
private const TEST_NAME = 'Name of Attribute';
private const TEST_CODE = 'system_code';

private ProductRelationAttribute $relationAttribute;

protected function setUp(): void
{
parent::setUp();

$code = $this->createMock(AttributeCode::class);
$code->method('getValue')->willReturn(self::TEST_CODE);

$this->relationAttribute = $this->createMock(ProductRelationAttribute::class);
$this->relationAttribute->method('getCode')->willReturn($code);
}

public function testCorrectNameMapper(): void
{
$label = $this->createMock(TranslatableString::class);
$label->method('get')->willReturn(self::TEST_NAME);
$this->relationAttribute->method('getLabel')->willReturn($label);

$productCrossSelling = $this->getProductCrossSellingClass();

$mapper = new NameMapper();

$new = $mapper->map(
$this->channel,
$this->export,
$productCrossSelling,
$this->product,
$this->relationAttribute,
);

self::assertEquals(self::TEST_NAME, $new->getName());
}

public function testCorrectNameFromCodeMapper(): void
{
$label = $this->createMock(TranslatableString::class);
$label->method('get')->willReturn(null);
$this->relationAttribute->method('getLabel')->willReturn($label);

$productCrossSelling = $this->getProductCrossSellingClass();

$mapper = new NameMapper();

$new = $mapper->map(
$this->channel,
$this->export,
$productCrossSelling,
$this->product,
$this->relationAttribute,
);

self::assertEquals(self::TEST_CODE, $new->getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Copyright © Ergonode Sp. z o.o. All rights reserved.
* See LICENSE.txt for license details.
*/

declare(strict_types=1);

namespace Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\ProductRelationAttribute;

use Ergonode\ExporterShopware6\Domain\Repository\ProductRepositoryInterface;
use Ergonode\ExporterShopware6\Infrastructure\Exception\Mapper\Shopware6ExporterProductNoFoundException;
use Ergonode\ExporterShopware6\Infrastructure\Mapper\ProductRelationAttribute\RootProductIdMapper;
use Ergonode\ExporterShopware6\Tests\Infrastructure\Mapper\AbstractProductRelationAttributeCase;
use Ergonode\Product\Domain\Entity\Attribute\ProductRelationAttribute;
use Ergonode\Product\Domain\Query\ProductQueryInterface;
use PHPUnit\Framework\MockObject\MockObject;

class RootProductIdMapperTest extends AbstractProductRelationAttributeCase
{
/**
* @var ProductRepositoryInterface|MockObject
*/
private ProductRepositoryInterface $shopware6ProductRepository;
/**
* @var ProductQueryInterface|MockObject
*/
private ProductQueryInterface $productQuery;

protected function setUp(): void
{
parent::setUp();
$this->shopware6ProductRepository = $this->createMock(ProductRepositoryInterface::class);
$this->productQuery = $this->createMock(ProductQueryInterface::class);
}

public function testCorrectMapper(): void
{
$this->shopware6ProductRepository->method('load')->willReturn('sh_id');

$productCrossSelling = $this->getProductCrossSellingClass();

$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$mapper = new RootProductIdMapper(
$this->shopware6ProductRepository,
$this->productQuery,
);
$new = $mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);

self::assertEquals('sh_id', $new->getProductId());
}

public function testNoShopwareIdMapper(): void
{
$this->expectException(Shopware6ExporterProductNoFoundException::class);

$productCrossSelling = $this->getProductCrossSellingClass();

$relationAttribute = $this->createMock(ProductRelationAttribute::class);

$mapper = new RootProductIdMapper(
$this->shopware6ProductRepository,
$this->productQuery,
);
$mapper->map($this->channel, $this->export, $productCrossSelling, $this->product, $relationAttribute);
}
}

0 comments on commit ee50fa0

Please sign in to comment.