Upgrade to Pro — share decks privately, control downloads, hide ads and more …

TDD PHP

Pondd
April 27, 2014

TDD PHP

Pondd

April 27, 2014
Tweet

More Decks by Pondd

Other Decks in Technology

Transcript

  1. Agenda » Basic Unit Testing » PHPUnit and Composer »

    test Double (Fake/Spy/Stub/Mock) » Database Test » TDD in Webapp development
  2. Muscle memory Practice vs Perform The goal is to build

    muscle memory so when the ball comes you will know how to act TDD is more of a mindset Software development is a 'Product' not 'Project', it has a long life cycle A good dev is able to pass the code to readble code to the new guy, not the ability to hack legacy !!
  3. PHP Legacy FTP code style. Developers develop on production. WAMP

    - is ok for beginner but it's an abstraction Since PHP 5.4 - there is a de-couple so dev can run small sever on local dev
  4. Kata Range write in pair [0,5] = {0,1,2,3,4,5} (0,5) =

    {1,2,3,4,5} [0,5) = {0,1,2,3,4} (1,1) = {0} (5,0) = exception
  5. Write Test » 'Test is boring and wasting my time'

    - great fucking programmer » Time pressure-fewer test unstable code more errors-more debugging-More pressure - Are we struggling in this loop?
  6. Composer Tool to manage project lib dependency in JSON Style

    » Packagist - php package archivist $ curl -sS https://getcomposer.org/installer | php $ mv composer.phar /usr/local/bin/composer PHPUnit testing framework for PHP { "require":{ "phpunit/phpunit":"4.0.4" } }
  7. 3 Rules of Test driven development Try same as Kata

    range exercise again with TDD class RangeSpec extends PHPUnit_Framework_TestCase{ function testCloseRangeFromZeroToFive(){ $this->assertEquals("{0,1,2,3,4,5}",rangeZeroToFive("[0,5]");) } } ... function rangeZeroToFive($range){ $openSignPosition = 0; $closeSignPosition = 4; if ($range[openSignPosition]=="("){ return ({0,1,2,3,4,5}); } else if ($range[closeSignPosition]==")") { .... } }
  8. Mock » Fake - Magic Number putting things in just

    because it is required but it is not really matter what those vaules is » Stub - "Fake" clock example » Mock - Similar to stub but also care about Behaviour "Execute database connention only one time » Spy - Usually use with Legacy code. Send spy "object" into private or hard to read method to try to understand it work
  9. TDD Excercise Captcha https://github.com/ountzza/TDD-PHP-captcha » pattern 1 => One +

    1 = 2 » pattern 2 => + Two = 3 » pattern 1 => - 1 = 2 » pattern 2 => * Two = 10
  10. MOCK <?php require_once 'SomeClass.php'; class StubTest extends PHPUnit_Framework_TestCase { public

    function testStub() { // Create a stub for the SomeClass class. $stub = $this->getMock('SomeClass'); // Configure the stub. $stub->expects($this->any()) ->method('doSomething') ->will($this->returnValue('foo')); // Calling $stub->doSomething() will now return // 'foo'. $this->assertEquals('foo', $stub->doSomething()); } } ?>
  11. Controller part silex Steps 1 Update composer file to point

    to Silex 2 composer update 3 create index.php to test if the Silex is up 4 run php -S locahost:8888 5 go to URL http://localhost:8888/hello/Pondd 6 now try to get it to call CaptCha 7 try to split View and Controller