在Laravel中使用PHPUnit测试上传

时间:2014-11-26 13:24:46

标签: php laravel upload phpunit

目前,我尝试使用PHPUnit for Laravel测试文件上传。经过一些搜索,我找到了第一个解决方案:

$file = new \Symfony\Component\HttpFoundation\File\UploadedFile(
            app_path() . '/tests/Files/default.png', 'default.png');

$response = $this->call('POST', 'students', 
                [
                   'firstname' => 'firstname', 
                   'lastname'  => 'lastname', 
                   'promotion' => '2016', 
                   'isCoop' => 1
                ], 
                [
                   'avatar' => [$file]
                ]
);

它有效,但是当我在Travis上推送这段代码时它失败了,我不确定这样做是否非常干净......

您可以看到我的测试失败here

谢谢你。

3 个答案:

答案 0 :(得分:3)

您应该使用virtual file system进行测试。检查phpunit文档中的mocking file system

答案 1 :(得分:0)

您可以使用此课程\Illuminate\Http\UploadedFile来测试上传文件,您可以在answer

上看到 我使用这样的测试用例开始工作:

public function testUploadLanscapseValid()
{
  $uploadFile= new \Illuminate\Http\UploadedFile(
      base_path()."/resources/fakerFile/mobileScreen/bg_land.jpg", //path image
      'example.jpg',
      'image/jpeg',
      filesize(base_path()."/resources/fakerFile/mobileScreen/bg_land.jpg"), // file size
      null,
      true
  );

  //checking UI validation response
  $this->visit('/mobile-screen')
      ->attach($uploadFile, 'image-landscape')
      ->press('upload-image-landscapse')
      ->seePageIs('/mobile-screen')
      ->see('Mobile screen successfully uploaded.');

  //checking database is inserted
  $this->seeInDatabase('mobile_screen',['link_lanscapse'=>'bg_land.jpg']);

  //checking file exist
  if(file_exists(base_path() . '/public/mobileScreen/bg_land.jpg')){
    $this->assertTrue(true);
  }else{
    $this->assertTrue(false);
  }


}

使用文件测试上传时,请使用\Illuminate\Http\UploadedFile代替\Symfony\Component\HttpFoundation\File\UploadedFile

<强>更新 在看到你的测试时,我认为它是不完整的,你可以像我这样得到你的测试的响应:

public function testUploadFunctionalTest()
{
  $uploadedFile= new \Illuminate\Http\UploadedFile(
      base_path()."/resources/fakerFile/mobileScreen/bg_land.jpg", //path image
      'example.jpg',
      'image/jpeg',
      filesize(base_path()."/resources/fakerFile/mobileScreen/bg_land.jpg"), // file size
      null,
      true
  );

  // you can use this or
  $parameters = [];
  $response = $this->action(
      'POST',
      'AdminWeb\MobileScreenController@store',
      [],
      $parameters,
      [],
      ['image' => $uploadedFile]
  );

  // you can use this choose One !
  $response  =$this->call('POST', 'mobile-screen@store',
            [
               'firstname' => 'firstname',
               'lastname'  => 'lastname',
               'promotion' => '2016',
               'isCoop' => 1
            ],
            [
               'image' => [$uploadedFile]
            ]);

  $result = json_decode($response->content()); // you can dump or whatever you want with the test
  var_dump($result->your_response);
  $this->assertEquals($result->your_response,'expected output');
}

注意:我正在使用laravel 5.2,此测试会将文件写入您的磁盘

答案 2 :(得分:-2)

$ file = new \ Symfony \ Component \ HttpFoundation \ File \ UploadedFile(             app_path()。 &#39; /tests/Files/default.png' ;,&#39; default.png&#39;,&#34; image / jpeg&#34;,null,null,true);