This page has moved and will be redirected soon. See Migrated topics for the complete list.

Application Area attribute

Configure a test environment in scope of the particular application area with the AppArea annotation.

Format

1
2
3
#[
    AppArea(string $area)
]

Parameters

  • area
    • Can take any value from the list below
      • \Magento\Framework\App\Area::AREA_GLOBAL
      • \Magento\Framework\App\Area::AREA_ADMINHTML
      • \Magento\Framework\App\Area::AREA_FRONTEND
      • \Magento\Framework\App\Area::AREA_WEBAPI_REST
      • \Magento\Framework\App\Area::AREA_WEBAPI_SOAP
      • \Magento\Framework\App\Area::AREA_CRONTAB
      • \Magento\Framework\App\Area::AREA_GRAPHQL

Fallback sequence

  1. Test method
  2. Test class
  3. Default application area, which is global

Test class attribute

A test class attribute enables the specified application area for all tests in the test class.

Test class attributes override test method attributes.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#[
    AppArea('adminhtml')
]
namespace Vendor\Module;
class ClassToTest extends \PHPUnit\Framework\TestCase
{
    public function testOne()
    {
        //...
    }

    #[
        AppArea('frontend')
    ]
    public function testTwo()
    {
        //...
    }

    public function testThree()
    {
        //...
    }
}

testOne() and testThree() are set to run in scope of the adminhtml application area, whereas testTwo() is set to run in scope of the frontend area.

Test method attribute

A test method attribute is used to configure the environment in scope of the specified application area for the test method. The application is reinitialized in the corresponding scope each time you specify a different area.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace \Vendor\Module;

class ClassToTest extends \PHPUnit\Framework\TestCase
{
    // executes the test in scope of the global area
    public function testOne()
    {
        //...
    }

    // reinitializes the application and executes the test in scope of the frontend area
    #[
        AppArea('frontend')
    ]
    public function testTwo()
    {
        //...
    }

    // reinitializes the application and executes the test in scope of the adminhtml area
    #[
        AppArea('adminhtml')
    ]
    public function testThree()
    {
        //...
    }

    // reinitializes the application and executes the test in scope of the global area
    public function testFour()
    {
        //...
    }

    // executes in scope of the global area
    public function testFive()
    {
        //...
    }
}