PHP 7.3 reaches end of support in December 2021 and Adobe Commerce 2.3.x reaches end of support in April 2022. You may want to consider planning your upgrade now to Adobe Commerce 2.4.x and PHP 7.4.x to help maintain PCI compliance.

Database isolation annotation

To isolate database changes between tests, the Integration testing framework (ITF) implements the @magentoDbIsolation annotation. When the @magentoDbIsolation is enabled, the ITF:

  • starts a database transaction before the test/test case.
  • avoids a database commit during the test/test case.
  • restores the database after the test/test case.

Format

Database isolation annotation:

1
2
3
/**
 * @magentoDbIsolation enabled|disabled
 */

Test case declaration

Use test case declarations when a sequence of tests introduces changes to the database and relies on the changes made by each other. For example, typical CRUD tests: create -> read -> update -> delete. Every next test relies on a database state after the previous one. If at any point of that sequence (after creation) the test fails, the database will be polluted with test data. To apply a database rollback after the test case, create a separate test case with @magentoDbIsolation enabled at a class level.

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
/**
 * @magentoDbIsolation enabled
 */
class Some\EntityTest extends \PHPUnit\Framework\TestCase
{
    public function testCreate()
    {
        $this->object->setData($this->initialData)->save();
        $this->assertNotEmpty($this->object->getId());
        return $this->object->getId();
    }

    public function testRead($objectId)
    {
        $this->object->load($objectId);
        $this->assertEquals($this->initialData, $this->object->getData());
        return $objectId;
    }

    public function testUpdate($objectId)
    {
        $this->object->setData($newData)->save();
        $object = new Some_Entity();
        $object->load($objectId);
        $this->assertEquals($newData, $object->getData());
        return $objectId;
    }

    public function testDelete($objectId)
    {
        $this->object->delete();
        $object = new Some_Entity();
        $object->load($objectId);
        $this->assertEmpty($object->getData());
    }
}

Test declaration

To isolate changes made to database by a single test, enable the @magentoDbIsolation for the test.

1
2
3
4
5
6
7
8
9
10
11
/**
* @magentoDbIsolation enabled
*/
public function testSave()
{
    $taxClassDataObject = $this->taxClassFactory->create();
    $taxClassDataObject->setClassName(self::SAMPLE_TAX_CLASS_NAME)
        ->setClassType(TaxClassManagementInterface::TYPE_CUSTOMER);
    $taxClassId = $this->taxClassRepository->save($taxClassDataObject);
    $this->assertEquals(self::SAMPLE_TAX_CLASS_NAME, $this->taxClassModel->load($taxClassId)->getClassName());
}