Unit Testing

A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every change is impractical. Your unit tests will help alleviate that by automatically testing your application’s components and alerting you when something is not working the same way it was when you wrote your tests.

The Zend Framework 2 API uses PHPUnit, and so does this tutorial application. A detailed explanation of unit testing is beyond the scope of this tutorial, so we will only provide sample tests for the components in the pages that follow. This tutorial assumes that you already have PHPUnit installed.

Setting up the tests directory

Start by creating a directory called test in zf2-tutorial\module\Application with the following subdirectories:

zf2-tutorial/
    /module
        /Application
            /test
                /ApplicationTest
                    /Controller

The structure of the test directory matches exactly with that of the module’s source files, and it will allow you to keep your tests well-organized and easy to find.

Bootstrapping your tests

Next, create a file called phpunit.xml.dist under zf2-tutorial/module/Application/test:

<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2tutorial">
            <directory>./ApplicationTest</directory>
        </testsuite>
    </testsuites>
</phpunit>

And a file called Bootstrap.php, also under zf-tutorial/tests/:

chdir(dirname(__DIR__));

include __DIR__ . '/../init_autoloader.php';

The contents of the bootstrap file are similar to those of zf-tutorial/public/index.php, except we don’t initialize the application. We’ll be doing that in our tests to ensure that each test is executed against a freshly initialized instance of our application without any previous tests influencing the current test’s results.

Your first Controller test

Next, create IndexControllerTest.php under zf-tutorial/module/Application/test/ApplicationTest/Controller with the following contents:

<?php

namespace ApplicationTest\Controller;

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;

class IndexControllerTest extends AbstractHttpControllerTestCase
{
    public function setUp()
    {
        $this->setApplicationConfig(
            include '/path/to/application/config/test/application.config.php'
        );
        parent::setUp();
    }
}

Add your application config with the setApplicationConfig method. You can use several config to test modules dependencies or your current application config.

Now, add the following function to the IndexControllerTest class:

public function testIndexActionCanBeAccessed()
{
    $this->dispatch('/');
    $this->assertResponseStatusCode(200);

    $this->assertModule('application');
    $this->assertControllerName('application_index');
    $this->assertControllerClass('IndexController');
    $this->assertMatchedRouteName('home');
}

The test is verifying that the homepage responds with HTTP status code 200 and that the controller’s return value is equal to ‘IndexController’.

Testing

Finally, cd to zf-tutorial/module/Application/test/ and run phpunit. If you see something like this, then your application is ready for more tests!

PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 15.75Mb

OK (1 test, 5 assertions)

Test your console router

ZendTest component provide a HTTP controller tests case and a console controller. To test your application with the console, just switch with the AbstractConsoleControllerTestCaseTest. Now, you can use the same methods in your tests controllers :

public function testConsoleActionCanBeAccessed()
{
    $this->dispatch('--your-arg');
    $this->assertResponseStatusCode(0);

    $this->assertModule('application');
    $this->assertControllerName('application_console');
    $this->assertControllerClass('ConsoleController');
    $this->assertMatchedRouteName('myaction');
}

More informations at the Zend\Test component documentation page.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Unit Testing on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.