• Save
PhpUnit - The most unknown Parts
Upcoming SlideShare
Loading in...5
×
 

PhpUnit - The most unknown Parts

on

  • 5,390 views

A lot of people using PHPunit for testing their source code. While I was observing my team ...

A lot of people using PHPunit for testing their source code. While I was observing my team
I recognized most of them are only using the standard ssertions like 'assertEquals()' or
'assertTrue()' and are complaining about how hard it is to test the code even when the tests are written first. This talk is about all the stuff not used on a daily basis. It shows you some nice features of PHPUnit and how to use them for your benefit.

Statistics

Views

Total Views
5,390
Views on SlideShare
5,328
Embed Views
62

Actions

Likes
6
Downloads
0
Comments
0

4 Embeds 62

https://twitter.com 30
http://www.berejeb.com 22
http://www.devetdesign.com 6
http://feeds.feedburner.com 4

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PhpUnit - The most unknown Parts PhpUnit - The most unknown Parts Presentation Transcript

  • The most unknown parts ofPHPUnitBastian Feder Confoo 2011 Montrealbastian.feder@liip.ch 11th March 2011
  • … on the command line -- testdox[-(html|text)] -- filter <pattern> generates a especially styled filters which testsuite to run. test report. $ phpunit --filter Handler --testdox ./ PHPUnit 3.4.15 by Sebastian Bergmann. FluentDOMCore [x] Get handler FluentDOMHandler [x] Insert nodes after [x] Insert nodes before
  • … on the command line (cont.) -- stop-on-failure stops the testrun on the first recognized failure. -- coverage-(html|source|clover) <(dir|file)> generates a report on how many lines of the code has how often been executed. -- group <groupname [, groupname]> runs only the named group(s). -- d key[=value] alter ini-settings (e.g. memory_limit, max_execution_time)
  • Assertions „In computer programming, an assertion is a predicate (for example a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place. [...] It may be used to verify that an assumption made by the programmer during the implementation of the program remains valid when the program is executed.. [...]“ (Wikipedia, http://en.wikipedia.org/w/index.php?title=Assertion_(computing)&oldid=382473744)
  • Assertions (cont.) assertContains(), assertContainsOnly() Cameleon within the asserts, handles Strings ( like strpos() ) Arrays ( like in_array() ) $this->assertContains(baz, foobar); $this->assertContainsOnly(string, array(1, 2, 3));
  • Assertions (cont.) assertXMLFileEqualsXMLFile() assertXMLStringEqualsXMLFile() assertXMLStringEqualsXMLString() $this->assertXMLFileEqualsXMLFile( /path/to/Expected.xml, /path/to/Fixture.xml );
  • Assertions (cont.) $ phpunit XmlFileEqualsXmlFileTest.php PHPUnit 3.4.15 by Sebastian Bergmann. … 1) XmlFileEqualsXmlFileTest::testFailure Failed asserting that two strings are equal. --- Expected +++ Actual @@ -1,4 +1,4 @@ <?xml version="1.0"?> <foo> - <bar/> + <baz/> </foo> /dev/tests/XmlFileEqualsXmlFileTest.php:7
  • Assertions (cont.) assertObjectHasAttribute(), assertClassHasAttribute() Overcomes visibilty by using Reflection-API Testifies the existance of a property, not its content $this->assertObjectHasAttribute( myPrivateAttribute, new stdClass() ); $this->assertObjectHasAttribute( myPrivateAttribute, stdClass );
  • Assertions (cont.) assertAttribute*() Asserts the content of a class attribute regardless its visibility […] private $collection = array( 1, 2, 3 ); private $name = Jakob; […] $this->assertAttributeContainsOnly( integer, collection, new FluentDOM ); $this->assertAttributeContains( ko, name, new FluentDOM );
  • Assertions (cont.) assertType() // TYPE_OBJECT $this->assertType( FluentDOM, new FluentDOM ); $this->assertInstanceOf( FluentDOM, new FluentDOM ); // TYPE_STRING $this->assertType( string, 4221 ); // TYPE_INTEGER $this->assertType( integer, 4221 ); // TYPE_RESSOURCE $this->assertType( resource, fopen(/file.txt, r );
  • Assertions (cont.) assertSelectEquals(), assertSelectCount() Assert the presence, absence, or count of elements in a document. Uses CSS selectors to select DOMNodes Handles XML and HTML $this->assertSelectEquals( #myElement, myContent, 3, $xml ); $this->assertSelectCount( #myElement, false, $xml );
  • Assertions (cont.) assertSelectRegExp() $xml = = <items version="1.0"> <persons> <person class="firstname">Thomas</person> <person class="firstname">Jakob</person> <person class="firstname">Bastian</person> </persons> </items> ; $this->assertSelectRegExp( person[class*="name"],(Jakob|Bastian), 2, $xml);
  • Assertions (cont.) assertThat() Evaluates constraints to build complex assertions. $this->assertThat( $expected, $ths->logicalAnd( $this->isInstanceOf(tire), $this->logicalNot( $this->identicalTo($myFrontTire) ) ) );
  • Inverted Assertions Mostly every assertion has an inverted sibling. assertNotContains() assertNotThat() assertAttributeNotSame() …
  • Annotations „In software programming, annotations are used mainly for the purpose of expanding code documentation and comments. They are typically ignored when the code is compiled or executed.“ ( Wikipedia: http://en.wikipedia.org/w/index.php?title=Annotation&oldid=385076084 )
  • Annotations (cont.)/** @covers, @group * @covers myClass::run * @group exceptions * @group Trac-123 */public function testInvalidArgumentException() { $obj = new myClass(); try{ $obj->run( invalidArgument ); $this->fail(Expected exception not thrown.); } catch ( InvalidArgumentException $e ) { }}
  • Annotations (cont.) @dependspublic function testIsApcAvailable() { if ( ! extension_loaded( apc ) ) { $this->markTestSkipped( Required APC not available ); }}/** * @depend testIsApcAvailable */public function testGetFileFromAPC () {}
  • Special tests Testing exceptions @expectedException /** * @expectedException InvalidArgumentException */ public function testInvalidArgumentException() { $obj = new myClass(); $obj->run(invalidArgument); }
  • Special tests (cont.) Testing exceptions setExpectedException( Exception )public function testInvalidArgumentException() { $this->setExpectedException(InvalidArgumentException ) $obj = new myClass(); $obj->run(invalidArgument);}
  • Special tests (cont.) Testing exceptions try/catchpublic function testInvalidArgumentException() { $obj = new myClass(); try{ $obj->run( invalidArgument ); $this->fail(Expected exception not thrown.); } catch ( InvalidArgumentException $e ) { }}
  • Special tests (cont.) public function callbackGetObject($name, $className = ) { retrun (strtolower($name) == Jakob)?: false; } […] $application = $this->getMock(FluentDOM); $application ->expects($this->any()) ->method(getObject) ->will( $this->returnCallback( array($this, callbackGetObject) ) ); […]
  • Special tests (cont.)[…]$application = $this->getMock(FluentDOM);$application ->expects($this->any()) ->method(getObject) ->will( $this->onConsecutiveCalls( array($this, callbackGetObject, $this->returnValue(true), $this->returnValue(false), $this->equalTo($expected) ) );[…]
  • Special tests (cont.) implicit integration testspublic function testGet() { $mock = $this->getMock( myAbstraction, array( method ) ); $mock ->expected( $this->once() ) ->method( methoSd ) ->will( $this->returnValue( return );}
  • Questions@lapistanolapistano@php.net
  • Slidesn contact Please comment the talk on joind.in http://joind.in/2879 http://joind.in/2848 Slides http://slideshare.net/lapistano Email: bastian.feder@liip.ch
  • PHP5.3 Powerworkshop New features of PHP5.3 Best Pratices using OOP PHPUnit PHPDocumentor
  • License  This set of slides and the source code included in the download package is licensed under theCreative Commons Attribution-Noncommercial-Share Alike 2.0 Generic License http://creativecommons.org/licenses/by-nc-sa/2.0/deed.en