Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Php day2010

on

  • 2,114 views

"Listen to your tests" on steroids

"Listen to your tests" on steroids

Statistics

Views

Total Views
2,114
Views on SlideShare
1,850
Embed Views
264

Actions

Likes
3
Downloads
14
Comments
0

4 Embeds 264

http://www.giorgiosironi.com 147
http://giorgiosironi.blogspot.com 74
http://www.slideshare.net 41
http://giorgiosironi.blogspot.in 2

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Php day2010 Php day2010 Presentation Transcript

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