Unit Testing and PHP

Robert Ames

Types of testing

Why Test?



What to test?

What not to test?

Why to test?

How to test?

What it looks like...

A Good Test is...

Bad Test

function testChargeBad() {
    $x = new BusinessAPI();
    $before = $x->getBalance( 'testUser' );
    $x->addCharge( 'testUser', 10 );
    $after = $x->getBalance( 'testUser' );

    // this duplicates business logic in your test
    assert( 10 == ($after - $before) );
}
    

Better Test

function testChargeBetter() {
    $x = new BusinessAPI();
    $x->setBalance( 'testUser', 100 );
    $x->addCharge( 'testUser', 10 );
    $res = $x->getBalance( 'testUser' );

    // this test is declarative, and can run multiple times
    assert( 110 == $res );
}
    

TDD - Test Driven Development

TDD - Dominoes

You need to implement a feature

  1. Prove that it isn't already implemented.
  2. How would you prove that it works?

TDD - Dominoes

TDD - Dominoes

TDD - Dominoes

TDD - Now write the code

TDD - Dominoes

TDD - Dominoes

TDD - Dominoes

TDD - Now write more code


  class BOARD {


    function play( $dom )
    {



    }
  }

Unit Testing

Tactical Goals

In the short term, focus on these:

Strategic Goals

Longer term benefits:

Resources