Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
GETTING YOUR HANDS DIRTY
TESTING
MAGENTO 2
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 -...
ME
Freelance Developer & Trainer
https://github.com/vinai
https://twitter.com/vinaikopp
Dev since 1998
PHP WebDev since PH...
TESTING
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitte...
WHO IS DOING IT?
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com...
WHAT TOOL?
> PHPUnit?
> Behat && PHPSpec?
> Selenium?
> Codeception?
Getting your hands dirty Testing Magento 2 - Magento ...
BUZZWORD
BINGO
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com -...
BUZZWORD I:
UNIT TEST
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikop...
$ vendor/bin/phpunit -c "dev/tests/unit/phpunit.xml.dist"
Getting your hands dirty Testing Magento 2 - Magento London Meet...
$ alias unit-test="$(pwd)/vendor/bin/phpunit
-c $(pwd)/dev/tests/unit/phpunit.xml.dist"
Getting your hands dirty Testing M...
$ unit-test
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - tw...
$ unit-test "app/code/My/Module/Test/Unit"
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 -...
TRY IT
vendor/bin/phpunit 
-c "dev/tests/unit/phpunit.xml.dist" 
vendor/magento/module-catalog/Test/Unit/Block
Getting you...
BUZZWORD II:
INTEGRATION
TEST
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@...
WHAT IS THE
DIFFERENCE?
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaik...
REAL OBJECTS INTERACT
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikop...
INTEGRATION TESTS NEED THE
RUNTIME ENVIRONMENT
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 20...
A failing unit test tells me
exactly where my code is broken.
Getting your hands dirty Testing Magento 2 - Magento London ...
A failing integration test only
tells me that something is broken.
Getting your hands dirty Testing Magento 2 - Magento Lo...
Separate DB
dev/tests/integration/etc/install-config-mysql.php.dist
Getting your hands dirty Testing Magento 2 - Magento L...
$ vendor/bin/phpunit -c dev/tests/integration/phpunit.xml
Could not read "dev/tests/integration/phpunit.xml".
Getting your...
$ cd dev/tests/integration
$ ../../../vendor/bin/phpunit
Getting your hands dirty Testing Magento 2 - Magento London Meetu...
Every run gets it's own
etc, pub and var dirs
dev/tests/integration/tmp/
sandbox-0-b79c13eb842cf3211459b11c775bfbde/
Getti...
BUZZWORD III:
TDD
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.co...
WHAT IS TDD?
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - t...
THE 3 RULES
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - tw...
I. No production code without
a failing unit test
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb....
II. Once there is a failing unit test,
stop and make it pass
by writing production code
Getting your hands dirty Testing M...
III. Once the test passes,
stop writing production code
until you have a failing unit test again
Getting your hands dirty ...
TDD FOR MAGENTO 2
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.co...
NO DIFFERENCE
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - ...
</TDD>
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter...
To learn more about TDD,
check out the Clean Coders
Videos from Robert C. Martin
http://cleancoders.com
(or do a training)...
INTEGRATION TESTS
FOR MAGENTO 2
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contac...
VERY SPECIFIC
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - ...
PLATFORM KNOWLEDGE
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.c...
Magento 2 Testing Framework
dev/tests/integration/framework
Getting your hands dirty Testing Magento 2 - Magento London Me...
Test Framework ObjectManager
MagentoTestFrameworkObjectManager::getInstance()
Getting your hands dirty Testing Magento 2 -...
Dependencies
$configReader = $objectManager->create(Reader::class);
$deploymentConfig = $objectManager->create(
Deployment...
BUZZWORD IV
FIXTURES
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp...
Fix the system into a known state
before a test is executed.
Getting your hands dirty Testing Magento 2 - Magento London M...
Simple PHP Scripts
Relative to
dev/tests/integration/testsuite
Getting your hands dirty Testing Magento 2 - Magento London...
/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
pub...
CONFIG FIXTURES
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com ...
Store Scope
/**
* @magentoConfigFixture current_store catalog/price/scope 1
*/
public function testDoesSomeoneReadThisHelp...
Global Scope
/**
* @magentoConfigFixture currency/options/allow USD
*/
public function testHowMashAndPieTastes()
{
// ...
...
BUZZWORD V
TEST ISOLATION
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vina...
FRESH INSTALL
FOR EACH TEST RUN
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contac...
dev/tests/integration/phpunit.xml.dist
<phpunit>
...
<php>
...
<const name="TESTS_CLEANUP" value="enabled"/>
...
</php>
.....
TESTS_CLEANUP
== disabled
SPEEDS THINGS UP
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 -...
TESTS_CLEANUP
== disabled
MANUAL CLEANUP REQUIRED
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb....
integration/tmp/sandbox-*
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vina...
DATABASE ISOLATION
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.c...
TRANSACTIONS
OR
ROLLBACK SCRIPTS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - conta...
TRANSACTIONS
/**
* @magentoDbIsolation enabled
*/
public function testWithoutSideEffects
{
// ...
Getting your hands dirty...
TRANSACTIONS
/**
* @magentoDataFixture Magento/Foo/_files/bar.php
*/
public function testTheFixtureIsAppliedWithinATransac...
ROLLBACK SCRIPTS
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDbIsolation disabled
*/
public f...
Before Test
include 'Magento/Customer/_files/customer.php';
Getting your hands dirty Testing Magento 2 - Magento London Me...
After Test
include 'Magento/Customer/_files/customer_rollback.php';
Getting your hands dirty Testing Magento 2 - Magento L...
APPLICATION ISOLATION
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikop...
REINSTANTIATE
ALMOST EVERYTHING
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contac...
RESET MAGENTO AFTER TEST
/**
* @magentoAppIsolation enabled
*/
public function testTestWithSingletons()
{
Getting your han...
Controller Tests: Automatic reset
class FooBarIndexActionTest extends
MagentoTestFrameworkTestCaseAbstractController
{
pub...
REMEMBER
THIS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - ...
ISOLATE
YOUR TESTS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.c...
AND ALL WILL BE
GOOD
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp...
[breathing pause]
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.co...
APPLICATION AREA
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com...
APP AREAS
global
frontend
adminhtml
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - co...
/**
* @magentoAppArea frontend
*/
class AddressesTest extends PHPUnit_Framework_TestCase
{
Getting your hands dirty Testin...
MORE AREAS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twi...
webapi_rest
webapi_soap
cron
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@v...
MagentoTestFrameworkAppState
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@v...
public function testPluginInWebapiRestScope()
{
$class = MagentoTestFrameworkAppState::class;
$appAreaState = $this->objec...
protected function tearDown()
{
// ...
$appAreaState->setAreaCode(null);
}
Getting your hands dirty Testing Magento 2 - Ma...
ACTION
[CONTROLLER]
TESTS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vina...
FRONTEND
MagentoTestFrameworkTestCase
AbstractController
ADMINHTML
MagentoTestFrameworkTestCase
AbstractBackendController
...
EXAMPLE!
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitt...
Event Observer for:
sales_order_place_after
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 ...
I. TESTING THE
CONFIGURATION
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@v...
public function testTheModuleRegistersASalesOrderPlaceAfterObserver()
{
$class = MagentoFrameworkEventConfig::class;
$even...
1/2) Get the config in array format
$class = MagentoFrameworkEventConfig::class;
$eventConfig = ObjectManager::getInstance...
2/2) Assert config values
$this->assertArrayHasKey('meetup_observer', $observers);
$this->assertSame(
SalesOrderPlaceAfter...
II. TESTING THE OBSERVER
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinai...
O_0(NOTHING TO TEST)
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp...
III. INTEGRATION TEST
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikop...
Our observer has no side effects.
We have to improvise...
Getting your hands dirty Testing Magento 2 - Magento London Meet...
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testSalesOrderPlaceAfterObserverIsCalled()
{
$...
public function injectMockObserverWithExpectation()
{
$mockObserver = $this->getMock(SalesOrderPlaceAfter::class);
$mockOb...
private function registerObserverTestDouble($mockObserver)
{
$class = MagentoFrameworkEventConfigData::class;
$eventConfig...
private function registerObserverTestDouble($mockObserver)
{
// ...
$this->objectManager->addSharedInstance(
$mockObserver...
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testSalesOrderPlaceAfterObserverIsCalled()
{
/...
private function getOrderFixture()
{
$class = OrderRepositoryInterface::class;
$orderRepository = $this->objectManager->cr...
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testSalesOrderPlaceAfterObserverIsCalled()
{
$...
GO FORTH
AND TESTALL THINGS!
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@v...
ONE MORE THING...
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.co...
MAGE2KATAS
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twi...
MAGE2KATAS
> Screencast Series
> Examples
> Code Katas
mage2katas.com
Getting your hands dirty Testing Magento 2 - Magento...
THANK YOU <3
Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - t...
Upcoming SlideShare
Loading in …5
×

Getting your hands dirty testing Magento 2 (at MageTitansIT)

6,245 views

Published on

Introduction into automated testing in Magento 2 with a focus on integration tests.
The presentation was held at the MageTitans conference in Milano, Italy on 2016-02-05.
I re-uploaded a newer version of the slide deck that contains more details.

Published in: Software
  • Be the first to comment

Getting your hands dirty testing Magento 2 (at MageTitansIT)

  1. 1. GETTING YOUR HANDS DIRTY TESTING MAGENTO 2 Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  2. 2. ME Freelance Developer & Trainer https://github.com/vinai https://twitter.com/vinaikopp Dev since 1998 PHP WebDev since PHP3 (1999) Magento since 2008-01-01 Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  3. 3. TESTING Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  4. 4. WHO IS DOING IT? Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  5. 5. WHAT TOOL? > PHPUnit? > Behat && PHPSpec? > Selenium? > Codeception? Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  6. 6. BUZZWORD BINGO Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  7. 7. BUZZWORD I: UNIT TEST Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  8. 8. $ vendor/bin/phpunit -c "dev/tests/unit/phpunit.xml.dist" Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  9. 9. $ alias unit-test="$(pwd)/vendor/bin/phpunit -c $(pwd)/dev/tests/unit/phpunit.xml.dist" Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  10. 10. $ unit-test Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  11. 11. $ unit-test "app/code/My/Module/Test/Unit" Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  12. 12. TRY IT vendor/bin/phpunit -c "dev/tests/unit/phpunit.xml.dist" vendor/magento/module-catalog/Test/Unit/Block Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  13. 13. BUZZWORD II: INTEGRATION TEST Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  14. 14. WHAT IS THE DIFFERENCE? Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  15. 15. REAL OBJECTS INTERACT Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  16. 16. INTEGRATION TESTS NEED THE RUNTIME ENVIRONMENT Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  17. 17. A failing unit test tells me exactly where my code is broken. Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  18. 18. A failing integration test only tells me that something is broken. Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  19. 19. Separate DB dev/tests/integration/etc/install-config-mysql.php.dist Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  20. 20. $ vendor/bin/phpunit -c dev/tests/integration/phpunit.xml Could not read "dev/tests/integration/phpunit.xml". Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  21. 21. $ cd dev/tests/integration $ ../../../vendor/bin/phpunit Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  22. 22. Every run gets it's own etc, pub and var dirs dev/tests/integration/tmp/ sandbox-0-b79c13eb842cf3211459b11c775bfbde/ Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  23. 23. BUZZWORD III: TDD Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  24. 24. WHAT IS TDD? Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  25. 25. THE 3 RULES Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  26. 26. I. No production code without a failing unit test Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  27. 27. II. Once there is a failing unit test, stop and make it pass by writing production code Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  28. 28. III. Once the test passes, stop writing production code until you have a failing unit test again Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  29. 29. TDD FOR MAGENTO 2 Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  30. 30. NO DIFFERENCE Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  31. 31. </TDD> Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  32. 32. To learn more about TDD, check out the Clean Coders Videos from Robert C. Martin http://cleancoders.com (or do a training) Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  33. 33. INTEGRATION TESTS FOR MAGENTO 2 Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  34. 34. VERY SPECIFIC Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  35. 35. PLATFORM KNOWLEDGE Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  36. 36. Magento 2 Testing Framework dev/tests/integration/framework Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  37. 37. Test Framework ObjectManager MagentoTestFrameworkObjectManager::getInstance() Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  38. 38. Dependencies $configReader = $objectManager->create(Reader::class); $deploymentConfig = $objectManager->create( DeploymentConfig::class, ['reader' => $configReader] ); Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  39. 39. BUZZWORD IV FIXTURES Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  40. 40. Fix the system into a known state before a test is executed. Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  41. 41. Simple PHP Scripts Relative to dev/tests/integration/testsuite Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  42. 42. /** * @magentoDataFixture Magento/Sales/_files/order.php * @magentoDataFixture Magento/Customer/_files/customer.php */ public function testSomethingWithAnOrderAndACustomer() { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  43. 43. CONFIG FIXTURES Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  44. 44. Store Scope /** * @magentoConfigFixture current_store catalog/price/scope 1 */ public function testDoesSomeoneReadThisHelpMeLetMeOut() { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  45. 45. Global Scope /** * @magentoConfigFixture currency/options/allow USD */ public function testHowMashAndPieTastes() { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  46. 46. BUZZWORD V TEST ISOLATION Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  47. 47. FRESH INSTALL FOR EACH TEST RUN Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  48. 48. dev/tests/integration/phpunit.xml.dist <phpunit> ... <php> ... <const name="TESTS_CLEANUP" value="enabled"/> ... </php> ... </phpunit> Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  49. 49. TESTS_CLEANUP == disabled SPEEDS THINGS UP Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  50. 50. TESTS_CLEANUP == disabled MANUAL CLEANUP REQUIRED Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  51. 51. integration/tmp/sandbox-* Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  52. 52. DATABASE ISOLATION Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  53. 53. TRANSACTIONS OR ROLLBACK SCRIPTS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  54. 54. TRANSACTIONS /** * @magentoDbIsolation enabled */ public function testWithoutSideEffects { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  55. 55. TRANSACTIONS /** * @magentoDataFixture Magento/Foo/_files/bar.php */ public function testTheFixtureIsAppliedWithinATransaction { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  56. 56. ROLLBACK SCRIPTS /** * @magentoDataFixture Magento/Customer/_files/customer.php * @magentoDbIsolation disabled */ public function testTransactionalCode() { // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  57. 57. Before Test include 'Magento/Customer/_files/customer.php'; Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  58. 58. After Test include 'Magento/Customer/_files/customer_rollback.php'; Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  59. 59. APPLICATION ISOLATION Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  60. 60. REINSTANTIATE ALMOST EVERYTHING Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  61. 61. RESET MAGENTO AFTER TEST /** * @magentoAppIsolation enabled */ public function testTestWithSingletons() { Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  62. 62. Controller Tests: Automatic reset class FooBarIndexActionTest extends MagentoTestFrameworkTestCaseAbstractController { public function testTheLayoutIsRendered() { $this->dispatch('foo/bar/index'); Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  63. 63. REMEMBER THIS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  64. 64. ISOLATE YOUR TESTS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  65. 65. AND ALL WILL BE GOOD Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  66. 66. [breathing pause] Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  67. 67. APPLICATION AREA Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  68. 68. APP AREAS global frontend adminhtml Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  69. 69. /** * @magentoAppArea frontend */ class AddressesTest extends PHPUnit_Framework_TestCase { Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  70. 70. MORE AREAS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  71. 71. webapi_rest webapi_soap cron Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  72. 72. MagentoTestFrameworkAppState Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  73. 73. public function testPluginInWebapiRestScope() { $class = MagentoTestFrameworkAppState::class; $appAreaState = $this->objectManager->get($class); $appAreaState->setAreaCode('webapi_rest'); Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  74. 74. protected function tearDown() { // ... $appAreaState->setAreaCode(null); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  75. 75. ACTION [CONTROLLER] TESTS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  76. 76. FRONTEND MagentoTestFrameworkTestCase AbstractController ADMINHTML MagentoTestFrameworkTestCase AbstractBackendController Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  77. 77. EXAMPLE! Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  78. 78. Event Observer for: sales_order_place_after Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  79. 79. I. TESTING THE CONFIGURATION Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  80. 80. public function testTheModuleRegistersASalesOrderPlaceAfterObserver() { $class = MagentoFrameworkEventConfig::class; $eventConfig = ObjectManager::getInstance()->create($class); $observers = $eventConfig->getObservers('sales_order_place_after'); $this->assertArrayHasKey('meetup_observer', $observers); $expectedClass = MeetupObserverObserverSalesOrderPlaceAfter::class; $this->assertSame($expectedClass, $observers['meetup_observer']['instance']); $this->assertFalse($observers['meetup_observer']['shared']); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  81. 81. 1/2) Get the config in array format $class = MagentoFrameworkEventConfig::class; $eventConfig = ObjectManager::getInstance()->create($class); $event = 'sales_order_place_after'; $observers = $eventConfig->getObservers($event); Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  82. 82. 2/2) Assert config values $this->assertArrayHasKey('meetup_observer', $observers); $this->assertSame( SalesOrderPlaceAfter::class, $observers['meetup_observer']['instance'] ); $this->assertFalse($observers['meetup_observer']['shared']); Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  83. 83. II. TESTING THE OBSERVER Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  84. 84. O_0(NOTHING TO TEST) Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  85. 85. III. INTEGRATION TEST Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  86. 86. Our observer has no side effects. We have to improvise... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  87. 87. /** * @magentoDataFixture Magento/Sales/_files/order.php */ public function testSalesOrderPlaceAfterObserverIsCalled() { $this->injectMockObserverWithExpectation(); $order = $this->getOrderFixture(); $this->objectManager ->create(OrderManagementInterface::class) ->place($order); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  88. 88. public function injectMockObserverWithExpectation() { $mockObserver = $this->getMock(SalesOrderPlaceAfter::class); $mockObserver->expects($this->once())->method('execute'); $this->registerObserverTestDouble($mockObserver); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  89. 89. private function registerObserverTestDouble($mockObserver) { $class = MagentoFrameworkEventConfigData::class; $eventConfigData = $this->objectManager->get($class); $event = 'sales_order_place_after'; $name = 'meetup_observer'; $eventConfigData->merge( [$event => [$name => ['shared' => true]]] ); // ... } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  90. 90. private function registerObserverTestDouble($mockObserver) { // ... $this->objectManager->addSharedInstance( $mockObserver, SalesOrderPlaceAfter::class ); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  91. 91. /** * @magentoDataFixture Magento/Sales/_files/order.php */ public function testSalesOrderPlaceAfterObserverIsCalled() { // ... $order = $this->getOrderFixture(); // ... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  92. 92. private function getOrderFixture() { $class = OrderRepositoryInterface::class; $orderRepository = $this->objectManager->create($class); $criteria = $this->objectManager ->create(SearchCriteriaBuilder::class) ->addFilter('increment_id', '100000001') ->create(); $result = $orderRepository->getList($criteria); return array_values($result->getItems())[0]; } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  93. 93. /** * @magentoDataFixture Magento/Sales/_files/order.php */ public function testSalesOrderPlaceAfterObserverIsCalled() { $this->injectMockObserverWithExpectation(); $order = $this->getOrderFixture(); $this->objectManager ->create(OrderManagementInterface::class) ->place($order); } Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  94. 94. GO FORTH AND TESTALL THINGS! Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  95. 95. ONE MORE THING... Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  96. 96. MAGE2KATAS Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  97. 97. MAGE2KATAS > Screencast Series > Examples > Code Katas mage2katas.com Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp
  98. 98. THANK YOU <3 Getting your hands dirty Testing Magento 2 - Magento London Meetup, Feb. 2016 - ! - contact@vinaikopp.com - twitter://@VinaiKopp

×