Testing isValid on zend form with a file element

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing isValid on zend form with a file element

dmitrybelyakov
Hi,

I have a quick question on the best approach to testing Zend_Form classes that have Zend_Form_Element_File in them.

I have constructed a small test that shows what i am trying to do. In this test the file element is optional so we not goig to provide a value for that.

The test is checking if we can process a valid submission:


    public function testCanProcessValidSubmission()
    {
        $form = new Zend_Form();
        $form->setAttrib('enctype', 'multipart/form-data');
       
        //Add text field
        $username = new Zend_Form_Element_Text('username');
        $username->setRequired(true);
        $form->addElement($username, 'username');
       
        //Add file field
        $file = new Zend_Form_Element_File('image');
        $file->setLabel('Upload an image:');
        $file->setDestination('/tmp');
        $form->addElement($file, 'image');
       
        //Prepare form data
        $formData = array();
        $formData['username'] = 'Test user name';
       
       
        //Validate
        $result = $form->isValid($formData);
       
        //Assert validation passed
        $this->assertTrue($result);
    }


The form is working fine from the browser, i just whant to programmatically test it for accepting valid data submission but that always fails.

I guess that has something to do with the global $_FILES array - just no idea how should we alter that for testing.

Dmitry.
Reply | Threaded
Open this post in threaded view
|

Re: Testing isValid on zend form with a file element

David Mintz


On Sun, Apr 18, 2010 at 9:17 AM, dmitrybelyakov <[hidden email]> wrote:

Hi,

I have a quick question on the best approach to testing Zend_Form classes
that have Zend_Form_Element_File in them.

I have constructed a small test that shows what i am trying to do. In this
test the file element is optional so we not goig to provide a value for
that.

The test is checking if we can process a valid submission:


   public function testCanProcessValidSubmission()
   {
       $form = new Zend_Form();
       $form->setAttrib('enctype', 'multipart/form-data');

       //Add text field
       $username = new Zend_Form_Element_Text('username');
       $username->setRequired(true);
       $form->addElement($username, 'username');

       //Add file field
       $file = new Zend_Form_Element_File('image');
       $file->setLabel('Upload an image:');
       $file->setDestination('/tmp');
       $form->addElement($file, 'image');

       //Prepare form data
       $formData = array();
       $formData['username'] = 'Test user name';


       //Validate
       $result = $form->isValid($formData);

       //Assert validation passed
       $this->assertTrue($result);
   }


The form is working fine from the browser, i just whant to programmatically
test it for accepting valid data submission but that always fails.

I guess that has something to do with the global $_FILES array - just no
idea how should we alter that for testing.


Have you dumped $form->getMessages() to see what's failing?

--
Support real health care reform:
http://phimg.org/

--
David Mintz
http://davidmintz.org/


Reply | Threaded
Open this post in threaded view
|

Re: Testing isValid on zend form with a file element

dmitrybelyakov
David Mintz wrote
Have you dumped $form->getMessages() to see what's failing?
Sure. I did getErrors() wich i guess is the same... and it's empty.

Dmitry.