我发现了这个: http: //www.devcomments.com/Uploads-on-functional-test-for-REST-services-to189066.htm#filtered
但我不得不稍微修改它以使其与 Symfony 1.4 一起工作。作为形式问题,我引入了一个新类“Browser”,它扩展了“sfBrowser”和“TestFunctional”,它扩展了“sfTestFunctional”。
浏览器::上传文件():
我为 content-type 添加了 $type 参数,否则测试失败('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;
}
您必须注意命名:
$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'
)))
;