This document provides examples of unit testing code in PHP using PHPUnit. It demonstrates how to write test cases with assertions, use PHPUnit features like data providers and expected exceptions, and addresses challenges like external dependencies by implementing dependency injection. Mocking and stubs are also presented as ways to isolate code during testing. The overall content aims to explain PHPUnit and best practices for writing clean, isolated and effective unit tests.
2. Unit testing
• wat is unit testen?
• phpunit?
• code coverage?
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. 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);
}
}
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));
}
}
12. setUp(), tearDown() (1)
<?php
class fooTest extends PHPUnit_Framework_TestCase
{
public function testBlahBlah() {
$foo = new Bar();
....
}
public function testBlah() {
$foo = new Bar();
....
}
}
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. @depends (1)
<?php
class fooTest extends PHPUnit_Framework_TestCase
{
public function testFoo() {
....
}
/**
* @depends testFoo
*/
public function testBar() {
....
}
}
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. @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. @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. @expectedException (2)
<?php
class fooTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException Exception
*/
public function testCount() {
$foo = new Foo();
$foo->functionWithException();
}
}
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. 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. 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);
}
}