在Symfony2中测试文件上传 [英] Testing File uploads in Symfony2
问题描述
$ $ p $ $ client $> request('POST',' / submit',array('name'=>'Fabien'),array('photo'=>'/ path / to / photo'));
模拟一个文件上传。
然而,在我所有的测试中,我没有在应用程序的$ request对象中获取任何内容,也没有在$ _FILES数组中。
这是一个简单的WebTestCase,它失败了。它是自包含的,并且测试$ client根据传入的参数构造的请求。它不测试应用程序。
class UploadTest extends WebTestCase
{
public function testNewPhotos()
{
$ client = $ this-> createClient();
$ client->请求(
'POST',
'/ submit',
array('name'=>'Fabien'),
array ('photo'=> __FILE__)
);
$ b $ this-> assertEquals(1,count($ client-> getRequest() - > files-> all()));
$ / code $ / pre
$ b $ 要清楚。这不是关于如何做文件上传的问题,我可以做。这是关于如何在Symfony2中测试它们的。
编辑
我相信我做对了。所以我为框架创建了一个测试,并提出了一个请求。
https://github.com/symfony/symfony/pull/1891
解决方案这是文档中的一个错误。
: https://github.com/symfony/symfony-docs/commit/e6027eb
文档在这里: http://symfony.com/doc/current/book/testing.html#working-with-the-test-client
In the Symfony2 documentation it gives the simple example of:
$client->request('POST', '/submit', array('name' => 'Fabien'), array('photo' => '/path/to/photo'));
To simulate a file upload.
However in all my tests I am getting nothing in the $request object in the app and nothing in the $_FILES array.
Here is a simple WebTestCase which is failing. It is self contained and tests the request that the $client constructs based on the parameters you pass in. It's not testing the app.
class UploadTest extends WebTestCase
{
public function testNewPhotos()
{
$client = $this->createClient();
$client->request(
'POST',
'/submit',
array('name' => 'Fabien'),
array('photo' => __FILE__)
);
$this->assertEquals(1, count($client->getRequest()->files->all()));
}
}
Just to be clear. This is not a question about how to do file uploads, that I can do. It is about how to test them in Symfony2.
Edit
I'm convinced I'm doing it right. So I've created a test for the Framework and made a pull request.
https://github.com/symfony/symfony/pull/1891
解决方案 This was an error in the documentation.
Fixed here: https://github.com/symfony/symfony-docs/commit/e6027eb
Documentation here: http://symfony.com/doc/current/book/testing.html#working-with-the-test-client
这篇关于在Symfony2中测试文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!