Magento – Magento 2 integration tests load data fixtures before config fixtures

integration-testmagento-2.0magento2

Let's say I have a method in my integration test with annotations like this:

/**
 * @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
 * @magentoConfigFixture secondstore_store some/config/field 1
**/

When I run the test it fails with:

PHP Fatal error: Uncaught exception
'Magento\Framework\Exception\NoSuchEntityException' with message
'Requested store is not found' in
/var/www/html/magento2/app/code/Magento/Store/Model/StoreRepository.php:60

I think that this is because config fixtures are loaded before data fixtures in dev/tests/integration/framework/Magento/TestFramework/Bootstrap/DocBlock.php and in this case we need data fixture to create the store.

Is there a way to load config fixtures after data fixtures so that stores would be available?

Best Answer

This looks to be impossible for now using annotations mechanism. Even if data fixture is declared on class level it will be executed for every test after config fixture.

The following approach works:

<?php
namespace VendorName\ModuleName\Model;

class SomeTest extends \PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        require realpath(TESTS_TEMP_DIR . '/../testsuite/Magento/Store/_files/core_fixturestore.php');
        parent::setUpBeforeClass();
    }

    public static function tearDownAfterClass()
    {
        /** Teardown is necessary because this fixture was committed to DB in setUpBeforeClass */
        require realpath(TESTS_TEMP_DIR . '/../testsuite/Magento/Store/_files/core_fixturestore_rollback.php');
        parent::setUpBeforeClass();
    }

    /**
     * @magentoConfigFixture fixturestore some/config/field 1
     **/
    public function testSomething()
    {
        /** Test content */
    }
}

Such approach also makes sense from performance standpoint, because store creation is pretty heavy operation, and now it will be done only once per test case.

Related Topic