Skip to content

Commit

Permalink
Merge pull request #5068 from magento-tsg/2.3-develop-com-pr7
Browse files Browse the repository at this point in the history
[TSG-Commerce] Tests for 2.3 (pr7) (2.3-develop)
  • Loading branch information
zakdma authored Nov 29, 2019
2 parents c7ca62e + da284ab commit 9de4a7e
Show file tree
Hide file tree
Showing 15 changed files with 1,124 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Attribute\Save;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Eav\Api\AttributeRepositoryInterface;
use Magento\Eav\Model\Entity\Attribute\Exception;
use Magento\Framework\ObjectManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Base class for text product attributes
*/
abstract class AbstractAttributeTest extends TestCase
{
/** @var ObjectManagerInterface */
protected $objectManager;

/** @var AttributeRepositoryInterface */
protected $attributeRepository;

/** @var ProductRepositoryInterface */
protected $productRepository;

/** @var ProductAttributeInterface */
protected $attribute;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();

$this->objectManager = Bootstrap::getObjectManager();
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
$this->attributeRepository = $this->objectManager->create(AttributeRepositoryInterface::class);
}

/**
* @dataProvider productProvider
* @param $productSku
* @return void
*/
public function testSaveAttribute(string $productSku): void
{
$product = $this->setAttributeValueAndValidate($productSku, $this->getDefaultAttributeValue());
$product = $this->productRepository->save($product);
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
}

/**
* @dataProvider productProvider
* @param string $productSku
* @return void
*/
public function testRequiredAttribute(string $productSku): void
{
$this->expectException(Exception::class);
$messageFormat = 'The "%s" attribute value is empty. Set the attribute and try again.';
$this->expectExceptionMessage(
(string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel()))
);
$this->prepareAttribute(['is_required' => true]);
$this->unsetAttributeValueAndValidate($productSku);
}

/**
* @dataProvider productProvider
* @param string $productSku
* @return void
*/
public function testDefaultValue(string $productSku): void
{
$this->prepareAttribute(['default_value' => $this->getDefaultAttributeValue()]);
$product = $this->unsetAttributeValueAndValidate($productSku);
$product = $this->productRepository->save($product);
$this->assertEquals($this->getDefaultAttributeValue(), $product->getData($this->getAttributeCode()));
}

/**
* @dataProvider uniqueAttributeValueProvider
* @param string $firstSku
* @param string $secondSku
* @return void
*/
public function testUniqueAttribute(string $firstSku, string $secondSku): void
{
$this->expectException(Exception::class);
$messageFormat = 'The value of the "%s" attribute isn\'t unique. Set a unique value and try again.';
$this->expectExceptionMessage(
(string)__(sprintf($messageFormat, $this->getAttribute()->getDefaultFrontendLabel()))
);
$this->prepareAttribute(['is_unique' => 1]);
$product = $this->setAttributeValueAndValidate($firstSku, $this->getDefaultAttributeValue());
$this->productRepository->save($product);
$this->setAttributeValueAndValidate($secondSku, $this->getDefaultAttributeValue());
}

/**
* Get attribute
*
* @return ProductAttributeInterface
*/
protected function getAttribute(): ProductAttributeInterface
{
if ($this->attribute === null) {
$this->attribute = $this->attributeRepository->get(
ProductAttributeInterface::ENTITY_TYPE_CODE,
$this->getAttributeCode()
);
}

return $this->attribute;
}

/**
* Set attribute value to product and validate the product
*
* @param string $attributeValue
* @param string $productSku
* @return ProductInterface
*/
protected function setAttributeValueAndValidate(string $productSku, string $attributeValue): ProductInterface
{
$product = $this->productRepository->get($productSku);
$product->addData([$this->getAttributeCode() => $attributeValue]);
$product->validate();

return $product;
}

/**
* Unset attribute value of the product and validate the product
*
* @param string $productSku
* @return ProductInterface
*/
private function unsetAttributeValueAndValidate(string $productSku): ProductInterface
{
$product = $this->productRepository->get($productSku);
$product->unsetData($this->getAttributeCode());
$product->validate();

return $product;
}

/**
* Prepare attribute to test
*
* @param array $data
* @return void
*/
private function prepareAttribute(array $data): void
{
$attribute = $this->getAttribute();
$attribute->addData($data);
$this->attributeRepository->save($attribute);
}

/**
* Returns attribute code for current test
*
* @return string
*/
abstract protected function getAttributeCode(): string;

/**
* Get default value for current attribute
*
* @return string
*/
abstract protected function getDefaultAttributeValue(): string;

/**
* Products provider for tests
*
* @return array
*/
abstract public function productProvider(): array;

/**
* Provider for unique attribute tests
*
* @return array
*/
abstract public function uniqueAttributeValueProvider(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Attribute\Save;

/**
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
*/
class AttributeDateTest extends AbstractAttributeTest
{
/**
* @dataProvider productProvider
* @param string $productSku
*/
public function testDefaultValue(string $productSku): void
{
$this->markTestSkipped('Test is blocked by issue MC-28950');
}

/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'date_attribute';
}

/**
* @inheritdoc
*/
protected function getDefaultAttributeValue(): string
{
return $this->getAttribute()->getBackend()->formatDate('11/20/19');
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_date_attribute.php
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
* @dataProvider uniqueAttributeValueProvider
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
* @inheritdoc
*/
public function testUniqueAttribute(string $firstSku, string $secondSku): void
{
parent::testUniqueAttribute($firstSku, $secondSku);
}

/**
* @inheritdoc
*/
public function productProvider(): array
{
return [
[
'product_sku' => 'simple2',
],
];
}

/**
* @inheritdoc
*/
public function uniqueAttributeValueProvider(): array
{
return [
[
'first_product_sku' => 'simple2',
'second_product_sku' => 'simple-out-of-stock',
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Attribute\Save;

/**
* @magentoDbIsolation enabled
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
*/
class AttributeDropdownTest extends AbstractAttributeTest
{
/**
* @inheritdoc
*/
protected function getAttributeCode(): string
{
return 'dropdown_attribute';
}

/**
* @inheritdoc
*/
protected function getDefaultAttributeValue(): string
{
return $this->getAttribute()->getSource()->getOptionId('Option 1');
}

/**
* @magentoDataFixture Magento/Catalog/_files/dropdown_attribute.php
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
* @dataProvider uniqueAttributeValueProvider
* phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod
* @inheritdoc
*/
public function testUniqueAttribute(string $firstSku, string $secondSku): void
{
parent::testUniqueAttribute($firstSku, $secondSku);
}

/**
* @inheritdoc
*/
public function productProvider(): array
{
return [
[
'product_sku' => 'simple2',
],
];
}

/**
* @inheritdoc
*/
public function uniqueAttributeValueProvider(): array
{
return [
[
'first_product_sku' => 'simple2',
'second_product_sku' => 'simple-out-of-stock',
],
];
}
}
Loading

0 comments on commit 9de4a7e

Please sign in to comment.