PHPunit and you

3,639 views
3,403 views

Published on

PHPUnit and CakePHP new features and options. Given at CakeFest 2011

Published in: Technology, Education

PHPunit and you

  1. 1. PHPUnit & You
  2. 2. PHPUnit?PHPUnit is a test suite framework.Pretty much the standard in PHP. Used by: Zend Framework Symfony ezComponents and many many more.Fully featured (more later).
  3. 3. Improvements over SimpleTestBetter command line tools.Better coverage reports.Better selection of assertions.Better mock objects.Better tool chain support.Pretty much all win.
  4. 4. Assertions
  5. 5. AssertionsAssertions are stricter in PHPUnit.More assertions to use.Better reporting on failureMore consistent interface.Easily extensible.
  6. 6. Stricter1 <?php2 $this->assertFalse(array()); // FAIL3 $this->assertNull(false); // FAIL4 $this->assertTrue(1); // FAIL5 $this->assertTrue(oh noes); // FAIL
  7. 7. Moar 1 <?php 2 // General 3 $this->assertContains(some, Awesome); 4 $this->assertEmpty(array()); 5 6 // files 7 $this->assertFileExists(/path/to/file); 8 $this->assertFileEquals(/path/to/expected, /path/to/actual); 910 //objects11 $this->assertInstanceOf(Controller, $thing);
  8. 8. Better failures 1 1) MyTest::makeSureStuffIsTheSame 2 Failed asserting that two strings are equal. 3 --- Expected 4 +++ Actual 5 @@ @@ 6 This is some 7 -text 8 -on multiple lines 9 +words10 +on11 +a second line
  9. 9. Consistent1 <?php2 // Always expected, result3 $this->assertEquals($expect, $result, $message);4 $this->assertSame($expect, $result, $message);5 $this->assertContains($expect, $result, $message);6 $this->assertInstanceOf($expect, $result, $message);7 $this->assertRegExp($pattern, $result, $message);8 $this->assertTrue($result, $message);9 $this->assertFalse($result, $message);
  10. 10. Extensible1 <?php2 // Create new assertions using the built-in ones.3 function assertJsonEquals($expected, $result, $message = null) {4 $this->assertEquals(5 json_encode($expected),6 json_encode($result),7 $message8 );9 }
  11. 11. Annotations
  12. 12. AnnotationsDoc block comment tags that do stuff!Allows for declarative tests.Test generators.Buffering.
  13. 13. Testing exceptions 1 <?php 2 /** 3 * Test to make sure ohNo() explodes 4 * 5 * @expectedException RuntimeException 6 * @expectedExceptionMessage On noes. 7 * @expectedExceptionCode 25 8 */ 9 function testSomething() {10 $this->Thing->ohNo();11 }
  14. 14. Generators 1 <?php 2 public static function dataGenerator() { 3 return array( 4 array(1, 1, 2), 5 array(2, 2, 4) 6 ); 7 } 8 /** 9 * Adding stuff up10 *11 * @dataProvider dataGenerator12 */13 function testAdding($a, $b, $expected) {14 $this->assertEquals($expected, $a + $b);15 }
  15. 15. Output buffering 1 <?php 2 /** 3 * Check saying hello. 4 * 5 * @ouputBuffering enabled 6 */ 7 function testHello() { 8 $this->Helper->hello(Sam); 9 $this->assertEquals(Hello Sam, ob_get_contents());10 }
  16. 16. Fixtures
  17. 17. FixturesThey are the same!No really, they are totally the same as before.
  18. 18. Mock Objects
  19. 19. Mock objectsBetter built, and a better interface.More expressive and powerful.Works with exceptions.Extensible.
  20. 20. Create a stub 1 <?php 2 function testProcess() { 3 $mock = $this->getMock(AuthorizeNet, process); 4 $mock->expects($this->once()) 5 ->method(process) 6 ->with($this->Order) 7 ->will($this->returnValue(Order successful)); 8 9 $this->Order->setGateway($mock);10 $this->Order->process();11 } Stub out expensive resources during testing.
  21. 21. Break stuff 1 <?php 2 function testProcessFailure() { 3 $mock = $this->getMock(AuthorizeNet, array(process)); 4 $mock->expects($this->at(2)) 5 ->method(process) 6 ->with($this->Order) 7 ->will($this->throwException( 8 new GatewayOrderFailedException() 9 ));1011 $this->Order->setGateway($mock);12 $this->Order->process();13 } Simulate failures that are hard to test.
  22. 22. Safety check1 <?php2 $response = $this->getMock(CakeResponse);3 $response->expects($this->once())4 ->method(header)5 ->with(Location: /posts);6 $this->Controller->response = $response;7 $this->Controller->myMethod(); Check objects are calling others correctly.
  23. 23. Strategies for mocksConstructor injection.Setter injection.You’ll need ways of getting mocks into your code.
  24. 24. Code coverage
  25. 25. Code coverageHow much of your code gets run in your tests.Requires xDebug.Comes in several formats: Clover format. Comprehensive HTML report. Single test reports.
  26. 26. Clover reportsClover reports. XML le that works well withJenkins/Hudson.Console/cake testsuite --coverage-clover clover.xml app AllTests
  27. 27. Complete HTML report Full detailed report for your entire project. Slower to generate, but more complete.Console/cake testsuite --coverage-html ./webroot/coverage app AllTests
  28. 28. ¡Sample/Demo!
  29. 29. Individual reportsSingle test results, shows coverage for each testcase/group.Generated at run time.Faster than comprehensive report.
  30. 30. ¡Sample/Demo!
  31. 31. RunnersCommand line runner.Web runner.Some con gurations have troubles runningsessions in cli. But its a con g option.Main difference is how you use them.
  32. 32. Demo time!
  33. 33. Continuous integrationCakePHP plays nice with Jenkins, and other CIservers.Run your tests all the time.Automate everything.
  34. 34. DEMO!
  35. 35. Questions?

×