phpunit and you

35
PHPUnit & You

Upload: markstory

Post on 11-May-2015

2.992 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: PHPunit and you

PHPUnit & You

Page 2: PHPunit and you

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

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

Assertions

Page 5: PHPunit and you

Assertions

Assertions are stricter in PHPUnit.

More assertions to use.

Better reporting on failure

More consistent interface.

Easily extensible.

Page 6: PHPunit and you

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

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

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

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

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

Annotations

Page 12: PHPunit and you

Annotations

Doc block comment tags that do stuff!

Allows for declarative tests.

Test generators.

Buffering.

Page 13: PHPunit and you

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

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

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

Fixtures

Page 17: PHPunit and you

Fixtures

They are the same!

No really, they are totally the same as before.

Page 18: PHPunit and you

Mock Objects

Page 19: PHPunit and you

Mock objects

Better built, and a better interface.

More expressive and powerful.

Works with exceptions.

Extensible.

Page 20: PHPunit and you

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

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

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

Strategies for mocks

Constructor injection.

Setter injection.

You’ll need ways of getting mocks into your code.

Page 24: PHPunit and you

Code coverage

Page 25: PHPunit and you

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

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

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

¡Sample/Demo!

Page 29: PHPunit and you

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

¡Sample/Demo!

Page 31: PHPunit and you

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

Demo time!

Page 33: PHPunit and you

Continuous integration

CakePHP plays nice with Jenkins, and other CI servers.

Run your tests all the time.

Automate everything.

Page 34: PHPunit and you

DEMO!

Page 35: PHPunit and you

Questions?