English 中文(简体)
How to test my forms in Zend Framework 1.8+?
原标题:

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?

最佳回答

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.

问题回答

If you look in your application.ini, look at the line stating "appnamespace" that defaults to "Application" if you re using Zend_Tool to create your Zend Framework skeleton.

In order to prevent this error to occur, you either change this setting to "Default" or you rename all Default_* classes to Application_* (in this particular case it would mean you rename "Default_Form_AccountProfile" into "Application_Form_AccountProfile").





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签