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.

Magento 2 TDD Code Kata

752 views

Published on

Intro slides to the Magento 2 TDD live coding demo at Meet-Magento 2018 in Ahmedabad on 6. January 2018.

Published in: Software

Magento 2 TDD Code Kata

  1. 1. TDD Katas in Magento 2 TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  2. 2. Code Kata TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  3. 3. Code Kata? TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  4. 4. We practice our craft. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  5. 5. We train like athletes do. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  6. 6. What do we train in a code kata? TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  7. 7. The Mind → The 3 phases of TDD → Working in small units → Recognizing patterns TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  8. 8. The Body → Keyboard shortcuts → Efficient grouping of actions TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  9. 9. Framework Knowledge → Memorizing framework sections TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  10. 10. Katas are usually done multiple times over a period of time. E.g. once per day over 1 week, once per week over 3 months, ... TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  11. 11. Time Limit: 15 - 30 minutes TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  12. 12. Today is just a Demo TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  13. 13. An impression how TDD in Magento can look. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  14. 14. To inspire you to start doing katas as soon as you are back! TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  15. 15. Today's Kata: Magento 2 Total Model TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  16. 16. Total Models A flexible system to calculate cart and cart item prices and adjustments. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  17. 17. <!-- File: etc/sales.xml --> <section name="quote"> <group name="totals"> <item name="subtotal" sort_order="100" instance="MagentoQuoteModelQuoteAddressTotalSubtotal"/> <item name="tax_subtotal" sort_order="200" instance="MagentoTaxModelSalesTotalQuoteSubtotal"/> <item name="shipping" sort_order="250" instance="MagentoQuoteModelQuoteAddressTotalShipping"/> <item name="discount" sort_order="400" instance="MagentoSalesRuleModelQuoteDiscount"/> <item name="tax" sort_order="450" instance="MagentoTaxModelSalesTotalQuoteTax"/> <item name="grand_total" sort_order="550" instance="MagentoQuoteModelQuoteAddressTotalGrand" /> </group> </section> TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  18. 18. Processed in order → First comes subtotal Sets quote item prices & subtotal → Then all the others shipping, discount, tax, ... → Last comes grand_total Sums up all other total models TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  19. 19. Each one adds it's value on ModelQuoteAddressTotal TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  20. 20. The Kata Example code is missing a custom total model. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  21. 21. Training Module: Example_GiftItem When a customer adds a product from the sample data "Bags" attribute set, to the cart, also add a free water bottle. https://github.com/Vinai/module-example-giftitem TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  22. 22. Training Module: Example_GiftItem The GiftItem total model is responsible for setting the price of gift items to zero and adjusting the subtotal. https://github.com/Vinai/module-example-giftitem TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  23. 23. What does this kata teach? TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  24. 24. (1/2) Generic skills: → TDD Red-Green-Refactor process → Working with test doubles → Separating pure functions from functions with side effects TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  25. 25. (2/2) Magento skills: → Test driving Magento development → The totals architecture → Dealing with Law of Demeter violations TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  26. 26. Tomorrow How you can get started: # Create module dir mkdir -p app/code/Example && cd app/code/Example # Clone repo git clone https://github.com/Vinai/module-example-giftitem.git GiftItem cd GiftItem # Check out git tag git checkout beginning-of-kata TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  27. 27. Kata Task: Use TDD to create the GiftItem total model ModelTotalsGiftItemAddressTotal Implement the collect() method. TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  28. 28. Target behavior: collect() → Subtract gift item row totals sum and base row totals sum from the subtotal → Set calculation_price and base_calculation_price of every gift item to zero → Call calcRowTotal on each gift TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp
  29. 29. Enjoy the session and feel those reflexes get sharper! TDD Magento 2 Kata - contact@vinaikopp.com - twitter://vinaikopp

×