Skip Navigation LinksHome > Categories > Code from a Category

The first PHPUnit-Test for the Zend Framework



User Name: serfcompany
Name: Serf
Contact Me: www.datawebcoder.com
Home Page: www.datawebcoder.com
php,mysql,javascript,html,css. Preferable working with Zend Framework. Good know javascript. I worked with various, javascript frameworks such as(jquery, YUI3, extjs, sencha touch). [More]
Viewed Times: 1852
Add Date: 07/24/2012
In this article I will discuss how best to quickly and easily begin to use PHPUnit to test applications on the Zend Framework, ie how to write a first test for type "Hello, World". In response to the question: what is the PHPUnit and what it is I recommend this article PHPUnit.

I assume that the Zend Framework is already installed and working correctly. Before you begin writing the first test, you need to install PHPUnit.


The directory structure


If you create a project you have used Zend-Tools, then the necessary directories and core files you have to be created.
The directory lib, should be the directory tests, which should be a file and directory phpunit.xml application with the file bootstrap.php.
Schematically, it looks like this:


project
|-- lib
|
|--application
| |
| |--controllers
| |
| |--IndexController.php
|
|--tests
|
|--application
| |
| |--bootstrap.php
|
|--phpunit.xml


These files and directories can be created manually if they do not exist.


Configuration.


All configuration parameters are set in the test configuration file phpunit.xml
Here is a minimal text of this file:



In the configuration file path is specified bootstrap.php and a set of tests.
We now have a set that includes all the tests.


File bootstrap.php


Bootstrap.php file on the functions and content almost doubles index.php.
Here are its contents:



In Bootstrap.php we point the way, and attach files to the appropriate classes.


File ControllerTestCase.php


This file is an abstract class ControllerTestCase, which will be inherited by all the tests.
File ControllerTestCase.php:



Each MVC application should properly initialize a variable $ this-> bootstrap.
The documentation describes three possible methods above used one of them.


The core test is ready, you can write a first test.


And once again about the directory structure


The documentation for PHPUnit is the recommended organizational structure of tests. According to the recommendations of the directory tree of tests should be repeated a tree the application under test.
Therefore, to test the controller in the application directory creates a directory controllers, which will be established tests of controllers


Classes and files, respectively, unit-tests should be named according to the rules:


1.Name of the tester class is formed by adding the postfix Test to name the test class. For example test class called Class, the tester - ClassTest.
2.Names of testing methods are formed by adding prefixes to the test names test methods.


Therefore, testing of IndexController create a class in the file IndexControllerTest IndexControllerTest.php.


File IndexControllerTest.php


This file is in application / controllers /
File IndexControllerTest.php:



As you can see, the class inherits ControllerTestCase.
To test the method of IndexController IndexAction creates a function testIndexAction.
Strictly speaking, this is not a functional test, and a simple test.
The test consists in the fact that when processing URL "/ index / index" should be the one controlled current controller index, the current action - the action index.
In addition, verify that the return code of HTML-request: 200 (no errors).


All tests are cost PHPUnit-on statements (assert).
This test verified statements: assertController, assertAction, assertResponseCode.
To successfully run the test necessarily requires that all allegations.


Performing the test


From the catalog oftests run phpunit. If all is well you will see the following result:



So, you see that the principle of writing tests is very simple.
In future articles I will discuss features of PHPUnit, having a more practical sense.


Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Details**:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
 
    
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.