Skip to content

Commit 1bdef0e

Browse files
committed
Proposal for reusable and customizable data fixtures
1 parent 4c7e673 commit 1bdef0e

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
## Background
2+
@magentoDataFixture takes an argument that points to the data fixture as a filename or local method.
3+
```php
4+
/**
5+
* @magentoDataFixture <script_filename>|<method_name>
6+
*/
7+
```
8+
## Problem
9+
When it comes to write a data fixture for your test case, the first thing you would do is to search for existing data fixture you can reuse in your test case. Most of the time you will find such data fixture that almost meets the requirements of your test case except that it's missing something very important to your test case.
10+
You will find a bunch of data fixture files that do practically the same and could be reduced to one single data fixture file.
11+
Here is a real example:
12+
13+
- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php
14+
- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php
15+
- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_out_of_stock.php
16+
17+
All these 3 files create a virtual product except one marks the product as out of stock and the other assigns different quantity to the product.
18+
If you need a virtual product with available quantity 1, here is what you can possibly do:
19+
20+
- Write a new data fixture (copy-past)
21+
- Reuse one of these data fixtures in a new data fixture file and edit the quantity there.
22+
- Reuse one of these data fixtures in your test case and edit the quantity in the test directly.
23+
24+
## Solution
25+
26+
There is certainly different approaches to address this issue including redesigning data fixtures. But the simplest or the one that requires less changes in the design would consist of making it possible to customize data fixtures using existing annotation @magentoDataFixture. We could extend the format of data fixture annotation
27+
to support parameter which will be injected to the data fixture file as following.
28+
```php
29+
/**
30+
* @magentoDataFixture Magento/Catalog/_files/product_virtual.php, {"price": 5, "stock_data": {"qty":1}}
31+
*/
32+
```
33+
34+
```php
35+
use Magento\Catalog\Api\Data\ProductInterface;
36+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
37+
use Magento\Catalog\Api\ProductRepositoryInterface;
38+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
39+
use Magento\Catalog\Model\Product\Visibility;
40+
use Magento\Framework\DataObject;
41+
use Magento\TestFramework\Helper\Bootstrap;
42+
43+
$productData = $productData ?? [];
44+
$defaultProductData = [
45+
'id' => 21,
46+
'sku' => 'virtual-product',
47+
'name' => 'Virtual Product',
48+
'attribute_set_id' => 'sku',
49+
'website_ids' => 'sku',
50+
'price' => 'sku',
51+
'visibility' => Visibility::VISIBILITY_BOTH,
52+
'status' => Status::STATUS_ENABLED,
53+
'stock_data' => Status::STATUS_ENABLED,
54+
];
55+
56+
$productData = array_merge($productData, $defaultProductData);
57+
$objectManager = Bootstrap::getObjectManager();
58+
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
59+
/** @var ProductRepositoryInterface $productResource */
60+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
61+
/** @var ProductInterface $product */
62+
$product = $productFactory->create();
63+
$product = data_object_hydrate_from_array($product, $productData);
64+
$productResource->save($product);
65+
66+
// this part of the code should be moved to a service.
67+
// something similar to \Magento\Framework\Webapi\ServiceInputProcessor::convertValue but less strict.
68+
69+
if (!function_exists('upper_came_case')) {
70+
function upper_came_case(string $value): string
71+
{
72+
return str_replace('_', '', ucwords($value, '_'));
73+
}
74+
}
75+
76+
if (!function_exists('data_object_hydrate_from_array')) {
77+
function data_object_hydrate_from_array(DataObject $object, array $data): DataObject
78+
{
79+
foreach ($data as $key => $value) {
80+
$camelCaseProperty = upper_came_case($key);
81+
$setterName = 'set' . $camelCaseProperty;
82+
$boolSetterName = 'setIs' . $camelCaseProperty;
83+
if (method_exists($object, $setterName)) {
84+
$object->$setterName($value);
85+
unset($data[$key]);
86+
} elseif (method_exists($object, $boolSetterName)) {
87+
$object->$boolSetterName($value);
88+
unset($data[$key]);
89+
}
90+
}
91+
$object->addData($data);
92+
return $object;
93+
}
94+
}
95+
```
96+
97+
The string after comma following the fixture file name is indeed the data that needs to be injected into the fixture file for customization. The format is well known JSON format that gives a flexible way to pass any type of data to the fixture (string, int, float and array).
98+
99+
100+
**Pros**
101+
- Reduces duplicate codes in data fixtures files
102+
- Reduces the number of data fixtures files
103+
104+
**Cons**
105+
- Increases dock block size

0 commit comments

Comments
 (0)