init
This commit is contained in:
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Setup\Fixtures\FixturesAsserts;
|
||||
|
||||
use Magento\Setup\Fixtures\BundleProductsFixture;
|
||||
|
||||
/**
|
||||
* Class BundleProductsAssert
|
||||
*
|
||||
* Class performs assertion that generated bundle products are valid
|
||||
* after running setup:performance:generate-fixtures command
|
||||
*/
|
||||
class BundleProductsAssert
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Api\ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Bundle\Model\Product\OptionList
|
||||
*/
|
||||
private $optionList;
|
||||
|
||||
/**
|
||||
* @var \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert
|
||||
*/
|
||||
private $productAssert;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
|
||||
* @param \Magento\Bundle\Model\Product\OptionList $optionList
|
||||
* @param \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
|
||||
\Magento\Bundle\Model\Product\OptionList $optionList,
|
||||
\Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->optionList = $optionList;
|
||||
$this->productAssert = $productAssert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that generated bundled products are valid
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assert()
|
||||
{
|
||||
$bundleSkuSuffix = '2-2';
|
||||
$product = $this->productRepository->get(
|
||||
sprintf(BundleProductsFixture::SKU_PATTERN, 1, $bundleSkuSuffix)
|
||||
);
|
||||
|
||||
$this->productAssert->assertProductsCount(
|
||||
sprintf(BundleProductsFixture::SKU_PATTERN, '%s', $bundleSkuSuffix),
|
||||
2
|
||||
);
|
||||
$this->productAssert->assertProductType('bundle', $product);
|
||||
|
||||
if (2 !== count($this->optionList->getItems($product))) {
|
||||
throw new \AssertionError('Bundle options amount is wrong');
|
||||
}
|
||||
|
||||
foreach ($this->optionList->getItems($product) as $option) {
|
||||
if (2 !== count($option->getProductLinks())) {
|
||||
throw new \AssertionError('Bundle option product links amount is wrong');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Setup\Fixtures\FixturesAsserts;
|
||||
|
||||
/**
|
||||
* Class ConfigurableProductsAssert
|
||||
*
|
||||
* Class performs assertion that generated configurable products are valid
|
||||
* after running setup:performance:generate-fixtures command
|
||||
*/
|
||||
class ConfigurableProductsAssert
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Api\ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\ConfigurableProduct\Api\OptionRepositoryInterface
|
||||
*/
|
||||
private $optionRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert
|
||||
*/
|
||||
private $productAssert;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
|
||||
* @param \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository
|
||||
* @param \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
|
||||
\Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository,
|
||||
\Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->optionRepository = $optionRepository;
|
||||
$this->productAssert = $productAssert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that generated configurable products are valid
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
* @throws \Magento\Framework\Exception\InputException
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assert()
|
||||
{
|
||||
$productsMap = [
|
||||
'Configurable Product - Default %s' => [
|
||||
'attributes' => 1,
|
||||
'options' => 3,
|
||||
'amount' => 2,
|
||||
],
|
||||
'Configurable Product - Color-Size %s' => [
|
||||
'attributes' => 2,
|
||||
'options' => 3,
|
||||
'amount' => 2,
|
||||
],
|
||||
'Configurable Product 2-2 %s' => [
|
||||
'attributes' => 2,
|
||||
'options' => 2,
|
||||
'amount' => 2,
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($productsMap as $skuPattern => $expectedData) {
|
||||
$configurableSku = sprintf($skuPattern, 1);
|
||||
$product = $this->productRepository->get($configurableSku);
|
||||
$this->productAssert->assertProductsCount($skuPattern, $expectedData['amount']);
|
||||
$this->productAssert->assertProductType('configurable', $product);
|
||||
$options = $this->optionRepository->getList($configurableSku);
|
||||
|
||||
if ($expectedData['attributes'] !== count($options)) {
|
||||
throw new \AssertionError('Configurable options amount is wrong');
|
||||
}
|
||||
|
||||
if ($expectedData['options'] !== count($options[0]->getValues())) {
|
||||
throw new \AssertionError('Configurable option values amount is wrong');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+159
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Magento\Setup\Fixtures\FixturesAsserts;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
/**
|
||||
* Class ImagesAssert
|
||||
*
|
||||
* Class performs assertions to check that generated images are valid
|
||||
* after running setup:performance:generate-fixtures command
|
||||
*/
|
||||
class ImagesAssert
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Api\ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\Product\Gallery\ReadHandler
|
||||
*/
|
||||
private $readHandler;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem
|
||||
*/
|
||||
private $filesystem;
|
||||
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\Product\Media\Config
|
||||
*/
|
||||
private $mediaConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
|
||||
*/
|
||||
private $mediaDirectory;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
|
||||
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
|
||||
* @param \Magento\Catalog\Model\Product\Gallery\ReadHandler $readHandler
|
||||
* @param \Magento\Framework\Filesystem $filesystem
|
||||
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
|
||||
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
|
||||
\Magento\Catalog\Model\Product\Gallery\ReadHandler $readHandler,
|
||||
\Magento\Framework\Filesystem $filesystem,
|
||||
\Magento\Catalog\Model\Product\Media\Config $mediaConfig
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
|
||||
$this->readHandler = $readHandler;
|
||||
$this->filesystem = $filesystem;
|
||||
$this->mediaConfig = $mediaConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertions over images
|
||||
*
|
||||
* @return bool
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assert()
|
||||
{
|
||||
$searchCriteria = $this->searchCriteriaBuilder->create();
|
||||
$products = $this->productRepository->getList($searchCriteria)->getItems();
|
||||
|
||||
foreach ($products as $product) {
|
||||
$this->assertProductMediaGallery($product);
|
||||
$this->assertProductMediaAttributes($product);
|
||||
$this->assertProductImageExistsInFS($product);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertions over media_gallery product attribute
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Product $product
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
private function assertProductMediaGallery(\Magento\Catalog\Model\Product $product)
|
||||
{
|
||||
$extendedProduct = $this->readHandler->execute($product);
|
||||
$mediaGalleryImages = $extendedProduct->getMediaGalleryEntries();
|
||||
|
||||
if (count($mediaGalleryImages) !== 1) {
|
||||
throw new \AssertionError('Product supposed to contain one image');
|
||||
}
|
||||
|
||||
$image = reset($mediaGalleryImages);
|
||||
|
||||
if ($image->getFile() === null) {
|
||||
throw new \AssertionError('Image path should not be null');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertions over product media attributes
|
||||
* e.g. image|small_image|swatch_image|thumbnail
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Product $product
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
private function assertProductMediaAttributes(\Magento\Catalog\Model\Product $product)
|
||||
{
|
||||
foreach ($product->getMediaAttributeValues() as $attributeCode => $attributeValue) {
|
||||
if (empty($attributeValue)) {
|
||||
throw new \AssertionError(
|
||||
sprintf('Attribute: %s should not be empty', $attributeCode)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertions over image files in FS
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Product $product
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
private function assertProductImageExistsInFS(\Magento\Catalog\Model\Product $product)
|
||||
{
|
||||
$mediaDirectory = $this->getMediaDirectory();
|
||||
$mediaAttributes = $product->getMediaAttributeValues();
|
||||
|
||||
if (!$mediaDirectory->isExist($this->mediaConfig->getBaseMediaPath() . $mediaAttributes['image'])) {
|
||||
throw new \AssertionError('Image file for product supposed to exist');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Local cache for $mediaDirectory
|
||||
*
|
||||
* @return \Magento\Framework\Filesystem\Directory\ReadInterface
|
||||
*/
|
||||
private function getMediaDirectory()
|
||||
{
|
||||
if ($this->mediaDirectory === null) {
|
||||
$this->mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
|
||||
}
|
||||
|
||||
return $this->mediaDirectory;
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Setup\Fixtures\FixturesAsserts;
|
||||
|
||||
/**
|
||||
* Class ProductAssert
|
||||
*
|
||||
* Class provides assertions about products count and products type
|
||||
* that helps validate them after running setup:performance:generate-fixtures command
|
||||
*/
|
||||
class ProductAssert
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Model\ResourceModel\Product\
|
||||
*/
|
||||
protected $productCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\DB\Sql\ColumnValueExpressionFactory
|
||||
*/
|
||||
protected $expressionFactory;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
|
||||
* @param \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
|
||||
\Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory
|
||||
) {
|
||||
$this->productCollectionFactory = $productCollectionFactory;
|
||||
$this->expressionFactory = $expressionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertion on generated products count
|
||||
* Accepts products sku pattern to calculate count and theirs expected count
|
||||
*
|
||||
* @param string $skuPattern
|
||||
* @param int $expectedCount
|
||||
* @return void
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assertProductsCount($skuPattern, $expectedCount)
|
||||
{
|
||||
$productSkuPattern = str_replace('%s', '[0-9]+', $skuPattern);
|
||||
$productCollection = $this->productCollectionFactory->create();
|
||||
$productCollection
|
||||
->getSelect()
|
||||
->where('sku ?', $this->expressionFactory->create([
|
||||
'expression' => 'REGEXP \'^' . $productSkuPattern . '$\''
|
||||
]));
|
||||
|
||||
if ($expectedCount !== count($productCollection)) {
|
||||
throw new \AssertionError(
|
||||
sprintf(
|
||||
'Expected amount of products with sku pattern "%s" not equals actual amount',
|
||||
$skuPattern
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs assertion that product has expected product type
|
||||
*
|
||||
* @param string $expectedProductType
|
||||
* @param \Magento\Catalog\Api\Data\ProductInterface $product
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assertProductType($expectedProductType, $product)
|
||||
{
|
||||
if ($expectedProductType !== $product->getTypeId()) {
|
||||
throw new \AssertionError('Product type is wrong');
|
||||
}
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
namespace Magento\Setup\Fixtures\FixturesAsserts;
|
||||
|
||||
use Magento\Setup\Fixtures\SimpleProductsFixture;
|
||||
|
||||
/**
|
||||
* Class SimpleProductsAssert
|
||||
*
|
||||
* Class performs assertion that generated simple products are valid
|
||||
* after running setup:performance:generate-fixtures command
|
||||
*/
|
||||
class SimpleProductsAssert
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Catalog\Api\ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\ConfigurableProduct\Api\OptionRepositoryInterface
|
||||
*/
|
||||
private $optionRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert
|
||||
*/
|
||||
private $productAssert;
|
||||
|
||||
/**
|
||||
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
|
||||
* @param \Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository
|
||||
* @param \Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
|
||||
\Magento\ConfigurableProduct\Api\OptionRepositoryInterface $optionRepository,
|
||||
\Magento\Setup\Fixtures\FixturesAsserts\ProductAssert $productAssert
|
||||
) {
|
||||
$this->productRepository = $productRepository;
|
||||
$this->optionRepository = $optionRepository;
|
||||
$this->productAssert = $productAssert;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that generated simple products are valid
|
||||
*
|
||||
* @return bool
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
* @throws \AssertionError
|
||||
*/
|
||||
public function assert()
|
||||
{
|
||||
$product = $this->productRepository->get(sprintf(SimpleProductsFixture::SKU_PATTERN, 1));
|
||||
$this->productAssert->assertProductsCount(SimpleProductsFixture::SKU_PATTERN, 2);
|
||||
$this->productAssert->assertProductType('simple', $product);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user