【问题标题】:Testing File uploads in Symfony2在 Symfony2 中测试文件上传
【发布时间】:2011-10-18 12:30:52
【问题描述】:

在 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

【问题讨论】:

  • 我认为这个问题应该被关闭或标记为已回答,我已经关注了这个对话:github.com/symfony/symfony/pull/1891,这似乎只是文档的问题。

标签: symfony file-upload phpunit integration-testing functional-testing


【解决方案1】:

这是文档中的错误。

固定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));

文档here

【讨论】:

    【解决方案2】:

    这是一个适用于 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()
        );
    }
    

    【讨论】:

    • 在创建的 UploadedFile 对象 $file->isValid() 上失败。知道为什么吗?
    • @astroanu 请添加一个新问题来解释您的问题。
    • 仍然适用于 Symfony 5.0,除了 UploadedFile 构造函数删除了文件大小并添加了 errortest
    【解决方案3】:

    我发现这篇文章,https://coderwall.com/p/vp52ew/symfony2-unit-testing-uploaded-file,效果很好。

    问候

    【讨论】:

    • 这是一个更好的解决方案。尽可能进行单元测试!
    • 请在您的回答中包含代码,以便您的回答在页面消失时仍然有效。
    【解决方案4】:

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

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

    【讨论】:

    • 第五个参数激活“测试”模式,以免在测试中触发HTTP真实上传。
    【解决方案5】:

    即使问题与 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

    【讨论】:

      猜你喜欢
      • 2019-12-28
      • 2016-06-11
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 2016-02-25
      • 2018-03-06
      • 1970-01-01
      相关资源
      最近更新 更多