Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction into unit testing

Introduction into unit testing

Joshua Thijssen

January 01, 2012
Tweet

More Decks by Joshua Thijssen

Other Decks in Programming

Transcript

  1. testcase (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function

    testBlahBlah() { $foo = new Foo(); $this->assertEquals($foo->baz(), 1); } } <?php class foo { public function baz() { return 1; } } FooTest.php: Foo.php woensdag 25 april 12
  2. testcase (2) include_once "foo.php"; class fooTest extends PHPUnit_Framework_TestCase { public

    function testBlahBlah() { $foo = new Foo(); $this->assertEquals($foo->baz(), 1); } } <?php class foo { public function baz() { return 1; } } FooTest.php: Foo.php woensdag 25 april 12
  3. testcase (4) <?php class foo { public function baz() {

    return 1; } public function isOdd($number) { if (($number & 1) == 1) { return true; } else { return false; } } } FooTest.php: Foo.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)); } } woensdag 25 april 12
  4. 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)); } } woensdag 25 april 12
  5. setUp(), tearDown() (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public

    function testBlahBlah() { $foo = new Bar(); .... } public function testBlah() { $foo = new Bar(); .... } } woensdag 25 april 12
  6. 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() { .... } } woensdag 25 april 12
  7. @depends (1) <?php class fooTest extends PHPUnit_Framework_TestCase { public function

    testFoo() { .... } /** * @depends testFoo */ public function testBar() { .... } } woensdag 25 april 12
  8. @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); ... } } woensdag 25 april 12
  9. @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) ); } } woensdag 25 april 12
  10. @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) { } } } woensdag 25 april 12
  11. @expectedException (2) <?php class fooTest extends PHPUnit_Framework_TestCase { /** *

    @expectedException Exception */ public function testCount() { $foo = new Foo(); $foo->functionWithException(); } } woensdag 25 april 12
  12. 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++; } return $count; } protected function _mail($to) { return mail($to, $this->_subject, $this->_body); } } Isolated woensdag 25 april 12
  13. 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("[email protected]"); $mailer->setBody("meeh"); $to = array("[email protected]", "[email protected]", "[email protected]"); $this->assertEquals($mailer->mail($to), 3); } } woensdag 25 april 12
  14. 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("[email protected]"); $mailer->setBody("meeh"); $to = array("[email protected]", "[email protected]", "[email protected]"); $this->assertEquals($mailer->mail($to), 3); } } woensdag 25 april 12
  15. 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("[email protected]"); $stub->setBody("meeh"); $to = array("[email protected]", "[email protected]", "[email protected]"); $this->assertEquals($stub->mail($to), 3); } } woensdag 25 april 12
  16. 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? woensdag 25 april 12
  17. 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 woensdag 25 april 12
  18. Difficult cases (3) function testGetItem() { $adapter = new Zend_Http_Client_Adapter_Test();

    $adapter->setResponse(“HTTP/1.1 200 OK\r\n\r\nBLAAT”); $adapter->addResponse(“HTTP/1.1 200 OK\r\n\r\nAAP”); $foo = new Foo(); $foo->setAdapter($adapter); $this->assertEquals($foo->getItem(1), ‘blaat’); $this->assertEquals($foo->getItem(2), ‘aap’); } ∂ IT’S TESTABLE AGAIN woensdag 25 april 12
  19. Dependency Injection (1) ∂ THIGHT COUPLING AGAIN public function pay(Article

    $article) { $psp = new PaymentProvider(); $psp->setAmount($article->getAmount()); return $psp->pay(); } woensdag 25 april 12
  20. Dependency Injection (2) ∂ DECOUPLE PAYMENT PROVIDER 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(); } woensdag 25 april 12
  21. Dependency Injection (3) ∂ TESTABLE *AND* EXTENDABLE 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); } woensdag 25 april 12
  22. Dependency Injection (4) ∂ TESTABLE *AND* EXTENDABLE 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); } woensdag 25 april 12