5

I wanted to write unit tests for my Zend Framework/Doctrine 2.0 application, but I don't quite understand how to set up unit testing in ZF. Also, I would like to include Doctrine 2.0 in those unit tests. How would I go about setting this up? Can you point me to an example?

Thank You

clarkstachio
  • 613
  • 4
  • 8
  • I'm also interested in how to include Doctrine 2 in the unit tests. I did even posted a question about it on the mailing list but I didn't got any answer. – tom Aug 26 '10 at 07:18
  • I have made some progress on this issue, and will post my setup in this post when I get a couple of minutes. Do you think one should test persistence in their Doctrine 2.0 model layer, or not worry about that and test the model as plain php objects? – clarkstachio Aug 27 '10 at 03:25
  • I think you should test only the model and not doctrine. As they write their own unit tests. Did you made some progress with it? – tom Aug 31 '10 at 08:05
  • I will post something this weekend. Two problems with not testing persistence: 1)you can't test repository custom methods, 2)if any of your models rely on an auto-id to work correctly, this wouldn't be possible as you would need to use the entity manager persist method. – clarkstachio Sep 01 '10 at 01:16
  • Hi, can you post your solution if you've got something that works? Thx. – tom Sep 11 '10 at 13:29
  • Check out this Gist from Ocramius on testing ZF2/Doctrine applications https://gist.github.com/Ocramius/3994325 – creamcheese Oct 26 '13 at 10:24

1 Answers1

2

To setup the unit tests I created a configuration file for phpunit (phpunit.xml) and a TestHelper.php in the test directory. The configuration basically says to phpunit which unit test needs to be executed and wich folders and files needs to be skipped in the coverage. In my config it just are all the unit test files in the application and library folder that are going to be executed.

The Testhelper needs to be extended by all your unit tests.

phpunit.xml

<phpunit bootstrap="./TestHelper.php" colors="true">
    <testsuite name="Your Application">
        <directory>./application</directory>
        <directory>./library</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application/</directory>
            <directory suffix=".php">../library/App/</directory>
            <exclude>
                <directory suffix=".phtml">../application/</directory>
                <directory suffix=".php">../application/database</directory>
                <directory suffix=".php">../application/models/Entities</directory>
                <directory suffix=".php">../application/models/mapping</directory>
                <directory suffix=".php">../application/models/proxy</directory>
                <directory suffix=".php">../application/views</directory>
                <file>../application/Bootstrap.php</file>
                <file>../application/modules/admin/controllers/ErrorController.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
        <log type="testdox" target="./log/testdox.html" />
    </logging>
</phpunit>

TestHelper.php

<?php
error_reporting(E_ALL | E_STRICT);

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define testing application environment
define('APPLICATION_ENV', 'testing');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/**
 * Zend_Application
 */
require_once 'Zend/Application.php';

/**
 * Base Controller Test Class
 *
 * All controller test should extend this
 */
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class BaseControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{   
    public function setUp()
    {
        $application = new Zend_Application(APPLICATION_ENV,
                             APPLICATION_PATH . '/configs/application.ini');
        $this->bootstrap = array($application->getBootstrap(), 'bootstrap');

        Zend_Session::$_unitTestEnabled = true;

        parent::setUp();
    }

    public function tearDown()
    {
        /* Tear Down Routine */
    }
}

This only covers the initial setup for ZF and PHPunit

tom
  • 8,189
  • 12
  • 51
  • 70
  • Thanks for the feedback. Do you normally extend Zend_Test_PHPUnit_ControllerTestCase in all your test cases? Or should this class only be extended for controller tests? – clarkstachio Aug 27 '10 at 03:27
  • My test all extend the BaseControllerTestCase. But normally you should only extend from the BaseControllerTestCase for your controller tests and all other test from PHPUnit_Framework_TestCase – tom Aug 27 '10 at 06:01