4. Magento 2 is a huge step forward
● Dependency injection
5. Magento 2 is a huge step forward
● Dependency injection
● Composer
6. Magento 2 is a huge step forward
● Dependency injection
● Composer
● Automated tests
7. What is testing?
Software testing is the process of validating and verifying that a
software program or application or product works as expected.
http://istqbexamcertification.com/what-is-a-software-testing/
9. Automated testing vs manual testing
More info: http://www.base36.com/2013/03/automated-vs-manual-testing-the-pros-and-cons-of-each/
Automated testing Manual testing
Fast Slow
Costs less Costs more
Interesting Boring
Reusable
Improve code design
15. Have you ever?
● Installed a 3rd party module and it broke something else
on your project?
16. Have you ever?
● Installed a 3rd party module and it broke something else
on your project?
● Had a module that worked on local but not on production?
● Had 2 modules that have a conflict?
● Upgraded a project and it broke?
● Experienced problems with layout?
● Had an edge case?
19. Unit tests and integration tests difference
● Unit tests test the smallest units of code, integration tests test
how those units work together
● Integration tests touch more code
● Integration tests have less mocking and less isolation
● Unit tests are faster
● Unit tests are easier to debug
20. Functional tests and integration tests difference
● Functional tests compare against the specification
● Functional tests are slower
● Functional tests can tell us if something visible to the customer
broke
21. Integration tests in Magento 2
● Based on PHPUnit
● Can touch the database and filesystem
● Have separate database
● Deployed on close to real environment
● Separated into modules
● Magento core code is tested with integration tests
22. Setup
● bin/magento dev:test:run integration
● cd dev/tests/integration && phpunit -c phpunit.xml
● Separate database: dev/tests/integration/etc/install-config-mysql.php.dist
● Configuration in dev/tests/integration/phpunit.xml.dist
● TESTS_CLEANUP
● TESTS_MAGENTO_MODE
● TESTS_ERROR_LOG_LISTENER_LEVEL
24. Annotations
/**
* Test something
*
* @magentoConfigFixture currency/options/allow USD
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
public function testSomething()
{
}
25. What are annotations
● Meta data used to inject some behaviour
● Placed in docblocks
● Change test behaviour
● PHPUnit_Framework_TestListener
● onTestStart, onTestSuccess, onTestFailure, etc
More info: http://dusanlukic.com/annotations-in-magento-2-integration-tests
26. @magentoDbIsolation
● when enabled wraps the test in a transaction
● enabled - makes sure that the tests don’t affect each other
● disabled - useful for debugging as data remains in database
● can be applied on class level and on method level
/**
* @magentoDbIsolation enabled
*/
33. Test that non existing category goes to 404
class CategoryTest extends
MagentoTestFrameworkTestCaseAbstractController
{
/*...*/
public function testViewActionInactiveCategory()
{
$this->dispatch('catalog/category/view/id/8');
$this->assert404NotFound();
}
/*...*/
}
35. Real life example, Fooman_EmailAttachments
● Most of the stores have terms and agreement
● An email is sent to the customer after each successful order
● Goal: Attach terms and agreement to order success email
39. Template
<p>This should appear in catalog product view page!</p>
/app/code/LDusan/Sample/view/frontend/templates/sample.phtml
40. Test if block is added correctly
namespace LDusanSampleController;
class ActionTest extends MagentoTestFrameworkTestCaseAbstractController
{
/**
* @magentoDataFixture Magento/Catalog/_files/product_special_price.php
*/
public function testBlockAdded()
{
$this->dispatch('catalog/product/view/id/' . $product->getId());
$layout = MagentoTestFrameworkHelperBootstrap::getObjectManager()->get(
'MagentoFrameworkViewLayoutInterface'
);
$this->assertArrayHasKey('ldusan-sample-block', $layout->getAllBlocks());
}
}
41. At the end...
● Integration tests are not:
● The only tests that you should write
● Integration tests are:
● A way to check if something really works as expected
● Proof that our code works well with the environment