Magento 2.1.18 is the final 2.1.x release. After June 2019, Magento 2.1.x will no longer receive security patches, quality fixes, or documentation updates.
To maintain your site's performance, security, and PCI compliance, upgrade to the latest version of Magento.

Component registration annotation

When your test design requires you to register fixture components and unregister them after the test execution, use the @magentoComponentsDir annotation. This enables you to register all the components at a specified directory recursively.

Format

1
2
3
/**
* @magentoComponentsDir <dir_path>
*/

Here, <dir_path> is the path to the directory with fixture components. Each component must be registered using registration.php file.

Test case

@magentoComponentsDir annotation for a test case is applied to all test methods in the test case.

Test method

@magentoComponentsDir annotation for a test method configures the test to run with registered components located in a specified directory. If the parent test case also declares a @magentoComponentsDir, both annotations are merged.

Example

The following example demonstrates the @magentoComponentsDir annotation in different scopes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace Magento\Foo;

/**
 * @magentoComponentsDir Magento/Foo/_files/code/Magento
 */
class BarTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @magentoComponentsDir Magento/Foo/_files/themes
     */
    public function testOne()
    {
       ...   // Here you can use registered components from 'Magento/Foo/_files/code/Magento' and 'Magento/Foo/_files/themes'
    }

    /**
     * @magentoComponentsDir Magento/Foo/_files/libs
     * @magentoComponentsDir Magento/Baz/_files/languages
     */
    public function testTwo()
    {
       ...   // Here you can use the registered components from 'Magento/Foo/_files/code/Magento', 'Magento/Foo/_files/libs', and 'Magento/Baz/_files/languages'
    }
}

Each path declared in an annotation must contain a registration file in the specified directory or its subdirectories. For example, the MagentoFooTest_MyModule component at @magentoComponentsDir Magento/Foo/_files/code/Magento can be registered in Magento/Foo/_files/code/Magento/MyModule/registration.php:

1
2
3
4
5
6
7
8
use Magento\Framework\Component\ComponentRegistrar;

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MagentoFooTest_MyModule',
    __DIR__
);

A theme must be registered in the database. Each time you register a theme, reset the entire application. Use the @magentoDbIsolation annotation to keep the database integrity safe.

Updated