views:

70

answers:

1

I am having trouble getting my filters/validators to work correctly on my form, so I want to create a Unit test to verify that the data I am submitting to my form is being filtered and validated correctly.

I started by auto-generating a PHPUnit test in Zend Studio, which gives me this:

<?php
require_once 'PHPUnit/Framework/TestCase.php';

/**
 * Form_Event test case.
 */
class Form_EventTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var Form_Event
     */
    private $Form_Event;
    /**
     * Prepares the environment before running a test.
     */
    protected function setUp ()
    {
        parent::setUp();
        // TODO Auto-generated Form_EventTest::setUp()
        $this->Form_Event = new Form_Event(/* parameters */);
    }
    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown ()
    {
        // TODO Auto-generated Form_EventTest::tearDown()
        $this->Form_Event = null;
        parent::tearDown();
    }
    /**
     * Constructs the test case.
     */
    public function __construct ()
    {    // TODO Auto-generated constructor
    }
    /**
     * Tests Form_Event->init()
     */
    public function testInit ()
    {
        // TODO Auto-generated Form_EventTest->testInit()
        $this->markTestIncomplete(
        "init test not implemented");
        $this->Form_Event->init(/* parameters */);
    }
    /**
     * Tests Form_Event->getFormattedMessages()
     */
    public function testGetFormattedMessages ()
    {
        // TODO Auto-generated Form_EventTest->testGetFormattedMessages()
        $this->markTestIncomplete(
        "getFormattedMessages test not implemented");
        $this->Form_Event->getFormattedMessages(/* parameters */);
    }
}

so then I open up terminal, navigate to the directory, and try to run the test:

$ cd my_app/tests/unit/application/forms
$ phpunit EventTest.php

Fatal error: Class 'Form_Event' not found in .../tests/unit/application/forms/EventTest.php on line 19

So then I add a require_once at the top to include my Form class and try it again. Now it says it can't find another class. I include that one and try it again. Then it says it can't find another class, and another class, and so on. I have all of these dependencies on all these other Zend_Form classes. What should I do? How should I go about testing my Form to make sure my Validators and Filters are being attached correctly, and that it's doing what I expect it to do. Or am I thinking about this the wrong way?

+2  A: 

Hi,

you have to use your Zend Framework Application Configuration altough for your Unittests. If you work with the "XML" Config of PHPUnit add an Bootstrap File wich runs before the tests are executed.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="TestConfig.php">
    <testsuite name="XY">
        <directory>./</directory>
    </testsuite>
</phpunit>

In your TestConfig.php setup the AutoLoader and other needed ressources, or use the config.ini from your Application like in my example.

$includeConfig = parse_ini_file(TEST_PATH . '/config/config.ini', true);

set_include_path(
    implode(PATH_SEPARATOR, $includeConfig['includes'])
    . PATH_SEPARATOR
    . TEST_PATH . PATH_SEPARATOR
    . get_include_path()
);
unset($includeConfig);

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('App_');

If you need more hints check out this Tutorial on Zend Framework with PHPUNIT PHPUnit + Zend Framework

ArneRie