Getting started with TDD - Confoo 2014

  • 993 views
Uploaded on

Introduction to Test Driven Development. I gave that talk on February 2014 at Confoo.

Introduction to Test Driven Development. I gave that talk on February 2014 at Confoo.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
993
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
8
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Getting Started With TDD Eric Hogue - @ehogue Confoo - 2014-02-27
  • 2. TDD
  • 3. Where should I Start?
  • 4. 1. Unit tests 2. Test Driven Development 3. What’s next?
  • 5. Unit Tests
  • 6. Unit Test a method by which individual units of source code [...] are tested to determine if they are fit for use http://en.wikipedia.org/wiki/Unit_testing
  • 7. Don’t Cross boundaries
  • 8. Tools ● ● ● ● SimpleTest atoum PHPT PHPUnit
  • 9. Installation - Phar $ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ mv phpunit.phar /usr/local/bin/phpunit
  • 10. Installation - Pear pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit
  • 11. Installation - Composer # composer.json { "require-dev": { "phpunit/phpunit": "3.7.*" } } $ composer install
  • 12. PHPUnit FactorialTest.php <?php class FactorialTest extends PHPUnit_Framework_TestCase { }
  • 13. public function testSomething() { } /** @test */ public function somethingElse() { }
  • 14. ● Arrange ● Act ● Assert
  • 15. Arrange /** @test */ public function factOf1() { $factorial = new Factorial; }
  • 16. Act /** @test */ public function factOf1() { $factorial = new Factorial; $result = $factorial->fact(1); }
  • 17. Assert /** @test */ public function factOf1() { $factorial = new Factorial; $result = $factorial->fact(1); $this->assertSame(1, $result); }
  • 18. PHPUnit Assertions ● ● ● ● ● ● ● $this->assertTrue(); $this->assertEquals(); $this->assertSame(); $this->assertContains(); $this->assertNull(); $this->assertRegExp(); ...
  • 19. Preparing For Your Tests setup() -> Before every tests teardown() -> After every tests setUpBeforeClass() + tearDownAfterClass() Once per test case
  • 20. phpunit.xml <phpunit bootstrap="bootstrap.php" colors="true" strict="true" verbose="true" > ... </phpunit>
  • 21. phpunit.xml <phpunit> <testsuites> <testsuite name="My Test Suite"> <directory>path</directory> <file>path</file> <exclude>path</exclude> </testsuite> </testsuites> </phpunit>
  • 22. TDD
  • 23. Red - Green - Refactor Red Write a failing test
  • 24. Red - Green - Refactor Green Make it pass
  • 25. Red - Green - Refactor Refactor Fix any shortcuts you took
  • 26. /** @test */ public function create() { $this->assertNotNull(new Factorial); }
  • 27. class Factorial { }
  • 28. /** @test */ public function factOf1() { $facto = new Factorial; $this->assertSame(1, $facto->fact(1)); }
  • 29. public function fact($number) { return 1; }
  • 30. Duplication public function create() { $this->assertNotNull(new Factorial); } public function factOf1() { $facto = new Factorial; ...
  • 31. public function setup() { $this->facto = new Factorial; } /** @test */ public function factOf1() { $this->assertSame(1, $this->facto->fact(1)); }
  • 32. /** @test */ public function factOf2() { $this->assertSame(2, $this->facto->fact(2)); }
  • 33. public function fact($number) { return $number; }
  • 34. More duplication /** @test */ public function factOf1() { $this->assertSame(1, $this->facto->fact(1)); } /** @test */ public function factOf2() { $this->assertSame(2, $this->facto->fact(2)); }
  • 35. public function factDataProvider() { return array( array(1, 1), array(2, 2), ); }
  • 36. /** * @test * @dataProvider factDataProvider */ public function factorial($number, $expected) { ...
  • 37. … $result = $this->facto->fact($number); $this->assertSame($expected, $result); }
  • 38. public function factDataProvider() { … array(2, 2), array(3, 6), ...
  • 39. public function fact($number) { if ($number < 2) return 1; return $number * $this->fact($number - 1); }
  • 40. It’s a lot of work
  • 41. Dependencies
  • 42. Problems class Foo { public function __construct() { $this->bar = new Bar; } }
  • 43. Dependency Injection
  • 44. Setter Injection class Foo { public function setBar(Bar $bar) { $this->bar = $bar; } public function doSomething() { // Use $this->bar } }
  • 45. Constructor Injection class Foo { public function __construct( Bar $bar) { $this->bar = $bar; } public function doSomething() { // Use $this->bar } }
  • 46. Pass the dependency directly class Foo { public function doSomething( Bar $bar) { // Use $bar } }
  • 47. File System
  • 48. vfsStream Virtual Files System composer.json "require-dev": { "mikey179/vfsStream": "*" },
  • 49. Check if a folder was created $root = vfsStream::setup('dir'); $parentDir = $root->url('dir'); //Code creating sub folder $SUT->createDir($parentDir, 'test'); $this->assertTrue( $root->hasChild('test'));
  • 50. Reading a file $struct = array( 'subDir' => array('test.txt' => 'content') ); $root = vfsStream::setup('root', null, $struct); $parentDir = $root->url('root'); ...
  • 51. Reading a file … $content = file_get_contents( $parentDir . '/subDir/test.txt'); $this->assertSame('content', $content);
  • 52. Databases
  • 53. Mocks Replaces a dependency ● PHPUnit mocks ● Mockery ● Phake
  • 54. Creation $mock = $this->getMock('NSClass');
  • 55. Creation $mock = $this->getMock('NSClass'); Or $mock = $this->getMockBuilder ('NamespaceClass') ->disableOriginalConstructor() ->getMock();
  • 56. $mock->expects($this->once()) ->method('methodName')
  • 57. $mock->expects($this->once()) ->method('methodName') ->with(1, 'aa', $this->anything())
  • 58. $mock->expects($this->once()) ->method('methodName') ->with(1, 'aa', $this->anything()) ->will($this->returnValue(10));
  • 59. Mocking PDO $statement = $this->getMockBuilder ('PDOStatement') ->getMock(); $statement->expects($this->once()) ->method('execute') ->will($this->returnValue(true)); ...
  • 60. ... $statement->expects($this->once()) ->method('fetchAll') ->will( $this->returnValue( array(array('id' => 123)) ) ); ...
  • 61. $this->getMockBuilder('PDO') ->getMock();
  • 62. … $pdo = $this->getMockBuilder( 'stdClass') ->setMethods(array('prepare')) ->getMock(); $pdo->expects($this->once()) ->method('prepare') ->will( $this->returnValue($statement));
  • 63. class PDOMock extends PDO { public function __construct() {} } $pdo = $this->getMockBuilder ('PDOMock') ->getMock();
  • 64. mysql_*
  • 65. DbUnit Extension extends PHPUnit_Extensions_Database_TestCase public function getConnection() { $pdo = new PDO('sqlite::memory:'); return $this-> createDefaultDBConnection( $pdo, ':memory:'); }
  • 66. public function getDataSet() { return $this-> createFlatXMLDataSet('file'); }
  • 67. API
  • 68. ● Wrap all call into a class ○ ZendHttp ○ Guzzle ○ Simple class that uses curl ● Mock the class ○ Return the wanted xml/json
  • 69. Pros and Cons
  • 70. Pros ● Less regressions
  • 71. Pros ● Less regressions ● Trust
  • 72. Pros ● Less regressions ● Trust ● Low coupling
  • 73. Pros ● ● ● ● Less regressions Trust Low coupling Simple Design
  • 74. Cons ● Takes longer
  • 75. “If it doesn't have to work, I can get it done a lot faster!” - Kent Beck
  • 76. Cons ● Takes longer ● Can be hard to sell to managers
  • 77. Cons ● Takes longer ● Can be hard to sell to managers ● It’s hard
  • 78. Prochaines étapes?
  • 79. Continuous Testing - Guard
  • 80. Continuous Integration
  • 81. Continuous Integration ● Run your tests automatically ○ ○ ○ ○ Unit Tests Acceptance Tests Performance Tests ...
  • 82. Continuous Integration ● Run your tests automatically ○ ○ ○ ○ Unit Tests Acceptance Tests Performance Tests … ● Check Standards ○ phpcs
  • 83. Continuous Integration ● Run your tests automatically ○ ○ ○ ○ Unit Tests Acceptance Tests Performance Tests … ● Check Standards ○ phpcs ● Check for "code smells" ○ phpcpd ○ PHP Depend ○ PHP Mess Detector
  • 84. Questions Twitter: @ehogue Blog: http://erichogue.ca/ Slides: http://www. slideshare.net/EricHogue
  • 85. Credits ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Paul - http://www.flickr.com/photos/pauldc/4626637600/in/photostream/ JaseMan - http://www.flickr.com/photos/bargas/3695903512/ mt 23 - http://www.flickr.com/photos/32961941@N03/3166085824/ Adam Melancon - http://www.flickr.com/photos/melancon/348974082/ Zhent_ - http://www.flickr.com/photos/zhent/574472488/in/faves-96579472@N07/ Ryan Vettese - http://www.flickr.com/photos/rvettese/383453435/ shindoverse - http://www.flickr.com/photos/shindotv/3835363999/ Eliot Phillips - http://www.flickr.com/photos/hackaday/5553713944/ World Bank Photo Collection - http://www.flickr.com/photos/worldbank/8262750458/ Steven Depolo - http://www.flickr.com/photos/stevendepolo/3021193208/ Deborah Austin - http://www.flickr.com/photos/littledebbie11/4687828358/ tec_estromberg - http://www.flickr.com/photos/92334668@N07/11122773785/ nyuhuhuu - http://www.flickr.com/photos/nyuhuhuu/4442144329/ Damián Navas - http://www.flickr.com/photos/wingedwolf/5471047557/ Improve It - http://www.flickr.com/photos/improveit/1573943815/