Table of contents


Hello World

    <?php
    require_once 'PHPUnit/Framework.php';
     
    class ArrayTest extends PHPUnit_Framework_TestCase {
        public function testNewArrayIsEmpty() {
            // Create the Array fixture.
            $fixture = array();
     
            // Assert that the size of the Array fixture is 0.
            $this->assertEquals(0, sizeof($fixture));
        }
     
        public function testArrayContainsAnElement() {
            // Create the Array fixture.
            $fixture = array();
     
            // Add an element to the Array fixture.
            $fixture[] = 'Element';
     
            // Assert that the size of the Array fixture is 1.
            $this->assertEquals(1, sizeof($fixture));
        }
    }
    ?>

Data Providers

  • using the @dataProvider annotation
  • A data provider method must be public
  • A data provider method must be return an array of arrays or an object that implements the Iterator interface and yields an array for each iteration step
    <?php
    class DataTest extends PHPUnit_Framework_TestCase {
        /**
         * @dataProvider provider
         */
        public function testAdd($a, $b, $c) {
            $this->assertEquals($c, $a + $b);
        }
     
        public function provider() {
            return array(
              array(0, 0, 0),
              array(0, 1, 1),
              array(1, 0, 1),
              array(1, 1, 3)
            );
        }
    }
    ?>

Expected Exception

there are 3 ways to expecte exception

  1. Using @expectedException annotation
  2. Using setExpectedException() method
  3. Using try/catch fail() pattern.
    <?php
    require_once 'PHPUnit/Framework.php';
     
    class ExceptionTest extends PHPUnit_Framework_TestCase
    {
        /**
         * @expectedException InvalidArgumentException
         */
        public function testException()
        {
        }
    }
    ?>

    <?php
    require_once 'PHPUnit/Framework.php';
     
    class ExceptionTest extends PHPUnit_Framework_TestCase
    {
        public function testException()
        {
            $this->setExpectedException('InvalidArgumentException');
        }
    }
    ?>

    <?php
    require_once 'PHPUnit/Framework.php';
     
    class ExceptionTest extends PHPUnit_Framework_TestCase {
        public function testException() {
            try {
                // ... Code that is expected to raise an exception ...
            }
     
            catch (InvalidArgumentException $expected) {
                return;
            }
     
            $this->fail('An expected exception has not been raised.');
        }
    }
    ?>

setUp() and tearDown()

Yes. There are setUp and terrDown fixtures in PHPUnit.

    <?php
    require_once 'PHPUnit/Framework.php';
     
    class DatabaseTestSuite extends PHPUnit_Framework_TestSuite
    {
        protected function setUp()
        {
            $this->sharedFixture = new PDO(
              'mysql:host=wopr;dbname=test',
              'root',
              ''
            );
        }
     
        protected function tearDown()
        {
            $this->sharedFixture = NULL;
        }
    }
    ?>

Test Suites

TBD

TestCase Extensions

there are some TestCase extensions can be used, for example

  1. PHPUnit_Extensions_OutputTestCase
  2. PHPUnit_Extensions_PerformanceTestCase
  3. PHPUnit_Extensions_Database_TestCase

or you can make your own.

Search PHPUnit_Extensions_* from all testcase extension.

Skip and Incomplete Test

    <?php
    require_once 'PHPUnit/Framework.php';
     
    class ArrayTest extends PHPUnit_Framework_TestCase {
        public function testNewArrayIsEmpty() {
            $array = array();
            $this->assertEquals(0, sizeOf($array));
        }
     
        public function testArrayContainsAnElement() {
            $array[] = "content";
            $this->assertEquals(1, sizeof($array));
            $this->markTestIncomplete(
              'This test has not been implemented yet.'
              );
        }
     
     
        public function testArrayContainsTwoElement() {
            $array[] = "content";
            $array[] = "content2";
            $this->assertEquals(2, sizeof($array));
            $this->markTestSkipped("this test is not completed yet");
        }
    }
     
    ?>

the output result will show as S or I

    PHPUnit 3.3.15 by Sebastian Bergmann.
     
    .SI
     
    Time: 0 seconds
     
    OK, but incomplete or skipped tests!
    Tests: 3, Assertions: 1, Incomplete: 1, Skipped: 1.

if assertion fail before markTestSkipped() or markTestIncomplete(), assertion will still fail.

Test Doubles (a.k.a Mocking)

check here