Share
Embed
Transcript
15 slides
-
Agenda » Basic Unit Testing » PHPUnit and Composer »
test Double (Fake/Spy/Stub/Mock) » Database Test » TDD in Webapp development -
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 !! -
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 -
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 -
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? -
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" } } -
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]==")") { .... } } -
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 -
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 -
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()); } } ?> -
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