-
Notifications
You must be signed in to change notification settings - Fork 151
Integration test data fixture change proposal #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bubasuma
wants to merge
6
commits into
magento:master
Choose a base branch
from
magento-mpi:integration_data_fixture_change_proposal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c702b96
Proposal for reusable and customizable data fixtures
bubasuma 81d9d5e
Proposal for reusable and customizable data fixtures
bubasuma ab892ec
Replace comma with space for consistency with other fixture annotations
bubasuma 4371ad8
Correct typo
bubasuma c16ef3d
Replace the json format with a new format that allows resolving depen…
bubasuma d7a7ee7
Fix grammar
bubasuma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| ## Problem | ||
| 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. | ||
| Therefore you end up writing a new data fixture for your test case that is 99% a copy of existing one. | ||
| Here is a real example: | ||
|
|
||
| - dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual.php | ||
| - dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_in_stock.php | ||
| - dev/tests/integration/testsuite/Magento/Catalog/_files/product_virtual_out_of_stock.php | ||
|
|
||
| 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. | ||
| So if you need a virtual product with available quantity 1, you will probably have to: | ||
|
|
||
| - Write a new data fixture (copy-past) | ||
| - Reuse one of these data fixtures in a new data fixture file and edit the quantity there. | ||
| - Reuse one of these data fixtures in your test case and edit the quantity in the test directly. | ||
|
|
||
| ## Solution | ||
|
|
||
| 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 | ||
| to support a second parameter which will be injected to the data fixture file as following. | ||
| ```php | ||
| /** | ||
| * @magentoDataFixture Magento/Catalog/_files/product_virtual.php, {"productData":{"stock_data": {"qty":1}}} | ||
| */ | ||
|
|
||
| ``` | ||
| 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). | ||
|
|
||
| ```php | ||
| use Magento\TestFramework\Catalog\Model\ProductFixtureFactory; | ||
| use Magento\TestFramework\Helper\Bootstrap; | ||
|
|
||
| $objectManager = Bootstrap::getObjectManager(); | ||
| $productFactory = $objectManager->get(ProductFixtureFactory::class); | ||
| $product = $productFactory->create($productData ?? []); | ||
| ``` | ||
|
|
||
| ```php | ||
| namespace Magento\TestFramework; | ||
|
|
||
| use Magento\Framework\DataObject; | ||
|
|
||
| class DataObjectHydrator | ||
| { | ||
| /** | ||
| * @param DataObject $object | ||
| * @param array $data | ||
| * @return DataObject | ||
| */ | ||
| public function hydrate(DataObject $object, array $data): DataObject | ||
| { | ||
| foreach ($data as $key => $value) { | ||
| $camelCaseProperty = str_replace('_', '', ucwords($key, '_')); | ||
| $setterName = 'set' . $camelCaseProperty; | ||
| $boolSetterName = 'setIs' . $camelCaseProperty; | ||
| if (method_exists($object, $setterName)) { | ||
| $object->$setterName($value); | ||
| unset($data[$key]); | ||
| } elseif (method_exists($object, $boolSetterName)) { | ||
| $object->$boolSetterName($value); | ||
| unset($data[$key]); | ||
| } | ||
| } | ||
| $object->addData($data); | ||
| return $object; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```php | ||
| namespace Magento\TestFramework\Catalog\Model; | ||
|
|
||
| use Magento\Catalog\Api\Data\ProductInterface; | ||
| use Magento\Catalog\Api\Data\ProductInterfaceFactory; | ||
| use Magento\Catalog\Api\ProductRepositoryInterface; | ||
| use Magento\Catalog\Model\Product\Attribute\Source\Status; | ||
| use Magento\Catalog\Model\Product\Type; | ||
| use Magento\Catalog\Model\Product\Visibility; | ||
| use Magento\TestFramework\DataObjectHydrator; | ||
|
|
||
| class ProductFixtureFactory | ||
| { | ||
| /** | ||
| * @var ProductInterfaceFactory | ||
| */ | ||
| private $factory; | ||
| /** | ||
| * @var ProductRepositoryInterface | ||
| */ | ||
| private $repository; | ||
| /** | ||
| * @var DataObjectHydrator | ||
| */ | ||
| private $dataObjectHydrator; | ||
|
|
||
| /** | ||
| * @param ProductInterfaceFactory $factory | ||
| * @param ProductRepositoryInterface $repository | ||
| * @param DataObjectHydrator $dataObjectHydrator | ||
| */ | ||
| public function __construct( | ||
| ProductInterfaceFactory $factory, | ||
| ProductRepositoryInterface $repository, | ||
| DataObjectHydrator $dataObjectHydrator | ||
| ) { | ||
| $this->factory = $factory; | ||
| $this->repository = $repository; | ||
| $this->dataObjectHydrator = $dataObjectHydrator; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $data | ||
| * @return ProductInterface | ||
| */ | ||
| public function create(array $data): ProductInterface | ||
| { | ||
| $product = $this->factory->create(); | ||
| $product = $this->dataObjectHydrator->hydrate($product, array_merge($this->defaultData(), $data)); | ||
| $this->repository->save($product); | ||
| return $product; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| private function defaultData(): array | ||
| { | ||
| return [ | ||
| 'id' => 21, | ||
| 'type_id' => Type::TYPE_VIRTUAL, | ||
| 'sku' => 'virtual-product', | ||
| 'name' => 'Virtual Product', | ||
| 'attribute_set_id' => 4, | ||
| 'tax_class_id' => 0, | ||
| 'website_ids' => [1], | ||
| 'price' => 10, | ||
| 'visibility' => Visibility::VISIBILITY_BOTH, | ||
| 'status' => Status::STATUS_ENABLED, | ||
| 'stock_data' => [ | ||
| 'qty' => 100, | ||
| 'is_in_stock' => 1, | ||
| 'manage_stock' => 1, | ||
| ], | ||
| ]; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| **Pros** | ||
| - Reduces duplicate codes in data fixtures files | ||
| - Reduces the number of data fixtures files | ||
|
|
||
| **Cons** | ||
| - Increases dock block size | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.