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.

Using of TDD practices for Magento

9,157 views

Published on

My Presentation from MeetMagento NL 2011

Published in: Technology, Business
  • Login to see the comments

Using of TDD practices for Magento

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

×