symfony 中的文件上传功能测试,无需调用 click()

发布于 2024-08-29 23:48:38 字数 317 浏览 3 评论 0原文

我似乎无法通过 sfTestFunctional 的 post() 方法对文件上传执行功能测试。我处理 HTTP post 的操作能够获取除文件上传字段之外的所有参数。

查看Symfony 1.4的源代码,我认为 sfBrowserBase 仅通过 click() 方法(具体来说, doClickElement())处理文件上传。这是通过功能测试“上传”文件的唯一方法吗?

顺便说一句,我问这个问题是因为我没有任何代表要提交的数据的 HTML 表单,它是 REST API 的一部分。如果功能测试必须“click()”,那么我只需使用所需的 html 表单元素创建虚拟 html 页面。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-09-05 23:48:38

我发现了这个: http://www .devcomments.com/Uploads-on-function-test-for-REST-services-to189066.htm#filtered

但我必须对其进行一些修改才能使其与 Symfony 1.4 一起使用。作为形式问题,我引入了一个新类“Browser”,它扩展了“sfBrowser”和“TestFunctional”,它扩展了“sfTestFunctional”。

Browser::uploadFile():

我为内容类型添加了 $type 参数,否则测试会失败('无效的 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;
  }

您必须注意命名:


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

;

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

;
走过海棠暮 2024-09-05 23:48:38

问题是功能测试认为 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 上的额外方法)对我不起作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文