开发者

Unit Test custom Zend_Form element

This is a long winded question but I've spent hours searching and trying different things but haven't gotten it solved yet so this is my last hope.

I've created a custom Zend_Form_Element which I got the code from here: http://www.zendcasts.com/ajaxify-your-zend_form-validation-with-jquery/2010/04/ All the code can be found on that site but basically I've created a form which has multiple input elements which returns one value.

Doing a view source on the resulting webpage, the form element will look like this

<input size="3" maxlength="3" id="phone-areanum" name="phone[areanum]" value="" type="text">
<input size="3" maxlength="3" id="phone-geonum" name="phone[geonum]" value="" type="text">
<input size="4" maxlength="4" id="phone-localnum" name="phone[localnum]" value="" type="text">

Here is the unit test which I feel should work but isn't

    public function testValidDataRedirectsToAppointmentTimePage()
{
    $phone = array('areanum'=>'480', 'geonum'=>'123', 'localnum'=>'5678' );
    $this->request->setMethod('post')
                  ->setPost(array(
                    'phone' => $phone,
                    'name' => 'Smith'
                  ));
    $this->dispatch('/sign-in');


    // assert that user was redirected to current-patient page
    $t开发者_运维知识库his->assertRedirectTo('/current-patient');
}

I'm testing that if the user enters their phone number and name into the fields they'll get redirected to the right page. When I stuck a Zend_Debug::dump($this->getResponse()->getBody()); into the unit test function I was able to determine that the value I set in post caused an error and the page didn't load correctly. Other values I've tried for $phone are:

$phone = array('480', '123', '5678');
$phone = array( 480, 123, 5678 );
$phone = '480-123-5678';
$phone = '4801235678';

Can't think of what else there is to try?

Update: Adding Controller Action

public function phoneAction()
{
    $this->view->title = "Please Sign In"; 
    $this->view->headTitle("Sign In");
    $form = new Application_Form_Phone();

    $this->view->form = $form;

    if( $this->getRequest()->isPost() ) {
        if( $form->isValid($this->getRequest()->getPost()) ) {
            $phone = $form->getValue('phone');
            $name = $form->getValue('name');

            // function to get user's id from their info
            $this->_session->user_id = $this->_getUserId($phone, $name);

            return $this->_redirect('/current-patient');
        }
    }
}

class Application_Form_Phone extends Zend_Dojo_Form
{

    public function init()
    {
        $this->addElementPrefixPath('Grabby_Form_Validate', 'Grabby/Form/Validate', 'validate');
        $this->setName('phoneform');
        $this->setMethod('post');

        $phone = new Grabby_Form_Element_Phone('phone');
        $phone->addValidator(new Grabby_Form_Validate_Phone());
        $this->addElement($phone);

        $this->addElement('ValidationTextBox', 'name', array(
            'required' => true,
            'label' => 'Last Name',
            'trim' => true,
            'promptMessage' =>'Enter your last name',
            'InvalidMessage' => 'Last name required',
        ));

        $this->addElement('SubmitButton', 'submitbutton', array(
            'label' => 'Submit',
            'ignore' => true,
        ));
    }
}

As you can see there is not much going on. The code works fine, just trying to unit-test it so I can "prove" it works.


What you need is probably something along these lines:

$phone = array(
    'areanum' => 480,
    'geonum' => 123,
    'localnum' => 5678
);

Although you create a combined value in the end, the post still sends it split into parts.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