Skip to content

Commit 302f6c5

Browse files
authored
Merge pull request #7265 from magento-l3/MC-39920
MC-39920: Add support for parametrized data fixtures in integration and API functional tests​
2 parents d0dccd8 + 4ef1718 commit 302f6c5

File tree

69 files changed

+4150
-416
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+4150
-416
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\Test\Fixture;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
13+
use Magento\TestFramework\Fixture\DataFixtureInterface;
14+
15+
class Link implements DataFixtureInterface
16+
{
17+
public const DEFAULT_DATA = [
18+
'id' => null,
19+
'sku' => null,
20+
'option_id' => null,
21+
'qty' => 1,
22+
'position' => 1,
23+
'is_default' => false,
24+
'price' => null,
25+
'price_type' => null,
26+
'can_change_quantity' => 0
27+
];
28+
29+
/**
30+
* @var ProcessorInterface
31+
*/
32+
private $dataProcessor;
33+
34+
/**
35+
* @var DataObjectFactory
36+
*/
37+
private $dataObjectFactory;
38+
39+
/**
40+
* @param ProcessorInterface $dataProcessor
41+
* @param DataObjectFactory $dataObjectFactory
42+
*/
43+
public function __construct(
44+
ProcessorInterface $dataProcessor,
45+
DataObjectFactory $dataObjectFactory
46+
) {
47+
$this->dataProcessor = $dataProcessor;
48+
$this->dataObjectFactory = $dataObjectFactory;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
* @param array $data Parameters. Same format as Link::DEFAULT_DATA.
54+
*/
55+
public function apply(array $data = []): ?DataObject
56+
{
57+
return $this->dataObjectFactory->create(['data' => $this->prepareData($data)]);
58+
}
59+
60+
/**
61+
* Prepare link data
62+
*
63+
* @param array $data
64+
* @return array
65+
*/
66+
private function prepareData(array $data): array
67+
{
68+
$data = array_merge(self::DEFAULT_DATA, $data);
69+
70+
return $this->dataProcessor->process($this, $data);
71+
}
72+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\Test\Fixture;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\DataObjectFactory;
14+
use Magento\TestFramework\Fixture\Data\ProcessorInterface;
15+
use Magento\TestFramework\Fixture\DataFixtureInterface;
16+
17+
class Option implements DataFixtureInterface
18+
{
19+
private const DEFAULT_DATA = [
20+
'option_id' => null,
21+
'title' => 'option%uniqid%',
22+
'required' => true,
23+
'type' => 'select',
24+
'position' => 1,
25+
'sku' => null,
26+
'product_links' => []
27+
];
28+
29+
/**
30+
* @var ProcessorInterface
31+
*/
32+
private $dataProcessor;
33+
34+
/**
35+
* @var DataObjectFactory
36+
*/
37+
private $dataObjectFactory;
38+
39+
/**
40+
* @var ProductRepositoryInterface
41+
*/
42+
private $productRepository;
43+
44+
/**
45+
* @param ProcessorInterface $dataProcessor
46+
* @param DataObjectFactory $dataObjectFactory
47+
*/
48+
public function __construct(
49+
ProcessorInterface $dataProcessor,
50+
DataObjectFactory $dataObjectFactory,
51+
ProductRepositoryInterface $productRepository
52+
) {
53+
$this->dataProcessor = $dataProcessor;
54+
$this->dataObjectFactory = $dataObjectFactory;
55+
$this->productRepository = $productRepository;
56+
}
57+
58+
/**
59+
* {@inheritdoc}
60+
* @param array $data Parameters. Same format as Option::DEFAULT_DATA.
61+
* - $data['product_links']: An array of product IDs, SKUs or instances. For advanced configuration use an array
62+
* like Link::DEFAULT_DATA.
63+
*/
64+
public function apply(array $data = []): ?DataObject
65+
{
66+
return $this->dataObjectFactory->create(['data' => $this->prepareData($data)]);
67+
}
68+
69+
/**
70+
* Prepare option data
71+
*
72+
* @param array $data
73+
* @return array
74+
*/
75+
private function prepareData(array $data): array
76+
{
77+
$data = array_merge(self::DEFAULT_DATA, $data);
78+
$data['product_links'] = $this->prepareLinksData($data);
79+
80+
return $this->dataProcessor->process($this, $data);
81+
}
82+
83+
/**
84+
* Prepare links data
85+
*
86+
* @param array $data
87+
* @return array
88+
*/
89+
private function prepareLinksData(array $data): array
90+
{
91+
$links = [];
92+
93+
foreach ($data['product_links'] as $link) {
94+
$linkData = [];
95+
if (is_numeric($link)) {
96+
$product = $this->productRepository->getById($link);
97+
$linkData['sku'] = $product->getSku();
98+
} elseif (is_string($link)) {
99+
$linkData['sku'] = $link;
100+
} elseif ($link instanceof ProductInterface) {
101+
$product = $this->productRepository->get($link->getSku());
102+
$linkData['sku'] = $product->getSku();
103+
} else {
104+
$linkData = $link instanceof DataObject ? $link->toArray() : $link;
105+
}
106+
107+
$linkData += Link::DEFAULT_DATA;
108+
$links[] = $linkData;
109+
}
110+
111+
return $links;
112+
}
113+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Bundle\Test\Fixture;
9+
10+
use Magento\Catalog\Model\Product\Type;
11+
use Magento\Framework\DataObject;
12+
13+
class Product extends \Magento\Catalog\Test\Fixture\Product
14+
{
15+
private const DEFAULT_DATA = [
16+
'id' => null,
17+
'type_id' => Type::TYPE_BUNDLE,
18+
'attribute_set_id' => 4,
19+
'name' => 'Bundle Product%uniqid%',
20+
'sku' => 'bundle-product%uniqid%',
21+
'price' => null,
22+
'weight' => null,
23+
'custom_attributes' => [
24+
'price_view' => '0',
25+
'sku_type' => '0',
26+
'price_type' => '0',
27+
'weight_type' => '0',
28+
'shipment_type' => '0',
29+
],
30+
'extension_attributes' => [
31+
'bundle_product_options' => [],
32+
]
33+
];
34+
35+
/**
36+
* {@inheritdoc}
37+
* @param array $data Parameters. Same format as \Magento\Catalog\Test\Fixture\Product::DEFAULT_DATA.
38+
* Custom attributes and extension attributes can be passed directly in the outer array instead of custom_attributes
39+
* or extension_attributes.
40+
* Additional fields:
41+
* - $data['_options']: An array of options. See Magento\Bundle\Test\Fixture\Option
42+
*/
43+
public function apply(array $data = []): ?DataObject
44+
{
45+
return parent::apply($this->prepareData($data));
46+
}
47+
48+
/**
49+
* Prepare product data
50+
*
51+
* @param array $data
52+
* @return array
53+
*/
54+
private function prepareData(array $data): array
55+
{
56+
$data = array_merge(self::DEFAULT_DATA, $data);
57+
58+
if (isset($data['_options'])) {
59+
$data['extension_attributes']['bundle_product_options'] = array_map(
60+
static function ($option) {
61+
return $option instanceof DataObject ? $option->toArray() : $option;
62+
},
63+
$data['_options']
64+
);
65+
unset($data['_options']);
66+
}
67+
68+
return $data;
69+
}
70+
}

0 commit comments

Comments
 (0)