1. Intro to TDD Using
PHPUnit: A FizzBuzz
Kata
2015 January 7
Kansas City PHP User Group
2. TDD is Stupid
• Writing tests takes time
• I’d rather be coding
• No way I can test everything
3. TDD is Stupid Smart
• Reduces time spent in debugger
• Reduces var_dump and bogus echo statements
• Reduces fear of change
4. Easiest TDD Deals With
Promises
• All tests start with a descriptive name, the promise:
• ex. testEvenNumbersAreDivisibleBy2
• All tests end with $this->assertSomething
• The assert is the final statement of the proof.
• ex.
When Even Numbers Are Divisible By 2,
Assert that (EvenNumber % 2) == 0
Assert that (OddNumber % 2) == 1.
5. TDD Kata
• Kata are are short series of movements repeated enough to be
done without thought — migrating intention into muscle-memory.
• Learn TDD concepts via kata
• True TDD kata are done, discarded, and repeated.
• Non-TDD-Kata Examples:
• Karate Kid III - Kata Scene
• Rika Usami of Japan. 宇佐美 里香。空手 - Final Female Kata
6. Essentials
• To start a TDD Kata using PHPUnit, you need:
• composer.json
• a KataClassTest.php
• a KataClass.php
• You do NOT need to implement the KataClass!
7. Fizz Buzz Kata
• Rules:
• Given a series of numbers, replace certain numbers with
a string.
• Numbers divisible by 3 are replaced by “Fizz”
• Numbers divisible by 5 are replaced by “Buzz”
• Numbers divisible by (3 and 5) are replaced by
“FizzBuzz”
• All other numbers are unchanged
8. Enough Talk. Begin!
• Add the following to composer.json:
• "require-dev": {
"phpunit/phpunit": “~4.4"
}
• Set up environment via Composer:
• $ composer install
9. Test Class
• Create FizzBuzzTest.php
• <?php
include 'vendor/autoload.php';
class FizzBuzzTest extends
PHPUnit_Framework_TestCase
{
}
10. Test, then Fix
• run $ vendor/phpunit/phpunit/phpunit —colors
FizzBuzzTest.php
• Errors!
• Fix ‘em.
• Test again.