Your SlideShare is downloading. ×
0
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Using of TDD practices for Magento
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Using of TDD practices for Magento

6,391

Published on

My Presentation from MeetMagento NL 2011

My Presentation from MeetMagento NL 2011

Published in: Technology, Business
1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total Views
6,391
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
86
Comments
1
Likes
11
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Using of Test Driven DevelopmentPractices for Magento<br />Ivan Chepurnyi<br />Magento Trainer / Lead Developer<br />
  • 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. 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. 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. Defect Costs<br />Meet Magento Netherlands<br />Percentage from project development hours<br />&lt; 1%<br />20%<br />&gt; 40%<br />Defect found at the coding stage<br />Defect found during QA phase<br />Defect found after going live<br />
  • 6. Type of Tests<br />Meet Magento Netherlands<br />Automated Test<br />Regression Test<br />Learning Test<br />Integration Test<br />
  • 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. 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 &amp; Controllers integration test<br />
  • 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()-&gt;getStore($storeId)-&gt;getId(); $product = Mage::getModel(&apos;catalog/product&apos;) -&gt;setStoreId($storeId) -&gt;load($productId); $expected = $this-&gt;expected(&apos;%s-%s&apos;, $productId, $storeId); $this-&gt;assertEquals($expected-&gt;getFinalPrice(), $product-&gt;getFinalPrice()); <br /> $this-&gt;assertEquals($expected-&gt;getPrice(), $product-&gt;getPrice()); <br /> }<br />}<br />Test Case Class<br />
  • 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 &amp; 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. 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. 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’)-&gt;getAvailableStatuses();<br /> $this-&gt;assertEventDispatched(<br /> ‘cms_page_get_available_statuses’<br /> );<br /> }<br />}<br />Test Case<br />
  • 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-&gt;getModelMock(&apos;core/cookie&apos;, array(&apos;set&apos;, &apos;delete&apos;));<br /> $cookie-&gt;expects($this-&gt;any())<br /> -&gt;method(&apos;set&apos;)<br /> -&gt;will($this-&gt;returnCallback(array($this, &apos;setCookieCallback‘)));<br /> $cookie-&gt;expects($this-&gt;any())<br /> -&gt;method(&apos;delete‘)<br /> -&gt;will($this-&gt;returnCallback(array($this, &apos;deleteCookieCallback‘)));<br /> $this-&gt;replaceByMock(&apos;singleton&apos;, &apos;core/cookie&apos;, $cookie);<br /> return $this;<br /> }<br />}<br />Test Case<br />
  • 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-&gt;assertModuleCodePool(&apos;local&apos;);<br /> $this-&gt;assertModuleDepends(‘Mage_Catalog’);<br /> $this-&gt;assertModuleVersionGreaterThan(‘0.1.0&apos;);<br /> }<br />}<br />Testing Module Nodes<br />
  • 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-&gt;assertModelAlias(&apos;catalog/product&apos;, &apos;Mage_Catalog_Model_Product&apos;);<br /> $this-&gt;assertResourceModelAlias(<br /> &apos;catalog/product&apos;, <br /> ‘Mage_Catalog_Model_Resource_Eav_Mysql4_Product‘<br /> );<br /> $this-&gt;assertBlockAlias(<br /> &apos;catalog/product_list&apos;, <br /> &apos;Mage_Catalog_Block_Product_List‘<br /> );<br /> }<br />}<br />Testing Class Aliases<br />
  • 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-&gt;assertEventObserverDefined(<br /> &apos;frontend&apos;, &apos;customer_login&apos;, <br /> &apos;catalog/product_compare_item&apos;, <br /> &apos;bindCustomerLogin&apos;<br /> );<br /> }<br />}<br />Testing Event Observer Definitions<br />
  • 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-&gt;dispatch(&apos;cms&apos;);<br /> $this-&gt;assertRequestDispatched();<br /> $this-&gt;assertRequestNotForwarded();<br /> $this-&gt;assertRequestRoute(&apos;cms/index/index&apos;);<br /> $this-&gt;assertRequestRouteName(&apos;cms&apos;);<br /> $this-&gt;assertRequestControllerName(&apos;index&apos;);<br /> $this-&gt;assertRequestControllerModule(&apos;Mage_Cms&apos;);<br /> $this-&gt;assertRequestActionName(&apos;index&apos;);<br /> }<br />}<br />Testing Request<br />
  • 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-&gt;dispatch(&apos;&apos;);<br /> $this-&gt;assertLayoutHandleLoaded(&apos;cms_index_index&apos;);<br /> $this-&gt;assertLayoutBlockCreated(&apos;right&apos;);<br /> $this-&gt;assertLayoutBlockRendered(&apos;content&apos;);<br /> $this-&gt;assertLayoutBlockActionNotInvoked(<br /> &apos;footer_links&apos;, &apos;addLink&apos;, &apos;&apos;, array(&apos;Custom Title&apos;)<br /> );<br /> $this-&gt;assertLayoutBlockActionInvokedAtLeast(<br /> &apos;footer_links&apos;, &apos;addLink&apos;, 4, &apos;‘<br /> );<br /> }<br />}<br />Testing Layouts<br />
  • 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. Questions?<br />ivan.chepurnyi@ecomdev.org<br />

×