Php day2010

1,684 views
1,590 views

Published on

"Listen to your tests" on steroids

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,684
On SlideShare
0
From Embeds
0
Number of Embeds
316
Actions
Shares
0
Downloads
16
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Php day2010

  1. 1. Architettura e testabilità (Architecture and testability) Listen to your tests on steroids Giorgio Sironi
  2. 2. Who am I ● PHP freelancer from 2005 ● Writer for php|architect, DZone ● Perito informatico, undergraduate in Engineering at Politecnico di Milano
  3. 3. This talk ● Maintainability, and why testing ● Various techniques to favor ease of testing AND maintainability ● Slides in English (test d'unità, Legge di Demetra...)
  4. 4. Other talks ● Dependency Injection ● Testare l'ignoto ● Agile software lifecycle
  5. 5. What is maintainability? ● Being ready to change! …but not too much ● Taking the first bullet
  6. 6. Favored by maintainability ● New requirements ● Evolving requirements ● Iterative development ● Long-term maintenance
  7. 7. Kinds of tests ● Unit tests ● Integration tests ● Acceptance (end-to-end) tests and functional tests
  8. 8. Maintainability (1 of 2) SOLID principles (not all of them) ● Single responsibility ● Open/closed ● Dependency Inversion
  9. 9. Maintainability (2 of 2) Loose coupling High cohesion Information hiding Orthogonality ...needed from day 1, like readability and testability
  10. 10. Good but... Good buzzwords, but how do I apply that in practice?
  11. 11. Testing first ● Testing first → Testable code ● Testable code → Maintainable code
  12. 12. Problems with tests ● A test is slow? ● A test is brittle? ● A test is difficult to write?
  13. 13. Solution to problems with tests Change the design to ease testing (listen to your tests)
  14. 14. Test must change too ● Good: test suite ● Bad: no test suite ● Worse: unmaintainable test suite
  15. 15. Examples of listening to the tests ● Small tests => high cohesion ● Unit tests => loose coupling 4 pillars...
  16. 16. 1. Dependency injection ● No new operator outside of Factories ● Easy to change lifecycles and implementations
  17. 17. 1. Dependency Injection (without) <?php class Ferrari { private $_engine; public function __construct() { $this->_engine = new Engine(); } }
  18. 18. 1. Dependency Injection (with) <?php class Ferrari { private $_engine; public function __construct(Engine $engine) { $this->_engine = $engine; } }
  19. 19. 2. Avoid static methods ● Explicit concept ● Orthogonality
  20. 20. 2. Avoid static methods (before) <?php class Collection { public function find($key) { Sorter::sort($this); // ..search stuff } }
  21. 21. 2. Avoid using static methods (after) <?php class Collection { public function find($key) { $this->_sorter->sort($this); // ..search stuff } }
  22. 22. 3. Law of Demeter ● Do not walk the... arrows ● Exceptions: Factories, data structures
  23. 23. 3. Law of Demeter (without) <?php class PC { public function powerOn() { $this->_mobo->cpu->cache ->powerOn(); } }
  24. 24. 3. Law of Demeter (with) <?php class PC { public function powerOn() { foreach ($this->_powered as $item) { /** @var Powered $item */ $item->powerOn(); } } }
  25. 25. 3. Law of Demeter (with) <?php class PC { public function powerOn() { $this->_mobo->powerOn(); } }
  26. 26. 4. Singleton vs. Factory ● Singleton is static, impossible to mock ● Global state ● Hidden dependency
  27. 27. 4. Singleton vs. Factory <?php class Authenticator { public function login($user, $pass) { Db::select(...); } }
  28. 28. 4. Singleton vs. Factory <?php class Authenticator { public function __construct(Db $db) { ... } public function login($user, $pass) { $this->_db->select(...); } }
  29. 29. Be honest ● If you need something, inject it (or inject its factory) ● Tell > Ask > Look Recipe for a clear, clean Api
  30. 30. TDD is... Test-Driven Design
  31. 31. References ● Guide: Writing Testable Code (Misko Hevery) ● Growing Object-Oriented Software, Guided by Tests – Freeman, Price
  32. 32. Contacts ● http://giorgiosironi.blogspot.com ● @giorgiosironi
  33. 33. Questions? Q/A

×