views:

148

answers:

1

So I've set up testing in my ZF 1.9.5 application thanks to this tutorial, I am able to test my controllers, now I want to create a test for a form. However, I'm having the problem that PHPUnit can't find my form.

Fatal error: Class 'Default_Form_AccountProfile' not found

I'm extending PHPUnit_Framework_TestCase instead of Zend_Test_PHPUnit_ControllerTestCase since it is not a controller. Is that the right thing to do? Here is my test:

<?php

class AccountProfileTest extends PHPUnit_Framework_TestCase
{
    public function testPopulate()
    {
        $form = new Default_Form_AccountProfile();
        $user = array(
            'firstName' => 'Joe',
            'lastName' => 'Schmoe'
        );
        $form->populate($user);
        $this->assertEquals($form->getElement('firstName')->getValue(), 'Joe');
        $this->assertEquals($form->getElement('lastName')->getValue(), 'Schmoe');
    }
}

What am I doing wrong? What would be the correct way to test a form in Zend Framework?

A: 

The simplest solution to your problem is to 'require_once' the php file where your form is located at the beginning of this file (or before calling new Default_Form...).

BTW, is there a specific reason why are you testing the default behavior of the Zend_Form? Tests for Zend_Form are already written and you can get them if you download the full version of ZF. If the form you are using has it's own, specific methods, or is overwriting some of the Zend_Forms methods than it makes sense to test those.

Goran Jurić
I wanted to overwrite the default populate method, but I wasn't sure how the original method was working in the first place, so I wanted to do a few tests to understand how it works.
Andrew
I tried using a required once, but it also can't find the Zend_Form class. I have a feeling my test is going to be littered with require statements. Is there a different way to include all the classes I need without having to specifically call them?
Andrew
You have to set the include path so your unit tests can find your Zend library. If you do not want to require_once all the classes you will be testing you have to setup the autoloader and include paths in the same way you are doing it in your application bootstrap. Put all this code in the TestHelper.php and tell phpunit tu use the TestHelper to set up your environment prior to running tests. Take a look at phpunit's documentation. I haven't seen the tutorial you are referring to so I do not know how the rest of your tests are set up.
Goran Jurić