Featured Post

Once again, Polish Programmers are on the top

Today I would like to share a good news about Polish Programmers, who won the World Cup called “Hello World Open” located in Helsinki (Finland). Among over 2 500 programmers from 90 countries, our team from Poznań (named Need For C) was the best. It is not a big surprise that one of this...

Read More

Unit testing in Zend Framework 2

Posted by Tomasz Kuter | Posted in Programming, Web development | Posted on 07-04-2013

Tags: , , , , , , , , ,

3

Overview

Today I would like to focus on the unit testing in applications made with Zend Framework 2 and Doctrine 2 ORM. Sample unit tests were included with Zend Framework 1.x and they are also attached to Zend Framework 2 Tutorial.

Important is, that we have two approaches of testing controllers for the latest version of the framework:

  • first, based on class PHPUnit_Framework_TestCase,
  • and the second, which uses AbstractHttpControllerTestCase class.

First approach to test controllers

To see the difference I would like to show some code. At first let see the scripts provided with ZF2 Tutorial application:

In those test we are validating controllers only (not the URLs) and checking the response status code.

Second approach to test controllers

Now have a look for a different solution – using Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase, provided with the Zend Framework 2.

I extended this class with methods related to translation and published it in the Loculus library.

Let’s check the code of this approach to controllers unit testing. At the beginning PHPUnit configuration (/module/Album/test/phpunit.xml):

All .php and .phtml files will be checked against the code coverage and tests. We also get TestDox listing.

Next script (/module/Album/test/TestConfig.php) is very similar to application.config.php, and looks as follow:

The most interesting thing in this file is new configuration script: /config/autoload/testing.php, introduced by myself. In this file we can configure for example different database – just for testing (e.g. SQLite). In my case this file has following code:

Now have a look at the controller’s tests: (/module/Album/test/AlbumTest/Controller/AlbumControllerTest.php)

As you can see Zend AbstractHttpControllerTestCase class comes with big set of useful functions. We use some of them, for example assertResponseStatusCode(), assertModuleName(), assertControllerName(), assertControllerClass(), assertActionName(), assertMatchedRouteName().

We also have jQuery alike assertQueryContentContains():

and assertXpathQueryContentContains(), which uses XPath queries:

But that’s just a beginning of our unit tests!

I wrote tests for:

  • controllers,
  • entities,
  • forms,
  • and models

Testing Doctrine 2 ORM Entities

Let’s have a look for Entity testing class (/module/Album/test/AlbumTest/Entity/AlbumTest.php):

In short, I test if we can insert, update and delete records.

Testing Zend Forms

We are using Zend Forms in our application to add new records and update existing ones. For one Doctrine 2 ORM Entity – we are using one Zend Form class.
Let’s check testing code:

I am testing if form will be valid or not – in short, that’s all for Zend Forms.

Testing models

At the end I am testing our models. In the code we have a following functions:

  • getAlbums() – default records order,
  • getAlbums(‘artist’) – order by artist column,
  • getAlbum(2) – get album with id = 2,
  • and try to get not existing album getAlbum(100).

What needs to be remembered is to create tests, that will cover 90-100% of our code. It will be very useful when we change something, e.g. in the Entity – some tests should fail, because they will be not up to date and will require some updates, according to the changes in the database.

That is all in this post. You can grab the code as always from GitHub:
https://github.com/evolic/zf2-tutorial/tree/v0.4.2

Comments (3)

I went over this web site and I think you have a lot of excellent info, saved to favorites (:.

Tks man!!!! This post helps me a lot =D

Thank you for your great effort. Its really help us to understand Unit Testing.

Write a comment