Подтвердить что ты не робот

Тестирование загрузки файлов в Symfony2

В документации Symfony2 приведен простой пример:

$client->request('POST', '/submit', array('name' => 'Fabien'), array('photo' => '/path/to/photo'));

Чтобы имитировать загрузку файла.

Однако во всех моих тестах я ничего не получаю в объекте $request в приложении и ничего в массиве $_FILES.

Вот простой WebTestCase, который терпит неудачу. Он является самодостаточным и проверяет запрос, который строит клиент $на основе параметров, которые вы передаете. Он не тестирует приложение.

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.

Edit

Я убежден, что делаю все правильно. Поэтому я создал тест для Framework и сделал запрос на pull. https://github.com/symfony/symfony/pull/1891

4b9b3361

Ответ 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()
    );
}

Ответ 3

Я думаю, что этот вопрос должен быть закрыт или отмечен как ответ, я следую этому разговору: github.com/symfony/symfony/pull/1891 и кажется, что это была проблема с документацией.