Creating a Zend Controller Test Case

A Zend Controller Test Case is a type of PHPUnit Test Case used for testing Zend Framework-based Web applications.

The Zend Controller Test Case tests Controller 'Action' methods. These are actions that are received from the server and so require tests which can simulate the server environment, including requests from the user and responses from the server.

See http://framework.zend.com/manual/en/zend.test.phpunit.html for more information.

The Zend Controller Test Case takes server settings defined in your bootstrap.php file to simulate your server environment.

 

 

Instructions on how to complete a procedure

To create a new Zend Controller Test Case:

  1. In PHP Explorer view, right-click the Controller file containing the classes you would like to test and select New | Zend Framework Item | Zend Controller Test Case.
    The New Zend Controller Test Case dialog will open, with relevant information already entered into the various fields.

     

  1. The following information will be entered by default:

  2. Target folder: A new 'test' directory will be created with the same directory structure as your Controller file.

  3. SuperClass: The default SuperClass is Zend_Test_PHPUnit_ControllerTestCase. If you would like to use a different SuperClass, the SuperClass must be one that extends Zend_Test_PHPUnit_ControllerTestCase.

  4. Controller to test: The Controller class you selected.

  5. Test Class Name:  The Test Class Name will be in the format <ControllerFileName>Test.

  6. Test File Name: The Test File Name will be in the format <ControllerFileName>Test.php.

  1. You can edit these fields if necessary.

  2. If PHPUnit is not on the include path of your project,  a warning will appear. To add PHPUnit to the include path, click the underlined "Click here" link at the bottom of the dialog screen. This will enable PHPUnit Code Assist options in the PHPUnit Test.
    Once it has been clicked, the link and the warning message will disappear.

  3. Click Finish to create your test case.

 A new test case file will be created. This will contain:

  • Skeleton test cases for all the 'Action' methods in the Controller file (i.e. all methods which have an 'Action' suffix.)

  • A 'bootstrap' function which takes the settings from your bootstrap.php file in order to simulate your server connection. This will be used when the test case is run in order to simulate a user response from the server.

  • A function which dispatches the URL to test in the format <controller_name>/<action_name>).
    e.g. '$this->dispatch(foo/bar);

Running your Test Case

The created test case will initially consist of a skeleton test framework which you must edit in order to get meaningful results.

Before you can run your test case you must:

Note

See http://framework.zend.com/manual/en/zend.test.phpunit.html for more information on setting up your test case and to see an example.

Once you have edited your test case functions, you can run the test following the instructions in Running a PHPUnit Test Case.

 

 

Related Links

Related Links:
Creating a Zend Action Helper

PHPUnit Testing

Running a PHPUnit Test Case

Zend Framework Integration

Integrating with Zend Framework

Creating Zend Framework Elements

Creating a Zend Controller File