international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
-
Upload
smuellersandsmedia -
Category
Technology
-
view
770 -
download
0
Transcript of international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
![Page 2: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/2.jpg)
Me, myself & I
JavaScript since 2002
PHP since 2001
Trainer & coach
Opensource addict
PHP manual translations
FluentDOM
...
![Page 3: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/3.jpg)
CLI
![Page 4: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/4.jpg)
… on the command line
-- testdox[-(html|text)]generates a especially styled 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
-- filter <pattern>filters which testsuite to run.
![Page 5: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/5.jpg)
… on the command line (cont.)
-- strictmarks test without an assertion as incomplete. Use in combination with –verbose to get the name of the test.
-- 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)
![Page 6: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/6.jpg)
Annotations
![Page 7: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/7.jpg)
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 )
![Page 8: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/8.jpg)
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 ) { }}
![Page 9: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/9.jpg)
Annotations (cont.)
Depending on other tests
public function testIsApcAvailable() {
if ( ! extension_loaded( 'apc' ) ) { $this->markTestSkipped( 'Required APC not available' ); } }
/** * @depend testIsApcAvailable */public function testGetFileFromAPC () {
}
![Page 10: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/10.jpg)
Assertions
![Page 11: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/11.jpg)
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)
![Page 12: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/12.jpg)
Assertions (cont.)
assertContains(), assertContainsOnly()
Cameleon within the asserts, handles
Strings ( like strpos() )
Arrays ( like in_array() )
$this->assertContains('bar', 'foobar'); // ✓
$this->assertContainsOnly('string', array('1', '2', 3)); // ✗
![Page 13: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/13.jpg)
Assertions (cont.)
assertXMLFileEqualsXMLFile()
assertXMLStringEqualsXMLFile()
assertXMLStringEqualsXMLString()
$this->assertXMLFileEqualsXMLFile( '/path/to/Expected.xml', '/path/to/Fixture.xml');
![Page 14: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/14.jpg)
Assertions (cont.)
$ phpunit XmlFileEqualsXmlFileTest.phpPHPUnit 3.4.15 by Sebastian Bergmann.…
1) XmlFileEqualsXmlFileTest::testFailureFailed 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
![Page 15: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/15.jpg)
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 myClass );
$this->assertAttributeContains( 'ko', 'name', new myClass );
![Page 16: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/16.jpg)
Assertions (cont.)
assertType()
// TYPE_OBJECT$this->assertType( 'FluentDOM', new FluentDOM );
$this->assertInstanceOf( 'FluentDOM', new FluentDOM );
// TYPE_STRING$this->assertInternalType( 'string', '4221' );
// TYPE_INTEGER$this->assertInternalType( 'integer', 4221 );
// TYPE_RESSOURCE$this->assertInternalType( 'resource', fopen('/file.txt', 'r' );
![Page 17: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/17.jpg)
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);
![Page 18: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/18.jpg)
Assertions (cont.)
assertThat()
Evaluates constraints to build complex assertions.
$this->assertThat( $expected, $ths->logicalAnd( $this->isInstanceOf('tire'), $this->logicalNot( $this->identicalTo($myFrontTire) ) ));
![Page 19: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/19.jpg)
Weaving in
![Page 20: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/20.jpg)
Test Listeners
Get called on several states of the test runner
startTest
endTest
addIncompleteTest
…
![Page 21: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/21.jpg)
Test Listeners (cont.)
Configuration
Add to your phpunit.xml
<listeners> <listener class="myListener" file="PATH/TO/YOUR/CODE"> <arguments> <string>build/log</string> </arguments> </listener></listeners>
![Page 22: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/22.jpg)
Test Listeners (cont.)
Implementation example
class ListenerLog implements PHPUnit_Framework_TestListener{ public function __construct($path) { $this->path = $path; }
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) { $this->log("Test suite '%s' precesed.\n", $test->getName()); }
}
![Page 23: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/23.jpg)
Specialities
![Page 24: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/24.jpg)
Special tests
Testing exceptions
@expectedException
/** * @expectedException InvalidArgumentException */public function testInvalidArgumentException() {
$obj = new myClass(); $obj->run('invalidArgument');
}
![Page 25: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/25.jpg)
Special tests (cont.)
Testing exceptions
setExpectedException( 'Exception' )
for internal use only!!
don't use it directly any more.
![Page 26: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/26.jpg)
Special tests (cont.)
Testing exceptions
try/catch
Does not work when using --strict switch
public function testInvalidArgumentException() {
$obj = new myClass(); try{ $obj->run( 'invalidArgument' ); $this->fail('Expected exception not thrown.'); } catch ( InvalidArgumentException $e ) { }}
![Page 27: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/27.jpg)
Special tests (cont.)
public function callbackGetObject($name, $className = '') { retrun strtolower($name) == 'Jakob';}
[…]
$application = $this->getMock('FluentDOM');$application ->expects($this->any()) ->method('getObject') ->will( $this->returnCallback( array($this, 'callbackGetObject') ) );[…]
![Page 28: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/28.jpg)
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) ) );
[…]
![Page 29: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/29.jpg)
Special tests (cont.)
implicit integration tests
public function testGet() {
$mock = $this->getMock( 'myAbstraction' ); $mock ->expected( $this->once() ) ->method( 'method' ) ->will( $this->returnValue( 'return' );}
![Page 31: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/31.jpg)
Slides'n contact
Please comment the talk on joind.in
http://joind.in/3518Slides
http://slideshare.net/lapistano Email:
![Page 32: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/32.jpg)
PHP5.3 Powerworkshop
New features of PHP5.3
Best Pratices using OOP
PHPUnit
PHPDocumentor
![Page 33: international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit](https://reader033.fdocuments.in/reader033/viewer/2022052823/5550c724b4c905f2318b4651/html5/thumbnails/33.jpg)
License
This set of slides and the source code included in the download package is licensed under the
Creative Commons Attribution-Noncommercial-Share Alike 2.0 Generic License
http://creativecommons.org/licenses/by-nc-sa/2.0/deed.en