Workshop unittesting

1,464 views

Published on

Small and simple workshop on the basics of unit testing and phpunit

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,464
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Workshop unittesting

    1. 1. phpunitHow not to get roundkicked by Chuck Norris
    2. 2. Unit testing • wat is unit testen? • phpunit? • code coverage?
    3. 3. testcase (1)Foo.php <?php class foo { public function baz() { return 1; } }FooTest.php: <?php class fooTest extends PHPUnit_Framework_TestCase { public function testBlahBlah() { $foo = new Foo(); $this->assertEquals($foo->baz(), 1); } }
    4. 4. testcase (2)Foo.php <?php class foo { public function baz() { return 1; } }FooTest.php: include_once "foo.php"; class fooTest extends PHPUnit_Framework_TestCase { public function testBlahBlah() { $foo = new Foo(); $this->assertEquals($foo->baz(), 1); } }
    5. 5. testcase (3)
    6. 6. testcase (4)Foo.php FooTest.php: <?php class foo { <?php public function baz() { return 1; include_once "foo.php"; } class fooTest extends PHPUnit_Framework_TestCase public function isOdd($number) { { if (($number & 1) == 1) { public function testBlahBlah() { return true; $foo = new Foo(); } else { $this->assertEquals($foo->baz(), 1); return false; } } } public function testIsOdd() { } $foo = new Foo(); $this->assertTrue($foo->isOdd(1)); } }
    7. 7. testcase (5)
    8. 8. testcase (6)
    9. 9. testcase (7)FooTest.php: <?php include_once "foo.php"; class fooTest extends PHPUnit_Framework_TestCase { public function testBlahBlah() { $foo = new Foo(); $this->assertEquals($foo->baz(), 1); } public function testIsOdd() { $foo = new Foo(); $this->assertTrue($foo->isOdd(1)); } public function testDoesIsOddReturnFalseWhenAnEvenDigitIsGiven { $foo = new Foo(); $this->assertFalse($foo->isOdd(2)); } }
    10. 10. testcase (8)
    11. 11. PHPUnit chapters • setUp(), tearDown() • @depends • @dataproviders • @exception testing • mocking & stubbing
    12. 12. setUp(), tearDown() (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function testBlahBlah() { $foo = new Bar(); .... } public function testBlah() { $foo = new Bar(); .... } }
    13. 13. setUp(), tearDown() (2) <?php class fooTest extends PHPUnit_Framework_TestCase { public setUp() { $this->_foo = new Bar(); } public tearDown() { unset($this->_foo); } public function testBlahBlah() { .... } public function testBlah() { .... } }
    14. 14. @depends (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function testFoo() { .... } /** * @depends testFoo */ public function testBar() { .... } }
    15. 15. @dataprovider (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function testCount() { $foo = new Foo(); $this->assertEquals($foo->add(1, 2), 3); $this->assertEquals($foo->add(10,-4), 6); $this->assertEquals($foo->add(4, 6), 10); ... } }
    16. 16. @dataprovider (2) <?php class fooTest extends PHPUnit_Framework_TestCase { /** * @dataprovider countProvider */ public function testCount($a, $b, $c) { $foo = new Foo(); $this->assertEquals($foo->add($a, $b), $c); } public function countProvider() { return array( array(1, 2, 3), array(10, -4, 6), array(4, 6, 10) ); } }
    17. 17. @expectedException (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function testCount() { $foo = new Foo(); try { $foo->functionWithException(); $this->fail(“An exception should have occurred”); } catch (Exception $e) { } } }
    18. 18. @expectedException (2) <?php class fooTest extends PHPUnit_Framework_TestCase { /** * @expectedException Exception */ public function testCount() { $foo = new Foo(); $foo->functionWithException(); } }
    19. 19. Mocking & Stubbing (1) <?php class MyMailer { protected $_from; protected $_body; protected $_subject; function setFrom($from) { $this->_from = $from; } ....... function mail(array $to) { $count = 0; foreach ($to as $recipient) { } if ($this->_mail($recipient)) $count++; Isolated return $count; } protected function _mail($to) { return mail($to, $this->_subject, $this->_body); } }
    20. 20. Mocking & Stubbing (2) <?php include_once "mail.php"; class Mock_MyMailer extends MyMailer { protected function _mail($to) { return true; } } class mailTest extends PHPUnit_Framework_TestCase { ... public function testMail() { $mailer = new Mock_MyMailer(); $mailer->setSubject("blaat"); $mailer->setFrom("jthijssen@example.com"); $mailer->setBody("meeh"); $to = array("aap@example.com", "blaat@example.com", "all@example.com"); $this->assertEquals($mailer->mail($to), 3); } }
    21. 21. Mocking & Stubbing (3) <?php include_once "mail.php"; class mailTest extends PHPUnit_Framework_TestCase { ... public function testMail() { $mailer = new MyMailer(); $mailer->setSubject("blaat"); $mailer->setFrom("jthijssen@example.com"); $mailer->setBody("meeh"); $to = array("aap@example.com", "blaat@example.com", "all@example.com"); $this->assertEquals($mailer->mail($to), 3); } }
    22. 22. Mocking & Stubbing (4) <?php class mailTest extends PHPUnit_Framework_TestCase { public function testMail() { $stub = $this->getMockBuilder(MyMailer, array(_mail))->getMock(); $stub->expects($this->any()) ->method(_mail) ->will($this->returnValue(true)); $stub->setSubject("blaat"); $stub->setFrom("jthijssen@example.com"); $stub->setBody("meeh"); $to = array("aap@example.com", "blaat@example.com", "all@example.com"); $this->assertEquals($stub->mail($to), 3); } }
    23. 23. Difficult cases (1) public function getItem($id) { $client = new Zend_Http_Client(); $client->setUri(‘http://my.api’); $client->setParameterPost(‘ID’,$id); $client->setMethod(Zend-Http_Client::POST); $request = $client->request(); $item = $request->getBody(); $item = strtolower($item); return $item; }∂ TESTABLE?
    24. 24. Difficult cases (2) public function __construct() { $this->_adapter = new Zend_Http_Client_Adapter_Socket(); } public function getItem($id) { $client = new Zend_Http_Client(); $client->setConfig(array(‘adapter’ => $this->getAdapter())); $client->setUri(‘http://my.api’); $client->setParameterPost(‘ID’,$id); $client->setMethod(Zend-Http_Client::POST); $request = $client->request(); $item = $request->getBody(); $item = strtolower($item); return $item; }∂ “DECOUPLE” ADAPTER
    25. 25. Difficult cases (3) function testGetItem() { $adapter = new Zend_Http_Client_Adapter_Test(); $adapter->setResponse(“HTTP/1.1 200 OKrnrnBLAAT”); $adapter->addResponse(“HTTP/1.1 200 OKrnrnAAP”); $foo = new Foo(); $foo->setAdapter($adapter); $this->assertEquals($foo->getItem(1), ‘blaat’); $this->assertEquals($foo->getItem(2), ‘aap’); }∂ IT’S TESTABLE AGAIN
    26. 26. Difficult cases (4) Gebruik “dependency injection” ipv “tight coupling”.
    27. 27. Dependency Injection (1) public function pay(Article $article) { $psp = new PaymentProvider(); $psp->setAmount($article->getAmount()); return $psp->pay(); }∂ THIGHT COUPLING AGAIN
    28. 28. Dependency Injection (2) public function setPaymentProvider(PaymentProvider $psp) { $this->_psp = $psp; } public function getPaymentProvider() { return $this->_psp; } public function pay(Article $article) { $psp = $this->getPaymentProvider(); $psp->setAmount($article->getAmount()); return $psp->pay(); }∂ DECOUPLE PAYMENT PROVIDER
    29. 29. Dependency Injection (3) public function testPayAccepted() { $stub = $this->getMockBuilder(‘paymentprovider’, array(‘pay’))->getMock(); $stub->expects($this->any()) ->method(‘pay’) ->will($this->returnValue(true)); $article = new Article(); $article->setAmount(15); $foo = new Foo(); $foo->setPaymentProvider($stub); $this->assertTrue($foo->pay($article)); $this->assertEquals($user->getAmount(), 85); }∂ TESTABLE *AND* EXTENDABLE
    30. 30. Dependency Injection (4) public function testPayFailed() { $stub = $this->getMockBuilder(‘paymentprovider’, array(‘pay’))->getMock(); $stub->expects($this->any()) ->method(‘pay’) ->will($this->returnValue(false)); $article = new Article(); $article->setAmount(15); $foo = new Foo(); $foo->setPaymentProvider($stub); $this->assertFalse($foo->pay($article)); $this->assertEquals($user->getAmount(), 100); }∂ TESTABLE *AND* EXTENDABLE
    31. 31. Workshop • Login op php5dev02 • directory: workshop • http://workshop.<username>.dev/ tests/coverage • cd tests • ./runtest.sh∂ FOLLOW INSTRUCTIONS
    32. 32. ∂ THANK YOU FOR YOUR ATTENTION

    ×