Download - PHPunit and you
![Page 1: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/1.jpg)
PHPUnit & You
![Page 2: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/2.jpg)
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).
![Page 3: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/3.jpg)
Improvements over SimpleTest
Better command line tools.
Better coverage reports.
Better selection of assertions.
Better mock objects.
Better tool chain support.
Pretty much all win.
![Page 4: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/4.jpg)
Assertions
![Page 5: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/5.jpg)
Assertions
Assertions are stricter in PHPUnit.
More assertions to use.
Better reporting on failure
More consistent interface.
Easily extensible.
![Page 6: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/6.jpg)
Stricter
1 <?php2 $this->assertFalse(array()); // FAIL3 $this->assertNull(false); // FAIL4 $this->assertTrue(1); // FAIL5 $this->assertTrue('oh noes'); // FAIL
![Page 7: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/7.jpg)
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'); 9 10 //objects11 $this->assertInstanceOf('Controller', $thing);
![Page 8: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/8.jpg)
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
![Page 9: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/9.jpg)
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);
![Page 10: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/10.jpg)
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 }
![Page 11: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/11.jpg)
Annotations
![Page 12: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/12.jpg)
Annotations
Doc block comment tags that do stuff!
Allows for declarative tests.
Test generators.
Buffering.
![Page 13: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/13.jpg)
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 }
![Page 14: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/14.jpg)
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 up 10 *11 * @dataProvider dataGenerator12 */13 function testAdding($a, $b, $expected) {14 $this->assertEquals($expected, $a + $b);15 }
![Page 15: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/15.jpg)
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 }
![Page 16: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/16.jpg)
Fixtures
![Page 17: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/17.jpg)
Fixtures
They are the same!
No really, they are totally the same as before.
![Page 18: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/18.jpg)
Mock Objects
![Page 19: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/19.jpg)
Mock objects
Better built, and a better interface.
More expressive and powerful.
Works with exceptions.
Extensible.
![Page 20: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/20.jpg)
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.
![Page 21: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/21.jpg)
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 ));10 11 $this->Order->setGateway($mock);12 $this->Order->process();13 }
Simulate failures that are hard to test.
![Page 22: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/22.jpg)
Safety check
Check objects are calling others correctly.
1 <?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();
![Page 23: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/23.jpg)
Strategies for mocks
Constructor injection.
Setter injection.
You’ll need ways of getting mocks into your code.
![Page 24: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/24.jpg)
Code coverage
![Page 25: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/25.jpg)
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.
![Page 26: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/26.jpg)
Clover reports
Clover reports. XML "le that works well with Jenkins/Hudson.
Console/cake testsuite \--coverage-clover clover.xml \app AllTests
![Page 27: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/27.jpg)
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
![Page 28: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/28.jpg)
¡Sample/Demo!
![Page 29: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/29.jpg)
Individual reports
Single test results, shows coverage for each test case/group.
Generated at run time.
Faster than comprehensive report.
![Page 30: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/30.jpg)
¡Sample/Demo!
![Page 31: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/31.jpg)
Runners
Command line runner.
Web runner.
Some con"gurations have troubles running sessions in cli. But its a con"g option.
Main difference is how you use them.
![Page 32: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/32.jpg)
Demo time!
![Page 33: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/33.jpg)
Continuous integration
CakePHP plays nice with Jenkins, and other CI servers.
Run your tests all the time.
Automate everything.
![Page 34: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/34.jpg)
DEMO!
![Page 35: PHPunit and you](https://reader038.vdocument.in/reader038/viewer/2022102901/5550c7c8b4c905f2318b46b5/html5/thumbnails/35.jpg)
Questions?