Skip Navigation LinksHome > Categories > Code from a Category

PHPUnit. Testing database applications on the Zend Framework

User Name: serfcompany
Name: Serf
Contact Me:
Home Page:
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: 3367
Add Date: 07/31/2012
In this article I propose to move on to more interesting and useful case - testing models, working with a database table for application Zend Framework.

The directory structure.

The directory structure described in the first article, supplemented by catalog models, which will house the files with the tests. This directory is a subdirectory of fixtures, in it I will keep the data for testing. And common to all test data will be placed directly into this directory and the data that is specific to the tested grades will be placed in the corresponding directories.

The directory structure.

In this example, I'll test the model table tcategory.
I will write tests for the functions to create, edit, delete, get category.

Infrastructure files.

We now consider the files. Bootstrap.php file has not undergone major changes, just added a few new connection files.

File bootstrap.php

File phpunit.xml not changed at all.
By analogy with the class create ControllerTestCase DbTestCase, which will be inherited by all the tests.

File DbTestCase.php

Consider the file more closely.

It is designed to prepare the test environment to work: initialize variables that store the path to the files and downloaded xml-file test data.
Function getTearDownOperation () defines the operation to be performed after each test. In this case, will be carried out cleaning tables.

Thus, by using the setUp () and getTearDownOperation () for each test created a special test environment, which upon completion will be deleted. It turns out that the tests work with their personal information and do not affect each other.

The function getConnection () establishes a connection to the database and requires no special explanation.
Let me just note that for testing a special base of the structure are the same battle, the database name read out from the ini-file, this is a typical configuration file Zend Framework.

The function protected function getDataSet ($ pFileName = null) - an implementation method of the class must Zend_Test_PHPUnit_DatabaseTestCase, the method of appointment - to create test data. For all tests assumed a data file init.xml, but the test can also use a specific file.

Class Zend_Test_PHPUnit_DatabaseTestCase - Zend Framework is the successor of sharpened PHPUnit_Extensions_Database_TestCase..

The function protected function prepareInitData ($ pInitData) perform the initial installation of the data if the test requires special conditions.

File init.xml
This file contains the configuration of the test environment. If the test does not use its own configuration file, the default is that.

As you can see, this is a simple xml-file, which describes the structure of the data and the actual data itself.

Class test.

We pass the tests.

File CategoryTest.php.
This file contains the main class.

Note the name of the test methods. The name is formed as: test + test method.

Test class Model_DbTable_Category, it is reasonable to put all configuration files in the directory fixtures / Model_DbTable_Category.
For the functions for creating and receiving categories used by a typical set of initial data init.xml. But for the functions of removing and changing yarebuyutsya special initial conditions.

Function testaddCategory ().

This is a test function to create the category. The meaning of the function is to create a category with predetermined parameters, then the database state, ie test table is compared with previously known. If the states match, then the function worked as expected.
To access the settings XML-file I'm using your class XmlDataSet, here is his code.

File XmlDataSet.php

I suspect that there is a more correct way, if you know - tell me.

The class inherits from XmlDataSet PHPUnit_Extensions_Database_DataSet_XmlDataSet and need to fix obscure feature - the lack of access method getTableInfo. GetValue function is needed to facilitate extraction of data from xml-files.

Now working as a test:

* PHPUnit runs a function setUp (), in the overdetermined DbTestCase, this function reads from the file init.xml initialization data for the table, ie essentially clears the table.
* Runs the test itselftestaddCategory, the test creates a category of checks and what happened as a result.
* After completing the test, regardless of the outcome function is executed tearDown (), in DbTestCase I did not like it override setUp (), because This makes no sense. tearDown () calls getTearDownOperation (), which is redefined in DbTestCase, this function cleans up all the results of the test.

Thus, after testaddCategory ready to perform other tests, as the database is in original condition.
Isolation test has many advantages: the result does not depend on the sequence of execution, we can safely change the test data of any test and not be afraid to influence the performance of others.

The principle of operation is similar to other tests, a test category: create the test environment, test the model function is executed, the result is compared with the standard, test environment returns to its original state.

The configuration files.

The use of configuration files, you can make changes to the tests, without correcting the source of these tests. This makes possible the division of labor between the programmer - author of the test and tester. Tester to change the data, do not climb on the source.

XML files are selected for storage of test data is not accidental. XML allows a very convenient way to describe the data structure and the data itself.

File addCategory.xml Reference data for testing a category.

File delBeginCategory.xml
Initial data for testing removal category.

File delEndCategory.xml
Reference data for testing the removal of the category. As you can see, the category with ID = 1 should be removed

File getCategory.xml
Reference data for testing the function of receiving category.

File updateBeginCategory.xml
Initial data for testing change categories.

File updateEndCategory.xml
Reference data for testing changes category.


In this article I have tried as much as possible to expand on the basics of writing tests for testing models of database applications on the Zend Framework.

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:
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.