Advanced PHPUnit Topics

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    Theme created by Sakari Koivunen and Henrik Omma Released under the LGPL license.

    9 Favorites

    Advanced PHPUnit Topics - Presentation Transcript

    1. Welcome! Advanced PHPUnit Topics Sebastian Bergmann http://sebastian-bergmann.de/ September 14 th 2007
    2. Who I am
      • Sebastian Bergmann
      • Computer Scientist
      • Involved in the PHP Project since 2000
      • Author, Consultant, Coach, Trainer
      • Developer for eZ Systems AS
    3. Who are you?
      • Your experience with
        • PHP 5?
        • OOP?
        • PHPUnit?
    4. PHPUnit
      • Test Framework
        • Member of the xUnit family of test frameworks
        • Mock Objects
        • DbUnit
      • Integration
        • Selenium RC
        • Bamboo, Bitten, CruiseControl, ...
      • Even More Cool Stuff :-)
        • Code Coverage and Software Metrics
        • Test Database
        • Mutation Testing (GSoC07 project)
    5. Annotations @assert <?php class Calculator { /** * @assert (1, 2) == 3 */ public function add ( $a , $b ) { return $a + $b ; } /** * @assert (2, 1) == 1 */ public function sub ( $a , $b ) { return $a - $b ; } }
    6. Annotations @assert [email_address] ~ % phpunit Calculator PHPUnit 3.2.0 by Sebastian Bergmann. .. Time: 0 seconds OK (2 tests)
    7. Annotations @group <?php class SomeTest extends PHPUnit_Framework_TestCase { /** * @group specification */ public function testSomething () { } /** * @group regresssion * @group bug2204 */ public function testSomethingElse () { } }
    8. Annotations @group [email_address] ~ % phpunit SomeTest PHPUnit 3.2.0 by Sebastian Bergmann. .. Time: 0 seconds OK (2 tests) [email_address] ~ % phpunit --group bug2204 SomeTest PHPUnit 3.2.0 by Sebastian Bergmann. . Time: 0 seconds OK (1 test)
    9. Annotations @test <?php class Specification extends PHPUnit_Framework_TestCase { /** * @test */ public function shouldDoSomething () { } /** * @test */ public function shouldDoSomethingElse () { } }
    10. Annotations @test [email_address] ~ % phpunit --testdox Specification PHPUnit 3.2.0 by Sebastian Bergmann. Specification - Should do something - Should do something else
    11. Test-Level Setup <?php class MyTest extends PHPUnit_Framework_TestCase { protected function setUp () { print &quot; MyTest::setUp()&quot; ; } protected function tearDown () { print &quot; MyTest::tearDown()&quot; ; } public function testOne () { print &quot; MyTest::testOne()&quot; ; } public function testTwo () { print &quot; MyTest::testTwo()&quot; ; } } MyTest::setUp() MyTest::testOne() MyTest::tearDown() MyTest::setUp() MyTest::testTwo() MyTest::tearDown()
    12. Suite-Level Setup <?php require_once 'MyTest.php' ; class MySuite extends PHPUnit_Framework_TestSuite { public static function suite () { return new MySuite ( 'MyTest' ); } protected function setUp () { print &quot; MySuite::setUp()&quot; ; } protected function tearDown () { print &quot; MySuite::tearDown()&quot; ; } } MySuite::setUp() MyTest::setUp() MyTest::testOne() MyTest::tearDown() MyTest::setUp() MyTest::testTwo() MyTest::tearDown() MySuite::tearDown()
    13. Suite-Level Setup <?php require_once 'MyTest.php' ; class MySuite extends PHPUnit_Framework_TestSuite { public static function suite () { return new MySuite ( 'MyTest' ); } protected function setUp () { $this -> sharedFixture = 'something' ; } } Setting up a shared fixture
    14. Suite-Level Setup <?php class MyTest extends PHPUnit_Framework_TestCase { public function testOne () { $this -> assertEquals ( 'something' , $this -> sharedFixture ); } } Using a shared fixture
    15. Testing Object Interaction
      • Tests that only test one thing are more informative than tests where failure can come from many sources
      • How can you isolate your tests from external influences?
      • Simply put, by replacing the expensive, messy, unreliable, slow, complicated resources with stubs that are automatically generated for the purpose of your tests
      • For example, you can replace what is in reality a complicated computation with a constant, at least for the purposes of a single test
      Stubs
    16. Testing Object Interaction
      • Dummy
        • Not the real object
      • Fake
        • Usable for testing but not for real job
      • Stub
        • Fake that returns canned data
      • Spy
        • Stub that records called methods, etc.
      • Mock
        • Spy with expectations
      Terminology
    17. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { } } ?> Stubs
    18. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); } } ?> Stubs
    19. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); $stub -> expects ( $this -> any ()) -> method ( 'doSomething' ) -> will ( $this -> returnValue ( 'foo' )); } } ?> Stubs
    20. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class StubTest extends PHPUnit_Framework_TestCase { public function testStub () { $stub = $this -> getMock ( 'SomeClass' ); $stub -> expects ( $this -> any ()) -> method ( 'doSomething' ) -> will ( $this -> returnValue ( 'foo' )); // Calling $stub->doSomething() will now return // 'foo'. } } ?> Stubs
    21. Testing Object Interaction
      • Mock Objects are simulated objects that mimic the behavior of real objects in controlled ways
      • We can create a mock object to test the behavior of some other object
      • The Mock Objects system that is integrated in PHPUnit provides a fluent interface to specify the behavior of and the expectations for the mock
      Mock Objects
    22. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { } } ?> Mock Objects
    23. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); } } ?> Mock Objects
    24. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); $observer -> expects ( $this -> once ()) -> method ( 'update' ) -> with ( $this -> equalTo ( 'something' )); } } ?> Mock Objects
    25. Testing Object Interaction <?php require_once 'PHPUnit/Framework.php' ; class ObserverTest extends PHPUnit_Framework_TestCase { public function testUpdateIsCalledOnce () { $observer = $this -> getMock ( 'Observer' , array( 'update' ) ); $observer -> expects ( $this -> once ()) -> method ( 'update' ) -> with ( $this -> equalTo ( 'something' )); $subject = new Subject ; $subject -> attach ( $observer ); $subject -> doSomething (); } } ?> Mock Objects
    26. Database Testing
      • Michael Lively Jr. has ported the DbUnit extension for JUnit to PHPUnit
      • PHPUnit_Extensions_Database_TestCase
        • is used to test database-driven projects and
        • puts your database into a known state between test runs
          • This avoids problems with one test corrupting the database for other tests
        • has the ability to export and import your database data to and from XML datasets
    27. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { } The BankAccountDB example
    28. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } } The BankAccountDB example
    29. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } protected function getConnection () { return $this -> createDefaultDBConnection ( $this -> pdo , 'sqlite' ); } } The BankAccountDB example
    30. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { protected $pdo ; public function __construct () { $this -> pdo = new PDO ( 'sqlite::memory:' ); BankAccount :: createTable ( $this->pdo ); } protected function getConnection () { return $this -> createDefaultDBConnection ( $this -> pdo , 'sqlite' ); } protected function getDataSet () { return $this -> createFlatXMLDataSet ( '/path/to/seed.xml' ); } } The BankAccountDB example
    31. Database Testing <dataset> <account account_number= &quot;15934903649620486&quot; balance= &quot;100.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12348612357236185&quot; balance= &quot;89.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> </dataset> The BankAccountDB example
    32. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { } } The BankAccountDB example
    33. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); } } The BankAccountDB example
    34. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); $set = $this -> createFlatXMLDataSet ( '/path/to/after-new-account.xml' ); } } The BankAccountDB example
    35. Database Testing <?php require_once 'PHPUnit/Extensions/Database/TestCase.php' ; class BankAccountDBTest extends PHPUnit_Extensions_Database_TestCase { // ... public function testNewAccount () { $ba = new BankAccount ( '12345678912345678' , $this -> pdo ); $set = $this -> createFlatXMLDataSet ( '/path/to/after-new-account.xml' ); $this -> assertTablesEqual ( $set -> getTable ( 'account' ), $this -> getConnection () -> createDataSet () -> getTable ( 'account' ) ); } } The BankAccountDB example
    36. Database Testing <dataset> <account account_number= &quot;15934903649620486&quot; balance= &quot;100.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12348612357236185&quot; balance= &quot;89.00&quot; /> <account account_number= &quot;15936487230215067&quot; balance= &quot;1216.00&quot; /> <account account_number= &quot;12345678912345678&quot; balance= &quot;0.00&quot; /> </dataset> The BankAccountDB example
    37. The End
      • Thank you for your interest!
      • These slides will be available shortly on http://sebastian-bergmann.de/talks/.
    38. License
      • This presentation material is published under the Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported license.
      • You are free:
        • to Share – to copy, distribute and transmit the work.
      • Under the following conditions:
        • Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
        • Noncommercial. You may not use this work for commercial purposes.
        • No Derivative Works. You may not alter, transform, or build upon this work.
      • For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
      • Any of the above conditions can be waived if you get permission from the copyright holder.
      • Nothing in this license impairs or restricts the author's moral rights.

    + Sebastian BergmannSebastian Bergmann, 3 years ago

    custom

    5882 views, 9 favs, 11 embeds more stats

    PHPUnit is an open source framework for test-driven more

    More info about this document

    CC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs LicenseCC Attribution-NonCommercial-NoDerivs License

    Go to text version

    • Total Views 5882
      • 5453 on SlideShare
      • 429 from embeds
    • Comments 0
    • Favorites 9
    • Downloads 0
    Most viewed embeds
    • 374 views on http://sebastian-bergmann.de
    • 35 views on http://www.planet-php.net
    • 7 views on http://www.planet-php.org
    • 3 views on http://lj-toys.com
    • 2 views on http://planet-php.org

    more

    All embeds
    • 374 views on http://sebastian-bergmann.de
    • 35 views on http://www.planet-php.net
    • 7 views on http://www.planet-php.org
    • 3 views on http://lj-toys.com
    • 2 views on http://planet-php.org
    • 2 views on http://www.netvibes.com
    • 2 views on http://phpeye.com
    • 1 views on file://
    • 1 views on http://www.phpeye.com
    • 1 views on http://newsalloy.com
    • 1 views on http://friends.thiesen.org

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories