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

Cache attribute

Enable or disable a cache type using the Cache attribute.

Format

1
2
3
#[
   Cache(string $type, bool $status)
]

Parameters

  • type
    • The cache type. Or “all” to change the status of all cache types.
  • status
    • Accepts TRUE or FALSE to enable or disable cache respectively.

Principles

  1. You can use more than one attribute for a test case or a test method.
  2. Multiple attributes are applied in the given order.
  3. Attributes from different scopes are not merged.
  4. A test method attribute completely overrides a test class attribute.
  5. All cache types are disabled by default.

Test class attribute

The Cache attribute at the test case level is applied to all tests.

Test method attribute

The Cache attribute at a test method level configures the test method only. It completely overrides the attribute specified for the test class.

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
<?php

namespace Magento\Foo;

#[
   Cache('all', true)
]
class BarTest extends \PHPUnit\Framework\TestCase
{
    public function testOne()
    {
        ...
    }

    #[
       Cache('config', false)
    ]
    public function testTwo()
    {
        ...
    }

    #[
       Cache('all', true),
       Cache('config', false)
    ]
    public function testThree()
    {
        ...
    }

    #[
       Cache('config', false),
       Cache('all', true)
    ]
    public function testFour()
    {
        ...
    }
}
  • Each test method without the Cache attribute is run with all cache types enabled.
  • testOne() is run with all cache types enabled.
  • testTwo() is run with all cache types disabled.

    The Cache('config', false) completely overrides the test method attribute. The test method attribute wasn’t applied in this case. By default, all cache types are disabled, so disabling attributes does not make much sense here.

  • testThree() is run with all but config cache type enabled.
  • testFour() is run with all the cache types enabled.

    All cache types are disabled initially, so Cache('config', false) doesn’t make sense here.