【问题标题】:Test an upload with PHPUnit in Laravel在 Laravel 中使用 PHPUnit 测试上传
【发布时间】:2015-01-24 20:32:15
【问题描述】:

目前,我尝试使用 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

谢谢。

【问题讨论】:

  • Intervention\Image\Exception\NotWritableException: Can't write image data to path (/home/travis/build/tomapp/trombinoscope/public/avatars/AnosOVIO.png) 很清楚...
  • 当然可以,但我不知道该怎么做...
  • 你检查写权限了吗?
  • 你应该检查一下 Maxim 的答案。如果您不知道如何执行此操作,请尝试将文件上传到具有您访问 storage_path() 的权限的文件夹中。你可以了解更多关于 laracasts (laracasts.com/lessons/testing-with-virtual-file-systems)

标签: php laravel upload phpunit


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。这对我很有帮助。但目前我认为它无法工作,因为 Laravel 绝对需要一个 \Symfony\Component\HttpFoundation\File\UploadedFile 实例。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    您可以使用此类 \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,这个测试会将文件写入你的磁盘

    【讨论】:

      猜你喜欢
      • 2016-01-21
      • 2014-09-11
      • 2018-10-23
      • 1970-01-01
      • 2016-06-04
      • 2014-09-28
      • 2016-02-18
      • 2016-08-18
      • 2017-01-11
      相关资源
      最近更新 更多