How to test file upload with laravel and phpunit?

前端 未结 6 1898
名媛妹妹
名媛妹妹 2020-12-11 14:32

I\'m trying to run this functional test on my laravel controller. I would like to test image processing, but to do so I want to fake image uploading. How do I do this? I fou

6条回答
  •  遥遥无期
    2020-12-11 15:16

    Add similar setUp() method into your testcase:

    protected function setUp()
    {
        parent::setUp();
    
        $_FILES = array(
            'image'    =>  array(
                'name'      =>  'test.jpg',
                'tmp_name'  =>  __DIR__ . '/_files/phpunit-test.jpg',
                'type'      =>  'image/jpeg',
                'size'      =>  499,
                'error'     =>  0
            )
        );
    }
    

    This will spoof your $_FILES global and let Laravel think that there is something uploaded.

提交回复
热议问题