6

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?

  • Can you please post your test? – Maddy Dec 15 '15 at 19:47
  • @Maddy I don't think it matters because the error appears before any line of the actual test code is executed. Anything can be in the test and it will show the same error. – LDusan Dec 16 '15 at 6:48
6

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.

  • Thanks that sounded like a nice solution but unfortunately it didn't work for me. – LDusan Dec 16 '15 at 19:26
  • @LDusan see updated response – Alex Paliarush Dec 17 '15 at 10:01
  • I assume the function tearDownAfterClass should use the parent call tearDownAfterClass. But thanks, I have now the rollback feature working – Herve Tribouilloy Nov 12 '18 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.