在 Symfony2 中测试文件上传
在 Symfony2 文档中,它给出了一个简单的示例:
$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->request(
'POST',
'/submit',
array('name' => 'Fabien'),
array('photo' => __FILE__)
);
$this->assertEquals(1, count($client->getRequest()->files->all()));
}
}
澄清一下。这不是一个关于如何上传文件的问题,我可以做到。这是关于如何在 Symfony2 中测试它们。
编辑
我确信我做得对。因此,我为框架创建了一个测试并提出了拉取请求。 https://github.com/symfony/symfony/pull/1891
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是文档中的错误。
修复了此处:
文档此处
This was an error in the documentation.
Fixed here:
Documentation here
这是一个适用于 Symfony 2.3 的代码(我没有尝试使用其他版本):
我创建了一个
photo.jpg
图像文件并将其放入Acme\Bundle\Tests\uploads< /代码>。
以下是 Acme\Bundle\Tests\Controller\AcmeTest.php 的摘录:
Here is a code which works with Symfony 2.3 (I didn't tried with another version):
I created an
photo.jpg
image file and put it inAcme\Bundle\Tests\uploads
.Here is an excerpt from
Acme\Bundle\Tests\Controller\AcmeTest.php
:即使问题与 Symfony2 相关,当在 Google 中搜索 Symfony4 时,它也会出现在顶部结果中。
实例化
UploadedFile
是可行的,但我发现的最短方法实际上也在官方文档中:https://symfony.com/doc/current/testing.html#forms
Even if the question is related to Symfony2, it appears in the top results when searching for Symfony4 in Google.
Instancing an
UploadedFile
works, but the shortest way I found was also actually in the official documentation:https://symfony.com/doc/current/testing.html#forms
我找到了这篇文章 https://coderwall.com/p/vp52ew/ symfony2-unit-testing-uploaded-file,并且工作完美。
问候
I found this article, https://coderwall.com/p/vp52ew/symfony2-unit-testing-uploaded-file, and works perfect.
Regards
如果你想在没有客户端请求的情况下模拟 UploadedFile,你可以使用:
if you want to mock a UploadedFile without a client request, you can use: