Zend Framework: How to start PHPUnit testing Forms?

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-06-16T21:28:06Z Indexed on 2010/06/16 21:32 UTC
Read the original article Hit count: 499

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?

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about zend-framework