Functional testing of file uploads in symfony without calling click ()

I cannot perform a functional test when uploading files via the sfTestFunctional post () method. My action processing the HTTP message can get all the parameters except the file upload field.

Looking at the source code for Symfony 1.4, I think that sfBrowserBase only handles downloading a file through the click () method (in particular, doClickElement ()). Is this the only way to “upload” a file through a functional test?

By the way, I am asking about this since I do not have any HTML form that represents the data to submit, its part of the REST API. If the functional test is to click (), then I just need to create a dummy html page with the necessary html form elements.

+3
source share
2 answers

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

But I had to modify it a bit to get it working with Symfony 1.4. As a form, I introduced a new class called “Browser”, which extends “sfBrowser” and “TestFunctional”, which extends “sfTestFunctional”.

Browser :: uploadFile ():

I added a parameter of type $ for the content type, otherwise the test will fail ("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 need to take care of the name:


$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'
  )))

;
+1
source

, , mime_type '' (.. ). app.yml:

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

test:
  .validation:
    valid_slug_file_types:
      -

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

( sfBrowser sfTestFunctional) , .

0

Source: https://habr.com/ru/post/1742867/


All Articles