Intro to TDD Using
PHPUnit: A FizzBuzz
Kata
2015 January 7
Kansas City PHP User Group
TDD is Stupid
• Writing tests takes time
• I’d rather be coding
• No way I can test everything
TDD is Stupid Smart
• Reduces time spent in debugger
• Reduces var_dump and bogus echo statements
• Reduces fear of change
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.
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
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!
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
Enough Talk. Begin!
• Add the following to composer.json:
• "require-dev": {
"phpunit/phpunit": “~4.4"
}
• Set up environment via Composer:
• $ composer install
Test Class
• Create FizzBuzzTest.php
• <?php
include 'vendor/autoload.php';
class FizzBuzzTest extends 
PHPUnit_Framework_TestCase
{
}
Test, then Fix
• run $ vendor/phpunit/phpunit/phpunit —colors
FizzBuzzTest.php
• Errors!
• Fix ‘em.
• Test again.
Demonstration
Now, go and study
• PHPUnit:
• Documentation: https://phpunit.de/documentation.html
• Presentations: https://phpunit.de/presentations.html
• Grumpy Programmer Books:
• The Grumpy Programmer's PHPUnit Cookbook: https://leanpub.com/grumpy-phpunit
• The Grumpy Programmer's Guide To Building Testable PHP Applications:
https://leanpub.com/grumpy-testing
• Kata:
• 12 TDDs of Christmas: http://www.wiredtothemoon.com/2012/12/12-tdds-of-christmas/
• http://cyber-dojo.org/
• http://www.codewars.com/
Thank you
Eric Poe
eric@ericpoe.com
@eric_poe
Please rate this talk!
https://joind.in/13192

Intro to TDD Using PHPUnit: A FizzBuzz Kata

  • 1.
    Intro to TDDUsing 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 StupidSmart • Reduces time spent in debugger • Reduces var_dump and bogus echo statements • Reduces fear of change
  • 4.
    Easiest TDD DealsWith 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 • Kataare 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 starta 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 • CreateFizzBuzzTest.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.
  • 11.
  • 12.
    Now, go andstudy • PHPUnit: • Documentation: https://phpunit.de/documentation.html • Presentations: https://phpunit.de/presentations.html • Grumpy Programmer Books: • The Grumpy Programmer's PHPUnit Cookbook: https://leanpub.com/grumpy-phpunit • The Grumpy Programmer's Guide To Building Testable PHP Applications: https://leanpub.com/grumpy-testing • Kata: • 12 TDDs of Christmas: http://www.wiredtothemoon.com/2012/12/12-tdds-of-christmas/ • http://cyber-dojo.org/ • http://www.codewars.com/
  • 13.
    Thank you Eric Poe eric@ericpoe.com @eric_poe Pleaserate this talk! https://joind.in/13192