SlideShare uma empresa Scribd logo
1 de 20
Using of Test Driven DevelopmentPractices for Magento Ivan Chepurnyi Magento Trainer / Lead Developer
Short Overview Meet Magento Netherlands Write a test Imaging how your feature should work and write a failing test Write feature quickly for receiving passed test Refactor your code and run the test again Pass Run the tests Fail Pass Write the code Fail Run the tests
Conventional Development Meet Magento Netherlands You need to write a full functionality for seeing the result You think that your code works Debugging With every fixed issue you may produce a new one Even if you write unit tests after implementation it doesn’t guarantee that you detect the defect.
Test Driven Development Meet Magento Netherlands You don’t need to implement full functionality for seeing the result Your colleagues can use your test as learning material The test proofs that your code works and can be verified Serious defects can be fixed on early state
Defect Costs Meet Magento Netherlands Percentage from project development hours < 1% 20% > 40% Defect found at the coding stage Defect found during QA phase Defect found after going live
Type of Tests Meet Magento Netherlands Automated Test Regression Test Learning Test Integration Test
Meet Magento Netherlands Test in Isolation Make test simple Test erroneous situations Test Doubles (Fake heavy resources you don’t depend on) Main Principles
EcomDev_PHPUnit Meet Magento Netherlands Making possible isolation of your test Easy test data loading via Yaml fixtures Data providers and expectations for reusable tests Easy way of testing configuration files Easy way for Layouts & Controllers  integration test
Simple Test Case Meet Magento Netherlands class EcomDev_Example_Test_Model_Productextends EcomDev_PHPUnit_Test_Case { 	/** * @test  * @loadFixture * @dataProviderdataProvider */ public function priceCalculation($productId, $storeId) {       $storeId = Mage::app()->getStore($storeId)->getId();       $product = Mage::getModel('catalog/product')                              ->setStoreId($storeId) ->load($productId);       $expected = $this->expected('%s-%s', $productId, $storeId);       $this->assertEquals($expected->getFinalPrice(), $product->getFinalPrice());        $this->assertEquals($expected->getPrice(), $product->getPrice());         } } Test Case Class
Simple Test Case Meet Magento Netherlands eav:    catalog_product:       - entity_id: 1        type_id: simple          sku: book                 website_ids:                     - usa_website                     - canada_website                   price: 12.99                 status: 1  # Enabled                 visibility: 4  # Visible in Catalog & Search                 /websites:  # Set different prices per  website            usa_website:            special_price: 9.99          german_website:                          price: 9.99            special_price: 5.99  Yaml Fixture
Simple Test Case Meet Magento Netherlands 1-2: # Product=Book Store=USA final_price: 9.99   price: 12.99 1-3: # Product=Book Store=Canada final_price: 12.99    price: 12.99 Yaml Expectation Yaml Data Provider -   - 1   - usa -   - 1   - canada -   - 1   - germany
Event Dispatch Check Meet Magento Netherlands class EcomDev_Example_Test_Model_Cms_Pageextends EcomDev_PHPUnit_Test_Case {        // … 	public function testAvailableStatuses() {              Mage::getModel(‘cms/page’)->getAvailableStatuses(); 	       $this->assertEventDispatched( 		  ‘cms_page_get_available_statuses’               ); 	} } Test Case
Test Doubles Meet Magento Netherlands class EcomDev_PHPUnit_Tes_Case_Controllerextends EcomDev_PHPUnit_Test_Case { 	protected function registerCookieStub()        {                 $cookie = $this->getModelMock('core/cookie', array('set', 'delete'));                 $cookie->expects($this->any())                        ->method('set')                        ->will($this->returnCallback(array($this, 'setCookieCallback‘)));                 $cookie->expects($this->any())                        ->method('delete‘)                        ->will($this->returnCallback(array($this, 'deleteCookieCallback‘)));                 $this->replaceByMock('singleton', 'core/cookie', $cookie);                 return $this;         } } Test Case
Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config {     //….     public function testModuleVersion()     {         $this->assertModuleCodePool('local');         $this->assertModuleDepends(‘Mage_Catalog’);         $this->assertModuleVersionGreaterThan(‘0.1.0');      } } Testing Module Nodes
Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config {     //….     public function testClassAliasDefinitions()     {         $this->assertModelAlias('catalog/product', 'Mage_Catalog_Model_Product');         $this->assertResourceModelAlias( 		'catalog/product',  		‘Mage_Catalog_Model_Resource_Eav_Mysql4_Product‘ 	);         $this->assertBlockAlias( 		'catalog/product_list',  		'Mage_Catalog_Block_Product_List‘ 	);      } } Testing Class Aliases
Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config {     //….     public function testEventObservers()     { 	  $this->assertEventObserverDefined(             'frontend', 'customer_login',              'catalog/product_compare_item',              'bindCustomerLogin'         );      } } Testing Event Observer Definitions
Controller Test Case Meet Magento Netherlands class EcomDev_Example_Test_Controller_Main extends EcomDev_PHPUnit_Test_Case_Controller {     public function testRequest()     {         $this->dispatch('cms');         $this->assertRequestDispatched();         $this->assertRequestNotForwarded();         $this->assertRequestRoute('cms/index/index');         $this->assertRequestRouteName('cms');         $this->assertRequestControllerName('index');         $this->assertRequestControllerModule('Mage_Cms');         $this->assertRequestActionName('index');     } } Testing Request
Controller Test Case Meet Magento Netherlands class EcomDev_Example_Test_Controller_Main         extends EcomDev_PHPUnit_Test_Case_Controller {     public function testLayout()     {         $this->dispatch('');         $this->assertLayoutHandleLoaded('cms_index_index'); 	$this->assertLayoutBlockCreated('right');         $this->assertLayoutBlockRendered('content'); 	 $this->assertLayoutBlockActionNotInvoked( 	       'footer_links', 'addLink', '', array('Custom Title')         );         $this->assertLayoutBlockActionInvokedAtLeast( 	       'footer_links', 'addLink', 4, '‘ 	);     } } Testing Layouts
What’s Next Meet Magento Netherlands Write automated tests for your moduleshttp://www.magentocommerce.com/magento-connect/Ecommerce%20Developers/extension/5717/ecomdev_phpunit Keep project healthy during its lifecycle with Continuous Integration Running Daily Builds Running Unit Tests in 10 minutes after last commit Hudson http://hudson-ci.org phpUnderControlhttp://phpundercontrol.org/
Questions? ivan.chepurnyi@ecomdev.org

Mais conteúdo relacionado

Mais procurados

購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
Owl: The New Odoo UI Framework
Owl: The New Odoo UI FrameworkOwl: The New Odoo UI Framework
Owl: The New Odoo UI FrameworkOdoo
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projectsIgnacio Martín
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2zfconfua
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片cfc
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsAlessandro Molina
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesCiaranMcNulty
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingErick Hitter
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS DirectivesEyal Vardi
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 MinutesAzim Kurt
 

Mais procurados (20)

Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
Drupal 8 Services
Drupal 8 ServicesDrupal 8 Services
Drupal 8 Services
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
Owl: The New Odoo UI Framework
Owl: The New Odoo UI FrameworkOwl: The New Odoo UI Framework
Owl: The New Odoo UI Framework
 
Frontin like-a-backer
Frontin like-a-backerFrontin like-a-backer
Frontin like-a-backer
 
Integrating React.js with PHP projects
Integrating React.js with PHP projectsIntegrating React.js with PHP projects
Integrating React.js with PHP projects
 
Doctrine 2
Doctrine 2Doctrine 2
Doctrine 2
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
 
Basic JSTL
Basic JSTLBasic JSTL
Basic JSTL
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 
Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes50 Laravel Tricks in 50 Minutes
50 Laravel Tricks in 50 Minutes
 
Framework
FrameworkFramework
Framework
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
Advanced Django
Advanced DjangoAdvanced Django
Advanced Django
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 

Semelhante a Using of TDD practices for Magento

From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the typeWim Godden
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2Javier Eguiluz
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4Javier Eguiluz
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Jason Lotito
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitPeter Wilcsinszky
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Best Practice Testing with Lime 2
Best Practice Testing with Lime 2Best Practice Testing with Lime 2
Best Practice Testing with Lime 2Bernhard Schussek
 
Meet Magento DE 2016 - Kristof Ringleff - Growing up with Magento
Meet Magento DE 2016 - Kristof Ringleff - Growing up with MagentoMeet Magento DE 2016 - Kristof Ringleff - Growing up with Magento
Meet Magento DE 2016 - Kristof Ringleff - Growing up with MagentoKristof Ringleff
 
Continously delivering
Continously deliveringContinously delivering
Continously deliveringJames Cowie
 

Semelhante a Using of TDD practices for Magento (20)

PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
Symfony 1, mi viejo amigo
Symfony 1, mi viejo amigoSymfony 1, mi viejo amigo
Symfony 1, mi viejo amigo
 
From typing the test to testing the type
From typing the test to testing the typeFrom typing the test to testing the type
From typing the test to testing the type
 
Test driven development_for_php
Test driven development_for_phpTest driven development_for_php
Test driven development_for_php
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
Curso Symfony - Clase 2
Curso Symfony - Clase 2Curso Symfony - Clase 2
Curso Symfony - Clase 2
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
 
Curso Symfony - Clase 4
Curso Symfony - Clase 4Curso Symfony - Clase 4
Curso Symfony - Clase 4
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Growing up with Magento
Growing up with MagentoGrowing up with Magento
Growing up with Magento
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Jasmine with JS-Test-Driver
Jasmine with JS-Test-DriverJasmine with JS-Test-Driver
Jasmine with JS-Test-Driver
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Testing persistence in PHP with DbUnit
Testing persistence in PHP with DbUnitTesting persistence in PHP with DbUnit
Testing persistence in PHP with DbUnit
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Best Practice Testing with Lime 2
Best Practice Testing with Lime 2Best Practice Testing with Lime 2
Best Practice Testing with Lime 2
 
Meet Magento DE 2016 - Kristof Ringleff - Growing up with Magento
Meet Magento DE 2016 - Kristof Ringleff - Growing up with MagentoMeet Magento DE 2016 - Kristof Ringleff - Growing up with Magento
Meet Magento DE 2016 - Kristof Ringleff - Growing up with Magento
 
Continously delivering
Continously deliveringContinously delivering
Continously delivering
 

Último

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

Using of TDD practices for Magento

  • 1. Using of Test Driven DevelopmentPractices for Magento Ivan Chepurnyi Magento Trainer / Lead Developer
  • 2. Short Overview Meet Magento Netherlands Write a test Imaging how your feature should work and write a failing test Write feature quickly for receiving passed test Refactor your code and run the test again Pass Run the tests Fail Pass Write the code Fail Run the tests
  • 3. Conventional Development Meet Magento Netherlands You need to write a full functionality for seeing the result You think that your code works Debugging With every fixed issue you may produce a new one Even if you write unit tests after implementation it doesn’t guarantee that you detect the defect.
  • 4. Test Driven Development Meet Magento Netherlands You don’t need to implement full functionality for seeing the result Your colleagues can use your test as learning material The test proofs that your code works and can be verified Serious defects can be fixed on early state
  • 5. Defect Costs Meet Magento Netherlands Percentage from project development hours < 1% 20% > 40% Defect found at the coding stage Defect found during QA phase Defect found after going live
  • 6. Type of Tests Meet Magento Netherlands Automated Test Regression Test Learning Test Integration Test
  • 7. Meet Magento Netherlands Test in Isolation Make test simple Test erroneous situations Test Doubles (Fake heavy resources you don’t depend on) Main Principles
  • 8. EcomDev_PHPUnit Meet Magento Netherlands Making possible isolation of your test Easy test data loading via Yaml fixtures Data providers and expectations for reusable tests Easy way of testing configuration files Easy way for Layouts & Controllers integration test
  • 9. Simple Test Case Meet Magento Netherlands class EcomDev_Example_Test_Model_Productextends EcomDev_PHPUnit_Test_Case { /** * @test * @loadFixture * @dataProviderdataProvider */ public function priceCalculation($productId, $storeId) { $storeId = Mage::app()->getStore($storeId)->getId(); $product = Mage::getModel('catalog/product') ->setStoreId($storeId) ->load($productId); $expected = $this->expected('%s-%s', $productId, $storeId); $this->assertEquals($expected->getFinalPrice(), $product->getFinalPrice()); $this->assertEquals($expected->getPrice(), $product->getPrice()); } } Test Case Class
  • 10. Simple Test Case Meet Magento Netherlands eav:    catalog_product:     - entity_id: 1        type_id: simple       sku: book          website_ids:          - usa_website          - canada_website          price: 12.99        status: 1  # Enabled        visibility: 4  # Visible in Catalog & Search        /websites:  # Set different prices per website         usa_website:            special_price: 9.99          german_website:            price: 9.99            special_price: 5.99 Yaml Fixture
  • 11. Simple Test Case Meet Magento Netherlands 1-2: # Product=Book Store=USA final_price: 9.99 price: 12.99 1-3: # Product=Book Store=Canada final_price: 12.99 price: 12.99 Yaml Expectation Yaml Data Provider - - 1 - usa - - 1 - canada - - 1 - germany
  • 12. Event Dispatch Check Meet Magento Netherlands class EcomDev_Example_Test_Model_Cms_Pageextends EcomDev_PHPUnit_Test_Case { // … public function testAvailableStatuses() { Mage::getModel(‘cms/page’)->getAvailableStatuses(); $this->assertEventDispatched( ‘cms_page_get_available_statuses’ ); } } Test Case
  • 13. Test Doubles Meet Magento Netherlands class EcomDev_PHPUnit_Tes_Case_Controllerextends EcomDev_PHPUnit_Test_Case { protected function registerCookieStub() { $cookie = $this->getModelMock('core/cookie', array('set', 'delete')); $cookie->expects($this->any()) ->method('set') ->will($this->returnCallback(array($this, 'setCookieCallback‘))); $cookie->expects($this->any()) ->method('delete‘) ->will($this->returnCallback(array($this, 'deleteCookieCallback‘))); $this->replaceByMock('singleton', 'core/cookie', $cookie); return $this; } } Test Case
  • 14. Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config { //…. public function testModuleVersion() { $this->assertModuleCodePool('local'); $this->assertModuleDepends(‘Mage_Catalog’); $this->assertModuleVersionGreaterThan(‘0.1.0'); } } Testing Module Nodes
  • 15. Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config { //…. public function testClassAliasDefinitions() { $this->assertModelAlias('catalog/product', 'Mage_Catalog_Model_Product'); $this->assertResourceModelAlias( 'catalog/product', ‘Mage_Catalog_Model_Resource_Eav_Mysql4_Product‘ ); $this->assertBlockAlias( 'catalog/product_list', 'Mage_Catalog_Block_Product_List‘ ); } } Testing Class Aliases
  • 16. Config Test Case Meet Magento Netherlands class EcomDev_Example_Test_Config_Main extends EcomDev_PHPUnit_Test_Case_Config { //…. public function testEventObservers() { $this->assertEventObserverDefined( 'frontend', 'customer_login', 'catalog/product_compare_item', 'bindCustomerLogin' ); } } Testing Event Observer Definitions
  • 17. Controller Test Case Meet Magento Netherlands class EcomDev_Example_Test_Controller_Main extends EcomDev_PHPUnit_Test_Case_Controller { public function testRequest() { $this->dispatch('cms'); $this->assertRequestDispatched(); $this->assertRequestNotForwarded(); $this->assertRequestRoute('cms/index/index'); $this->assertRequestRouteName('cms'); $this->assertRequestControllerName('index'); $this->assertRequestControllerModule('Mage_Cms'); $this->assertRequestActionName('index'); } } Testing Request
  • 18. Controller Test Case Meet Magento Netherlands class EcomDev_Example_Test_Controller_Main extends EcomDev_PHPUnit_Test_Case_Controller { public function testLayout() { $this->dispatch(''); $this->assertLayoutHandleLoaded('cms_index_index'); $this->assertLayoutBlockCreated('right'); $this->assertLayoutBlockRendered('content'); $this->assertLayoutBlockActionNotInvoked( 'footer_links', 'addLink', '', array('Custom Title') ); $this->assertLayoutBlockActionInvokedAtLeast( 'footer_links', 'addLink', 4, '‘ ); } } Testing Layouts
  • 19. What’s Next Meet Magento Netherlands Write automated tests for your moduleshttp://www.magentocommerce.com/magento-connect/Ecommerce%20Developers/extension/5717/ecomdev_phpunit Keep project healthy during its lifecycle with Continuous Integration Running Daily Builds Running Unit Tests in 10 minutes after last commit Hudson http://hudson-ci.org phpUnderControlhttp://phpundercontrol.org/