Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

TDD - Test Driven Development
A quick intro in TDD with focus on PHP
tehlug, Azar 1392 (Dec 2013)
Why test? 
R U KIDDING ME?
Old style tests
  • Coverage is always lower than expected
  • Writing tests is a nightmare!
  • Hard to debug
 if you had a code without test, start to write test today, tomorrow is late :)
TDD - Test Driven Development
  • Write test (or additional test)
  • Make sure all are failed
  • Write code to pass them, even dirty :) code
  1. KISS (Keep it simple stupid)
  2. YAGNI (You Arenot Gonna Need It)
  3.  fake it til you make it
  • Refactor  the code
  • Jump to step 1 again

Benefits :)
  • Best coverage, near 100%
  • Write test is easy, since we write them first!!
  • Less debug required
  • Better understanding of the code
  • Design is done in writing tests
  • More flexible and readable code
A bunch of should and should not
    Write the simplest code to pass the test
Don’t introduce dependencies between tests. Test should pass when run in any order.
Use mock objects to test code at system boundaries (e.g. database, container,
 file system) so that tests run fast. (What is a mock??  Thanks wiki :) )
Only write new code when a test is failing. Each test should test 
new/different behavior.
Acceptance!
a wild custommer appeared
What about this dots? 
Domain-specific language like Cucumbers
To accept or not to accept
BDD*
* I promise to have bdd here
PHPunit
A simple demonstration with PHPunit and Composer
PHPUnit is the de-facto standard for unit testing in PHP projects. It provides both a framework that makes the writing
of tests easy as well as the functionality to easily run the tests and analyse their results.

Composer
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project
needs and it will install them in your project for you.

Definition: 
A simple class with two method:

  1. Min ($a, $b);
  2. Max ($a, $b);

Its easy :) I know.

Create a new composer project

{
    "name": "fzerorubigd/tehlog-test",
    "description": "TDD demonstration",
    "authors": [
        {
            "name": "fzerorubigd",
            "email": "[email protected]"
        }
    ],
    "require": {
    },
        "require-dev": {
        "phpunit/phpunit":
        "~3.7"
        },
    "config": {
        "bin-dir": "bin/"
    },
    "autoload": {
        "psr-0": {
            "Cybits": "src/"
        }
    }
}

then run the composer :

composer install --dev




<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="tests/bootstrap.php" colors="true">
    <testsuites>
        <testsuite name="TDD example test suite">
            <directory>tests/</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory suffix=".php">src/</directory>
        </whitelist>
    </filter>
</phpunit>

phpunit.xml.dist




<?php

class ExampleTest extends PHPUnit_Framework_TestCase
{

    public function testMin()
    {
        $obj = new \Cybits\Example();
        $this->assertEquals(10, $obj->min(10, 100));
    }

    public function testMax()
    {
        $obj = new \Cybits\Example();
        $this->assertEquals(100, $obj->max(10, 100));
    }
}
tests/ExampleTest.php




<?php

namespace Cybits;

class Example
{
    public function min($a, $b)
    {
        return 10;
    }

    public function max($a, $b)
    {
        return 100;
    }
} 
I know, this is the most idiotic way, but it pass the tests!
Wow!! JUST pass the tests :)




f0rud@elbix:/home/f0rud/testSlides/php git:(master*) $ ./bin/phpunit
    PHPUnit 3.7.28 by Sebastian Bergmann.

    Configuration read from /home/f0rud/testSlides/php/phpunit.xml.dist

    ..

    Time: 25 ms, Memory: 3.00Mb

    OK (2 tests, 2 assertions)
First pass




<?php

class ExampleTest extends PHPUnit_Framework_TestCase
{

    public function testMin()
    {
        $obj = new \Cybits\Example();
        $this->assertEquals(10, $obj->min(10, 100));
        $this->assertEquals(5, $obj->min(10, 5));
        $this->assertEquals(1, $obj->min(1, 100));
    }

    public function testMax()
    {
        $obj = new \Cybits\Example();
        $this->assertEquals(100, $obj->max(10, 100));
        $this->assertEquals(70, $obj->max(11, 70));
        $this->assertEquals(20, $obj->max(10, 20));
    }
}
Add more tests




<?php

namespace Cybits;

class Example
{
    public function min($a, $b)
    {
        return min($a, $b);
    }

    public function max($a, $b)
    {
        return max($a, $b);
    }
} 
The final code :))
Who Am I?
Bita's Dad :) 
Developer @ Rasa-Web
Questions?

Use a spacebar or arrow keys to navigate