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.
© 2017 Magento, Inc. Page | 1
Unit testing in MSI
*and not only
Igor Miniailo
Magento 2 Architect
© 2017 Magento, Inc. Page | 2
© 2017 Magento, Inc. Page | 3
Unit testing in MSI
Unit testing in MSI
Unit testing in MSI Magento 2
Unit testing in MSI Ma...
© 2017 Magento, Inc. Page | 4
MSI is designed to enable stock management in multiple
locations so that merchants can prope...
© 2017 Magento, Inc. Page | 5
Split the inventory
between the
sources within one
Magento installation
© 2017 Magento, Inc. Page | 6
Replace current CatalogInventory with MSI
© 2017 Magento, Inc. Page | 7
© 2017 Magento, Inc. Page | 8
Unit Testing
© 2017 Magento, Inc. Page | 9
Unit test types
https://martinfowler.com/bliki/UnitTest.html
© 2017 Magento, Inc. Page | 10
Test Doubles
https://martinfowler.com/bliki/TestDouble.html
© 2017 Magento, Inc. Page | 11
Let’s see how typical Unit test for this code looks like
© 2017 Magento, Inc. Page | 12
Solitary Unit Test with Mocks and Expects
© 2017 Magento, Inc. Page | 13
TTDD - Tautological Test Driven Development
Tautological: - needless, redundant repetition ...
© 2017 Magento, Inc. Page | 14
TTDD - Tautological Test Driven Development
Unit tests currently do not check that code wor...
© 2017 Magento, Inc. Page | 15
http://fabiopereira.me/blog/2010/05/27/ttdd-tautological-
test-driven-development-anti-patt...
© 2017 Magento, Inc. Page | 16
• Asserts more interactions with collaborators than the outputs
• It doesn’t really test th...
© 2017 Magento, Inc. Page | 17
The problem is not in Unit tests
The problem is in Transient State of objects
under the tes...
© 2017 Magento, Inc. Page | 18
© 2017 Magento, Inc. Page | 19
Two Unit tests passed. No Integration tests
© 2017 Magento, Inc. Page | 20
Two Unit tests passed. No Integration tests
© 2017 Magento, Inc. Page | 21
Whether Unit tests are useless?
NO!!!
They could be handy for TDD (as Vinai shown today)
Un...
© 2017 Magento, Inc. Page | 22
Integration Testing
© 2017 Magento, Inc. Page | 23
• Readability
– Test should be easy to follow
– Test should be simple to update
– Code can ...
© 2017 Magento, Inc. Page | 24
Reservation - the entity is used when the order is placed and
we need to reserve some produ...
© 2017 Magento, Inc. Page | 25
Order Placement – Step 1
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse...
© 2017 Magento, Inc. Page | 26
Order Placement – Step 2
Action: Customer buys 5 products on frontend
© 2017 Magento, Inc. Page | 27
Order Placement – Step 3
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse...
© 2017 Magento, Inc. Page | 28
Order Placement – Step 4
Action: Admin makes a re-order, 3 products out of 5 returned,
and ...
© 2017 Magento, Inc. Page | 29
Order Placement – Step 5
France Warehouse
SKU-1: 5qty
EU Stock
SKU-1: 15qty
Italy Warehouse...
© 2017 Magento, Inc. Page | 30
Order Placement – Step 6
Action: Admin completes order. Re-index was run.
© 2017 Magento, Inc. Page | 31
Order Placement – Step 7
France Warehouse
SKU-1: 3qty
EU Stock
SKU-1: 13qty
Italy Warehouse...
© 2017 Magento, Inc. Page | 32
Order Placement – Step 8
Action: Reservation cleaning
Looping through these reservations we...
© 2017 Magento, Inc. Page | 33
Order Placement – Step 9
(like Step 1)
France Warehouse
SKU-1: 3qty
EU Stock
SKU-1: 13qty
I...
© 2017 Magento, Inc. Page | 34
API interface to test
© 2017 Magento, Inc. Page | 35
So, what’s about testing?
© 2017 Magento, Inc. Page | 36
What about fixtures?
© 2017 Magento, Inc. Page | 37
© 2017 Magento, Inc. Page | 38
Tests Isolation
© 2017 Magento, Inc. Page | 39
Clean the state after your tests!
© 2017 Magento, Inc. Page | 40
Cleaning up the state. Rollback Fixtures
© 2017 Magento, Inc. Page | 41
Cleaning up the state. Rollback Fixtures
© 2017 Magento, Inc. Page | 42
Cleaning up the state
© 2017 Magento, Inc. Page | 43
• Readability
– Test should be easy to follow
– Test should be simple to update
– Code can ...
© 2017 Magento, Inc. Page | 44
Web API Testing
© 2017 Magento, Inc. Page | 45
© 2017 Magento, Inc. Page | 46
© 2017 Magento, Inc. Page | 47
© 2017 Magento, Inc. Page | 48
Public vs Private code
All public code annotated with
@api PhpDoc Block.
Private code is no...
© 2017 Magento, Inc. Page | 49
Good object oriented programming in the service layer is basically
functional programming:
...
© 2017 Magento, Inc. Page | 50
© 2017 Magento, Inc. Page | 51
Repositories
© 2017 Magento, Inc. Page | 52
Headless Magento
© 2017 Magento, Inc. Page | 53
RESTful API
© 2017 Magento, Inc. Page | 54
Swagger
http://devdocs.magento.com/guides/v2.2/rest/generate-local.html
© 2017 Magento, Inc. Page | 55
Web API tests
https://github.com/magento-engcom/msi/wiki/How-to-create-Web-API-and-How-To-c...
© 2017 Magento, Inc. Page | 56
Database Generated IDs
© 2017 Magento, Inc. Page | 57
Why can’t we just use predefined IDs?
© 2017 Magento, Inc. Page | 58
Predefined ID approach (used in MSI)
© 2017 Magento, Inc. Page | 59
Predefined IDs matter
Just for this particular case (Source to Stock assignment) - we got r...
© 2017 Magento, Inc. Page | 60
Be Responsible or train your dog tests to be!
© 2017 Magento, Inc. Page | 61
How to join us? Send an email to
engcom@magento.com
@iminyaylo
Thank y’all!
Upcoming SlideShare
Loading in …5
×

