Skip to content

Commit

Permalink
Merge pull request #7265 from magento-l3/MC-39920
Browse files Browse the repository at this point in the history
MC-39920: Add support for parametrized data fixtures in integration and API functional tests​
  • Loading branch information
dhorytskyi authored Dec 24, 2021
2 parents d0dccd8 + 4ef1718 commit 302f6c5
Show file tree
Hide file tree
Showing 69 changed files with 4,150 additions and 416 deletions.
72 changes: 72 additions & 0 deletions app/code/Magento/Bundle/Test/Fixture/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Test\Fixture;

use Magento\Framework\DataObject;
use Magento\Framework\DataObjectFactory;
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
use Magento\TestFramework\Fixture\DataFixtureInterface;

class Link implements DataFixtureInterface
{
public const DEFAULT_DATA = [
'id' => null,
'sku' => null,
'option_id' => null,
'qty' => 1,
'position' => 1,
'is_default' => false,
'price' => null,
'price_type' => null,
'can_change_quantity' => 0
];

/**
* @var ProcessorInterface
*/
private $dataProcessor;

/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

/**
* @param ProcessorInterface $dataProcessor
* @param DataObjectFactory $dataObjectFactory
*/
public function __construct(
ProcessorInterface $dataProcessor,
DataObjectFactory $dataObjectFactory
) {
$this->dataProcessor = $dataProcessor;
$this->dataObjectFactory = $dataObjectFactory;
}

/**
* {@inheritdoc}
* @param array $data Parameters. Same format as Link::DEFAULT_DATA.
*/
public function apply(array $data = []): ?DataObject
{
return $this->dataObjectFactory->create(['data' => $this->prepareData($data)]);
}

/**
* Prepare link data
*
* @param array $data
* @return array
*/
private function prepareData(array $data): array
{
$data = array_merge(self::DEFAULT_DATA, $data);

return $this->dataProcessor->process($this, $data);
}
}
113 changes: 113 additions & 0 deletions app/code/Magento/Bundle/Test/Fixture/Option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Bundle\Test\Fixture;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\DataObjectFactory;
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
use Magento\TestFramework\Fixture\DataFixtureInterface;

class Option implements DataFixtureInterface
{
private const DEFAULT_DATA = [
'option_id' => null,
'title' => 'option%uniqid%',
'required' => true,
'type' => 'select',
'position' => 1,
'sku' => null,
'product_links' => []
];

/**
* @var ProcessorInterface
*/
private $dataProcessor;

/**
* @var DataObjectFactory
*/
private $dataObjectFactory;

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

/**
* @param ProcessorInterface $dataProcessor
* @param DataObjectFactory $dataObjectFactory
*/
public function __construct(
ProcessorInterface $dataProcessor,
DataObjectFactory $dataObjectFactory,
ProductRepositoryInterface $productRepository
) {
$this->dataProcessor = $dataProcessor;
$this->dataObjectFactory = $dataObjectFactory;
$this->productRepository = $productRepository;
}

/**
* {@inheritdoc}
* @param array $data Parameters. Same format as Option::DEFAULT_DATA.
* - $data['product_links']: An array of product IDs, SKUs or instances. For advanced configuration use an array
* like Link::DEFAULT_DATA.
*/
public function apply(array $data = []): ?DataObject
{
return $this->dataObjectFactory->create(['data' => $this->prepareData($data)]);
}

/**
* Prepare option data
*
* @param array $data
* @return array
*/
private function prepareData(array $data): array
{
$data = array_merge(self::DEFAULT_DATA, $data);
$data['product_links'] = $this->prepareLinksData($data);

return $this->dataProcessor->process($this, $data);
}

/**
* Prepare links data
*
* @param array $data
* @return array
*/
private function prepareLinksData(array $data): array
{
$links = [];

foreach ($data['product_links'] as $link) {
$linkData = [];
if (is_numeric($link)) {
$product = $this->productRepository->getById($link);
$linkData['sku'] = $product->getSku();
} elseif (is_string($link)) {
$linkData['sku'] = $link;
} elseif ($link instanceof ProductInterface) {
$product = $this->productRepository->get($link->getSku());
$linkData['sku'] = $product->getSku();
} else {
$linkData = $link instanceof DataObject ? $link->toArray() : $link;
}

$linkData += Link::DEFAULT_DATA;
$links[] = $linkData;
}

return $links;
}
}
70 changes: 70 additions & 0 deletions app/code/Magento/Bundle/Test/Fixture/Product.php
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\Bundle\Test\Fixture;

use Magento\Catalog\Model\Product\Type;
use Magento\Framework\DataObject;

class Product extends \Magento\Catalog\Test\Fixture\Product
{
private const DEFAULT_DATA = [
'id' => null,
'type_id' => Type::TYPE_BUNDLE,
'attribute_set_id' => 4,
'name' => 'Bundle Product%uniqid%',
'sku' => 'bundle-product%uniqid%',
'price' => null,
'weight' => null,
'custom_attributes' => [
'price_view' => '0',
'sku_type' => '0',
'price_type' => '0',
'weight_type' => '0',
'shipment_type' => '0',
],
'extension_attributes' => [
'bundle_product_options' => [],
]
];

/**
* {@inheritdoc}
* @param array $data Parameters. Same format as \Magento\Catalog\Test\Fixture\Product::DEFAULT_DATA.
* Custom attributes and extension attributes can be passed directly in the outer array instead of custom_attributes
* or extension_attributes.
* Additional fields:
* - $data['_options']: An array of options. See Magento\Bundle\Test\Fixture\Option
*/
public function apply(array $data = []): ?DataObject
{
return parent::apply($this->prepareData($data));
}

/**
* Prepare product data
*
* @param array $data
* @return array
*/
private function prepareData(array $data): array
{
$data = array_merge(self::DEFAULT_DATA, $data);

if (isset($data['_options'])) {
$data['extension_attributes']['bundle_product_options'] = array_map(
static function ($option) {
return $option instanceof DataObject ? $option->toArray() : $option;
},
$data['_options']
);
unset($data['_options']);
}

return $data;
}
}
Loading

0 comments on commit 302f6c5

Please sign in to comment.