在 Symfony2 中测试文件上传

发布于 2024-11-27 10:46:05 字数 1054 浏览 2 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(5

转身以后 2024-12-04 10:46:05

这是文档中的错误。

修复了此处

use Symfony\Component\HttpFoundation\File\UploadedFile;

$photo = new UploadedFile('/path/to/photo.jpg', 'photo.jpg', 'image/jpeg', 123);
// or
$photo = array('tmp_name' => '/path/to/photo.jpg', 'name' => 'photo.jpg', 'type' => 'image/jpeg', 'size' => 123, 'error' => UPLOAD_ERR_OK);

$client = static::createClient();
$client->request('POST', '/submit', array('name' => 'Fabien'), array('photo' => $photo));

文档此处

This was an error in the documentation.

Fixed here:

use Symfony\Component\HttpFoundation\File\UploadedFile;

$photo = new UploadedFile('/path/to/photo.jpg', 'photo.jpg', 'image/jpeg', 123);
// or
$photo = array('tmp_name' => '/path/to/photo.jpg', 'name' => 'photo.jpg', 'type' => 'image/jpeg', 'size' => 123, 'error' => UPLOAD_ERR_OK);

$client = static::createClient();
$client->request('POST', '/submit', array('name' => 'Fabien'), array('photo' => $photo));

Documentation here

Smile简单爱 2024-12-04 10:46:05

这是一个适用于 Symfony 2.3 的代码(我没有尝试使用其他版本):

我创建了一个 photo.jpg 图像文件并将其放入 Acme\Bundle\Tests\uploads< /代码>。

以下是 Acme\Bundle\Tests\Controller\AcmeTest.php 的摘录:

function testUpload()
{
    // Open the page
    ...

    // Select the file from the filesystem
    $image = new UploadedFile(
        // Path to the file to send
        dirname(__FILE__).'/../uploads/photo.jpg',
        // Name of the sent file
        'filename.jpg',
        // MIME type
        'image/jpeg',
        // Size of the file
        9988
    );

    // Select the form (adapt it for your needs)
    $form = $crawler->filter('input[type=submit]...')->form();

    // Put the file in the upload field
    $form['... name of your field ....']->upload($image);

    // Send it
    $crawler = $this->client->submit($form);

    // Check that the file has been successfully sent
    //  (in my case the filename is displayed in a <a> link so I check
    //  that it appears on the page)
    $this->assertEquals(
        1,
        $crawler->filter('a:contains("filename.jpg")')->count()
    );
}

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 in Acme\Bundle\Tests\uploads.

Here is an excerpt from Acme\Bundle\Tests\Controller\AcmeTest.php:

function testUpload()
{
    // Open the page
    ...

    // Select the file from the filesystem
    $image = new UploadedFile(
        // Path to the file to send
        dirname(__FILE__).'/../uploads/photo.jpg',
        // Name of the sent file
        'filename.jpg',
        // MIME type
        'image/jpeg',
        // Size of the file
        9988
    );

    // Select the form (adapt it for your needs)
    $form = $crawler->filter('input[type=submit]...')->form();

    // Put the file in the upload field
    $form['... name of your field ....']->upload($image);

    // Send it
    $crawler = $this->client->submit($form);

    // Check that the file has been successfully sent
    //  (in my case the filename is displayed in a <a> link so I check
    //  that it appears on the page)
    $this->assertEquals(
        1,
        $crawler->filter('a:contains("filename.jpg")')->count()
    );
}
伏妖词 2024-12-04 10:46:05

即使问题与 Symfony2 相关,当在 Google 中搜索 Symfony4 时,它也会出现在顶部结果中。

实例化 UploadedFile 是可行的,但我发现的最短方法实际上也在官方文档中:

$crawler = $client->request('GET', '/post/hello-world');
$buttonCrawlerNode = $crawler->selectButton('submit');
$form = $buttonCrawlerNode->form();
$form['photo']->upload('/path/to/lucas.jpg');

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:

$crawler = $client->request('GET', '/post/hello-world');
$buttonCrawlerNode = $crawler->selectButton('submit');
$form = $buttonCrawlerNode->form();
$form['photo']->upload('/path/to/lucas.jpg');

https://symfony.com/doc/current/testing.html#forms

无人问我粥可暖 2024-12-04 10:46:05

我找到了这篇文章 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

烟火散人牵绊 2024-12-04 10:46:05

如果你想在没有客户端请求的情况下模拟 UploadedFile,你可以使用:

    $path = 'path/to/file.test';
    $originalName = 'original_name.test';
    $file = new UploadedFile($path, $originalName, null, UPLOAD_ERR_OK, true);
    $testSubject->method($file);

if you want to mock a UploadedFile without a client request, you can use:

    $path = 'path/to/file.test';
    $originalName = 'original_name.test';
    $file = new UploadedFile($path, $originalName, null, UPLOAD_ERR_OK, true);
    $testSubject->method($file);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文