Introduce an alternative way to define fixtures using PHP8 Attributes #509
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.
Problem
Recently, we introduced parameterized fixture for integration and api-functional test that accepts parameters directly from @magentoDataFixture annotation.
We enhanced
@magentoDataFixture
annotation format to support additional information that contains the parameters and the alias of a fixture. And we introduced a new annotation @magentoDataFixtureDataProvider for advanced fixture configuration.However, with the current implementation, fixture parameters have to be passed in JSON format which is hard to read and maintain. PHP 8.0 introduced a new feature (Attributes) that we can utilize to generate fixtures instead of DocBlock annotations which will enable us to use PHP build in syntax and increase the code readability.
Example
Solution
PHP 8.0 introduced a new feature (Attributes) that we can utilize to generate fixtures instead of DocBlock annotations.
Implementation
Create PHP attribute equivalent for all existing DocBlock annotations used in integration and functional tests
@magentoAppIsolation
->AppIsolation
@magentoDbIsolation
->DbIsolation
@magentoDataFixtureBeforeTransaction
->DataFixtureBeforeTransaction
@magentoDataFixture
->DataFixture
(1)@magentoApiDataFixture
->DataFixture
(1)@magentoIndexerDimensionMode
->IndexerDimensionMode
@magentoComponentsDir
->ComponentsDir
@magentoAppArea
->AppArea
@magentoCache
->Cache
@magentoAdminConfigFixture
->Config
(2)@magentoConfigFixture
->Config
(2)(1) Both
magentoDataFixture
andmagentoApiDataFixture
can be replaced withDataFixture
attribute. Currently we usemagentoDataFixture
in integration tests andmagentoApiDataFixture
in functional api tests (WebAPI). Both executes data fixtures except thatmagentoApiDataFixture
does not trigger db transaction (db isolation) by default (without explicitly enabling db isolation with @magentoDbIsolation) whereasmagentoDataFixture
does.All it takes to make this work is to replace
\Magento\TestFramework\Annotation\DataFixture
with\Magento\TestFramework\Annotation\ApiDataFixture
in WebapiDocBlock and override\Magento\TestFramework\Annotation\AbstractDataFixture::getDbIsolationState
in\Magento\TestFramework\Annotation\ApiDataFixture
(ApiDataFixture extends DataFixture which also extends AbstractDataFixture) to return[disabled]
instead ofNULL
in case the db isolation is not explicitly defined. This will prevent the transaction from auto starting in \Magento\TestFramework\Annotation\DataFixture::startTestTransactionRequest unless explicitly enabled with@magentoDbIsolation
.(2) Both
magentoAdminConfigFixture
andmagentoConfigFixture
can be replaced withConfig
attribute.Example:
Pros
Cons
Thumbs up 👍 if you like this proposal