3

I can't seem to perform functional test on file uploads through the sfTestFunctional's post() method. My action handling the HTTP post is able to get all parameters except the file upload field.

Looking at Symfony 1.4's source codes, i think the sfBrowserBase only handle file upload through the click() method (specifically, the doClickElement()) . Is that the only way to 'upload' a file through functional test?

BTW, i'm asking this as i do not have any HTML form that represents the data to submit, its part of a REST API. If the functional test must 'click()', then i just have to create dummy html page with the needed html form elements.

2
  • Long shot... koss, did you get anywhere with the above problem? Commented Oct 20, 2010 at 13:21
  • @richsage sorry..been too long. can't remember exactly. vaguely remember extending sfBrowserBase to get around what i need. not very help, i'm sorry. Commented Oct 20, 2010 at 15:13

2 Answers 2

1

I found this: http://www.devcomments.com/Uploads-on-functional-test-for-REST-services-to189066.htm#filtered

But I had to modify it a little bit in order to get it to work with Symfony 1.4. As a matter of form I introduced a new class 'Browser', which extends 'sfBrowser' and 'TestFunctional', which extends 'sfTestFunctional'.

Browser::uploadFile():

I added a $type parameter for the content-type, otherwise the test fails ('invalid mime-type')


  public function uploadFile($fieldName, $filename, $type = '')
  {
    if (is_readable($filename))
    {
      $fileError = UPLOAD_ERR_OK;
      $fileSize = filesize($filename);
    }
    else
    {
      $fileError = UPLOAD_ERR_NO_FILE;
      $fileSize = 0;
    }

    $this->parseArgumentAsArray($fieldName, array('name' => basename($filename), 'type' => $type, 'tmp_name' => $filename, 'error' => $fileError, 'size' => $fileSize), $this->files);

    return $this;
  }

TestFunctional::uploadFile()


  public function uploadFile($fieldName, $filename, $type = '')
  {
    $this->browser->uploadFile($fieldName, $filename, $type);

    return $this;
  }

You have to take care about naming:


$browser = new TestFunctional(new Browser());
$browser->

  uploadFile('article_image[image]', '/path/to/your/image.gif', 'image/gif')->

  post('/articles/1/images.json', array('article_image' => array(
    'title' => 'foobar',
    'description' => 'lorum ipsum'
  )))

;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this. Almost 5 years later it's still a very usefull answer !
0

The problem is that the functional test thinks the mime_type is '' (i.e., blank). So for me an easier way was to define the valid file types in app.yml:

all:
  .validation:
    valid_file_types:
      - text/comma-separated-values
      - text/csv
...et cetera...

test:
  .validation:
    valid_slug_file_types:
      -

Then in the Form,

$this->setValidator('file', new sfValidatorFile(array(
        'mime_types' => sfConfig::get('app_valid_file_types')
    )));

The other solution (extra methods on the sfBrowser and sfTestFunctional) won't work for me if the file field is required.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.