Vitalyi Golomoziy - Integration tests in Magento 2

21

Transcript of Vitalyi Golomoziy - Integration tests in Magento 2

Page 1: Vitalyi Golomoziy - Integration tests in Magento 2
Page 2: Vitalyi Golomoziy - Integration tests in Magento 2

ABOUT MYSELF

• Architect and SME for Magento U

• Working in Magento for more than 8 years

Page 3: Vitalyi Golomoziy - Integration tests in Magento 2

TABLE OF CONTENTS

• Automated testing in Magento 2

• Integration tests framework overview

• Writing a simple integration test

Page 4: Vitalyi Golomoziy - Integration tests in Magento 2

AUTOMATED TESTING IN MAGENTO 2

Page 5: Vitalyi Golomoziy - Integration tests in Magento 2

QUALITY OF THE AUTOMATED TEST

FragilityAccuracy

Savings Costs

Page 6: Vitalyi Golomoziy - Integration tests in Magento 2

AUTOMATED TESTING IN MAGENTO 2

• Unit testing

• Integration testing

• Functional testing

Page 7: Vitalyi Golomoziy - Integration tests in Magento 2

UNIT TEST

• TDD

• Better code structure

• Execution of every line of the

code

Fragility Accuracy

Page 8: Vitalyi Golomoziy - Integration tests in Magento 2

FUNCTIONAL TEST

• Business scenarios testing

• Ensuring the whole system is

working as expected

• Edge case in user’s behavior

testing

Accuracy Fragility

Page 9: Vitalyi Golomoziy - Integration tests in Magento 2

INTEGRATION TEST

• Code-level feature testing

• Testing feature connected to the

infrastructure

Accuracy Fragility

Page 10: Vitalyi Golomoziy - Integration tests in Magento 2

INTEGRATION TESTS FRAMEWORK OVERVIEW

Page 11: Vitalyi Golomoziy - Integration tests in Magento 2

FRAMEWORK

• Based on phpunit

• Provides magento-specific annotations

• Provides a lot of magento-specific tools

• Special object manager object

Page 12: Vitalyi Golomoziy - Integration tests in Magento 2

CONFIGURATION

• etc/install-config-mysql.php

• etc/config-global.php

• etc/di/preferences/(ce.php|ee.php)

• phpunit.xml

Page 13: Vitalyi Golomoziy - Integration tests in Magento 2

USEFUL ANNOTATIONS

• @magentoDataFixture

• @magentoAppIsolation

• @magentoDbIsolation

• @magentoAppArea

• @magentoCache

• @magentoConfigFixture

Page 14: Vitalyi Golomoziy - Integration tests in Magento 2

MAGENTO TEST CASES

• AbstractController

• AbstractBackendController

• AbstractConfigFile

• AbstractIntegrity

Page 15: Vitalyi Golomoziy - Integration tests in Magento 2

ADD SHARED INSTANCE

\PHPUnit_Framework_MockObject_MockObject $transportBuilderMock,

$originalClassName

) {

$userMock = $this->_objectManager->create(

$originalClassName,

['transportBuilder' => $transportBuilderMock]

);

$factoryMock = $this->getMockBuilder('Magento\User\Model\UserFactory')

->disableOriginalConstructor()

->setMethods(['create'])

->getMock();

$factoryMock->method('create')

->willReturn($userMock);

$this->_objectManager->addSharedInstance(

$factoryMock,

'Magento\User\Model\UserFactory'

);

}

Page 16: Vitalyi Golomoziy - Integration tests in Magento 2

WRITING A SIMPLE INTEGRATION TEST

Page 17: Vitalyi Golomoziy - Integration tests in Magento 2

CONFIGURATION

return [

'db-host' => 'localhost',

'db-user' => ’__database_user__',

'db-password' => ’__password__',

'db-name' => ’special_db_for_integration_tests',

'db-prefix' => '',

'backend-frontname' => 'admin',

'admin-user' => \Magento\TestFramework\Bootstrap::ADMIN_NAME,

'admin-password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,

'admin-email' => \Magento\TestFramework\Bootstrap::ADMIN_EMAIL,

'admin-firstname' => \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME,

'admin-lastname' => \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME,

];

Page 18: Vitalyi Golomoziy - Integration tests in Magento 2

FIXTURES

<?php

global $students;

$students = [

[’name' => ’John’, ‘surname’ => ‘Doe’],

[’name' => ’Another John’, ‘surname’ => ‘Doe’],

];

Page 19: Vitalyi Golomoziy - Integration tests in Magento 2

TEST

/**

* @magentoDataFixture __path_to_fixture_file.php

* @magentoDbIsolation enabled

* @magentoAppIsolation enabled

* @magentoAppArea adminhtml

*/

public function testCRUD()

{

global $students;

$model = $this->studentsFactory>create();

$model->setName($students[0][’name’])

->setSurname($students[0][‘surname’]);

$crud = new \Magento\TestFramework\Entity($model);

$crud->testCrud();

}

Page 20: Vitalyi Golomoziy - Integration tests in Magento 2

Q&A

Page 21: Vitalyi Golomoziy - Integration tests in Magento 2

THANK YOU FOR ATTENTION