Testing in Magento 2

771 views

Published on

Testing in Magento 2 by examples of Multi-Source Inventory Project (MSI)
- Unit Testing and how to do them right.
- Tautological Test Driven Development
- Integration Testing
- Example of Reservation mechanism in MSI and Integration test coverage for it
- Web API testing (REST, SOAP)

Published in: Software
  • Be the first to comment

Testing in Magento 2

  1. 1. © 2017 Magento, Inc. Page | 1 Unit testing in MSI *and not only Igor Miniailo Magento 2 Architect
  2. 2. © 2017 Magento, Inc. Page | 2
  3. 3. © 2017 Magento, Inc. Page | 3 Unit testing in MSI Unit testing in MSI Unit testing in MSI Magento 2 Unit testing in MSI Magento 2 based on MSI
  4. 4. © 2017 Magento, Inc. Page | 4 MSI is designed to enable stock management in multiple locations so that merchants can properly reflect their physical warehouses in Magento system without external extensions or customization Multi-Source Inventory (MSI) https://github.com/magento-engcom/msi/
  5. 5. © 2017 Magento, Inc. Page | 5 Split the inventory between the sources within one Magento installation
  6. 6. © 2017 Magento, Inc. Page | 6 Replace current CatalogInventory with MSI
  7. 7. © 2017 Magento, Inc. Page | 7
  8. 8. © 2017 Magento, Inc. Page | 8 Unit Testing
  9. 9. © 2017 Magento, Inc. Page | 9 Unit test types https://martinfowler.com/bliki/UnitTest.html
  10. 10. © 2017 Magento, Inc. Page | 10 Test Doubles https://martinfowler.com/bliki/TestDouble.html
  11. 11. © 2017 Magento, Inc. Page | 11 Let’s see how typical Unit test for this code looks like
  12. 12. © 2017 Magento, Inc. Page | 12 Solitary Unit Test with Mocks and Expects
  13. 13. © 2017 Magento, Inc. Page | 13 TTDD - Tautological Test Driven Development Tautological: - needless, redundant repetition of an idea - repetition of same sense in different words; - the phrase "a beginner who has just started" is tautological
  14. 14. © 2017 Magento, Inc. Page | 14 TTDD - Tautological Test Driven Development Unit tests currently do not check that code works properly, but check that code works the same way as we implemented it. Coupling between Production and Testing code. Test duplicates the business logic code.
  15. 15. © 2017 Magento, Inc. Page | 15 http://fabiopereira.me/blog/2010/05/27/ttdd-tautological- test-driven-development-anti-pattern/
  16. 16. © 2017 Magento, Inc. Page | 16 • Asserts more interactions with collaborators than the outputs • It doesn’t really test the behavior of the class, but only its implementation • The test is too white box • Too much mock setup deviates the intent of the test • Adding a new feature or changing an existing one requires changing mock expectations • Such tests don’t help to find bugs in application code
  17. 17. © 2017 Magento, Inc. Page | 17 The problem is not in Unit tests The problem is in Transient State of objects under the test (especially in legacy code)
  18. 18. © 2017 Magento, Inc. Page | 18
  19. 19. © 2017 Magento, Inc. Page | 19 Two Unit tests passed. No Integration tests
  20. 20. © 2017 Magento, Inc. Page | 20 Two Unit tests passed. No Integration tests
  21. 21. © 2017 Magento, Inc. Page | 21 Whether Unit tests are useless? NO!!! They could be handy for TDD (as Vinai shown today) Unit tests could be the first “Smell” informing that something wrong with your application code
  22. 22. © 2017 Magento, Inc. Page | 22 Integration Testing
  23. 23. © 2017 Magento, Inc. Page | 23 • Readability – Test should be easy to follow – Test should be simple to update – Code can be reused to test all similar cases • Stability – Test should be stable by execution – Test should be stable to code changes – Test should focus on result not on the path • Speed Qualities of a Test
  24. 24. © 2017 Magento, Inc. Page | 24 Reservation - the entity is used when the order is placed and we need to reserve some product quantity in stock. Reservations are append only operations and help us to prevent blocking operations and race conditions at the time of checkout. Reservation mechanism https://github.com/magento-engcom/msi/wiki/Reservations
  25. 25. © 2017 Magento, Inc. Page | 25 Order Placement – Step 1 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations No records Available Qty: 15qty (data from index, empty reservations)
  26. 26. © 2017 Magento, Inc. Page | 26 Order Placement – Step 2 Action: Customer buys 5 products on frontend
  27. 27. © 2017 Magento, Inc. Page | 27 Order Placement – Step 3 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 Available Qty: 10qty (data from index 15, apply all reservations -5) Data is NOT changed Reservation has been added
  28. 28. © 2017 Magento, Inc. Page | 28 Order Placement – Step 4 Action: Admin makes a re-order, 3 products out of 5 returned, and new order consists of 2 products
  29. 29. © 2017 Magento, Inc. Page | 29 Order Placement – Step 5 France Warehouse SKU-1: 5qty EU Stock SKU-1: 15qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 SKU-1: +3 Available Qty: 13qty (data from index 15, apply reservations -5+3) Data is NOT changed Reservation has been added
  30. 30. © 2017 Magento, Inc. Page | 30 Order Placement – Step 6 Action: Admin completes order. Re-index was run.
  31. 31. © 2017 Magento, Inc. Page | 31 Order Placement – Step 7 France Warehouse SKU-1: 3qty EU Stock SKU-1: 13qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations SKU-1: -5 SKU-1: +3 SKU-1: +2 Available Qty: 13qty (data from index 13, apply reservations -5+3+2=0) Data is CHANGED Compensation Reservation has been added
  32. 32. © 2017 Magento, Inc. Page | 32 Order Placement – Step 8 Action: Reservation cleaning Looping through these reservations we could find reservations which in sum gives 0 (Zero) and remove them.
  33. 33. © 2017 Magento, Inc. Page | 33 Order Placement – Step 9 (like Step 1) France Warehouse SKU-1: 3qty EU Stock SKU-1: 13qty Italy Warehouse SKU-1: 10qty Raw data Index Reservations Available Qty: 13qty (data from index, empty reservations) Data is NOT changed Reservations have been removed No records
  34. 34. © 2017 Magento, Inc. Page | 34 API interface to test
  35. 35. © 2017 Magento, Inc. Page | 35 So, what’s about testing?
  36. 36. © 2017 Magento, Inc. Page | 36 What about fixtures?
  37. 37. © 2017 Magento, Inc. Page | 37
  38. 38. © 2017 Magento, Inc. Page | 38 Tests Isolation
  39. 39. © 2017 Magento, Inc. Page | 39 Clean the state after your tests!
  40. 40. © 2017 Magento, Inc. Page | 40 Cleaning up the state. Rollback Fixtures
  41. 41. © 2017 Magento, Inc. Page | 41 Cleaning up the state. Rollback Fixtures
  42. 42. © 2017 Magento, Inc. Page | 42 Cleaning up the state
  43. 43. © 2017 Magento, Inc. Page | 43 • Readability – Test should be easy to follow – Test should be simple to update – Code can be reused to test all similar cases • Stability – Test should be stable by execution – Test should be stable to code changes – Test should focus on result not on the path • Speed Qualities of a Test
  44. 44. © 2017 Magento, Inc. Page | 44 Web API Testing
  45. 45. © 2017 Magento, Inc. Page | 45
  46. 46. © 2017 Magento, Inc. Page | 46
  47. 47. © 2017 Magento, Inc. Page | 47
  48. 48. © 2017 Magento, Inc. Page | 48 Public vs Private code All public code annotated with @api PhpDoc Block. Private code is not supposed to be used by third party modules, so, in most cases, its modifications will only trigger PATCH version bumps. Changes in public code always trigger MINOR or MAJOR version bumps.
  49. 49. © 2017 Magento, Inc. Page | 49 Good object oriented programming in the service layer is basically functional programming: • constructor injection • immutable state • single responsibility principle • uniform interfaces • value objects passed across services Bringing these concepts to an extreme leads to single-method, immutable objects.
  50. 50. © 2017 Magento, Inc. Page | 50
  51. 51. © 2017 Magento, Inc. Page | 51 Repositories
  52. 52. © 2017 Magento, Inc. Page | 52 Headless Magento
  53. 53. © 2017 Magento, Inc. Page | 53 RESTful API
  54. 54. © 2017 Magento, Inc. Page | 54 Swagger http://devdocs.magento.com/guides/v2.2/rest/generate-local.html
  55. 55. © 2017 Magento, Inc. Page | 55 Web API tests https://github.com/magento-engcom/msi/wiki/How-to-create-Web-API-and-How-To-cover-them-with-Functional-Testing
  56. 56. © 2017 Magento, Inc. Page | 56 Database Generated IDs
  57. 57. © 2017 Magento, Inc. Page | 57 Why can’t we just use predefined IDs?
  58. 58. © 2017 Magento, Inc. Page | 58 Predefined ID approach (used in MSI)
  59. 59. © 2017 Magento, Inc. Page | 59 Predefined IDs matter Just for this particular case (Source to Stock assignment) - we got rid of more than 300 lines of boilerplate code in our Integration tests using Pre-Generated IDs approach. https://github.com/magento-engcom/msi/wiki/The-first-step-towards-pre-generated- IDs.-And-how-this-will-improve-your-Integration-tests
  60. 60. © 2017 Magento, Inc. Page | 60 Be Responsible or train your dog tests to be!
  61. 61. © 2017 Magento, Inc. Page | 61 How to join us? Send an email to engcom@magento.com @iminyaylo Thank y’all!

×