|
| 1 | +## Problem |
| 2 | +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. |
| 3 | +Therefore you end up writing a new data fixture for your test case that is 99% a copy of existing one. |
| 4 | +Here is a real example: |
| 5 | + |
| 6 | +- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php |
| 7 | +- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php |
| 8 | +- dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_out_of_stock.php |
| 9 | + |
| 10 | +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. |
| 11 | +So if you need a virtual product with available quantity 1, you will probably have to: |
| 12 | + |
| 13 | +- Write a new data fixture (copy-past) |
| 14 | +- Reuse one of these data fixtures in a new data fixture file and edit the quantity there. |
| 15 | +- Reuse one of these data fixtures in your test case and edit the quantity in the test directly. |
| 16 | + |
| 17 | +## Solution |
| 18 | + |
| 19 | +We could certainly redesign data fixtures to be object oriented. But if we just want to tackle this issue as simpler as we can, we could consist we could extend the format of data fixture annotation |
| 20 | +to support a second parameter which will be injected to the data fixture file as following. |
| 21 | +```php |
| 22 | +/** |
| 23 | + * @magentoDataFixture Magento/Catalog/_files/product_virtual.php, {"productData":{"stock_data": {"qty":1}}} |
| 24 | + */ |
| 25 | + |
| 26 | +``` |
| 27 | +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). |
| 28 | + |
| 29 | +```php |
| 30 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 31 | +use Magento\Catalog\Api\Data\ProductInterfaceFactory; |
| 32 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 33 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 34 | +use Magento\Catalog\Model\Product\Visibility; |
| 35 | +use Magento\Framework\DataObject; |
| 36 | +use Magento\TestFramework\Helper\Bootstrap; |
| 37 | + |
| 38 | +$productData = $productData ?? []; |
| 39 | +$defaultProductData = [ |
| 40 | + 'id' => 21, |
| 41 | + 'sku' => 'virtual-product', |
| 42 | + 'name' => 'Virtual Product', |
| 43 | + 'attribute_set_id' => 'sku', |
| 44 | + 'website_ids' => 'sku', |
| 45 | + 'price' => 'sku', |
| 46 | + 'visibility' => Visibility::VISIBILITY_BOTH, |
| 47 | + 'status' => Status::STATUS_ENABLED, |
| 48 | + 'stock_data' => Status::STATUS_ENABLED, |
| 49 | +]; |
| 50 | + |
| 51 | +$productData = array_merge($productData, $defaultProductData); |
| 52 | +$objectManager = Bootstrap::getObjectManager(); |
| 53 | +$productFactory = $objectManager->get(ProductInterfaceFactory::class); |
| 54 | +/** @var ProductRepositoryInterface $productResource */ |
| 55 | +$productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 56 | +/** @var ProductInterface $product */ |
| 57 | +$product = $productFactory->create(); |
| 58 | +$product = data_object_hydrate_from_array($product, $productData); |
| 59 | +$productResource->save($product); |
| 60 | + |
| 61 | +// this part of the code should be moved to a service. |
| 62 | +// something similar to \Magento\Framework\Webapi\ServiceInputProcessor::convertValue but less strict. |
| 63 | + |
| 64 | +if (!function_exists('upper_came_case')) { |
| 65 | + function upper_came_case(string $value): string |
| 66 | + { |
| 67 | + return str_replace('_', '', ucwords($value, '_')); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +if (!function_exists('data_object_hydrate_from_array')) { |
| 72 | + function data_object_hydrate_from_array(DataObject $object, array $data): DataObject |
| 73 | + { |
| 74 | + foreach ($data as $key => $value) { |
| 75 | + $camelCaseProperty = upper_came_case($key); |
| 76 | + $setterName = 'set' . $camelCaseProperty; |
| 77 | + $boolSetterName = 'setIs' . $camelCaseProperty; |
| 78 | + if (method_exists($object, $setterName)) { |
| 79 | + $object->$setterName($value); |
| 80 | + unset($data[$key]); |
| 81 | + } elseif (method_exists($object, $boolSetterName)) { |
| 82 | + $object->$boolSetterName($value); |
| 83 | + unset($data[$key]); |
| 84 | + } |
| 85 | + } |
| 86 | + $object->addData($data); |
| 87 | + return $object; |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | +**Pros** |
| 92 | +- Reduces duplicate codes in data fixtures files |
| 93 | +- Reduces the number of data fixtures files |
| 94 | + |
| 95 | +**Cons** |
| 96 | +- Increases dock block size |
0 commit comments