3

Currently, i try to test a file upload with PHPUnit for Laravel. After some searches, i found a first solution :

$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]
                ]
);

It works, but it failed when I push this code on Travis, and i'm not sure it's very clean to do that...

You can see my test failed here.

Thank's you.

Thomas Leduc
  • 31
  • 1
  • 4
  • `Intervention\Image\Exception\NotWritableException: Can't write image data to path (/home/travis/build/tomapp/trombinoscope/public/avatars/AnosOVIO.png)` is pretty clear... – ceejayoz Nov 26 '14 at 14:26
  • For sure, but i have no idea to do otherwise... – Thomas Leduc Nov 26 '14 at 15:10
  • did you checked write permissions? – vaske Aug 13 '15 at 14:55
  • You should check for Maxim's answer. If you do not know how to do that, try to upload files in a folder that has permsissions like the ones you access storage_path() . You can lear more on laracasts (https://laracasts.com/lessons/testing-with-virtual-file-systems) – user237329 Sep 22 '16 at 04:51

3 Answers3

3

You should use virtual file system for your tests. Check mocking file system in the phpunit documentation

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
  • Thank's for your answer. That's really help me. But currently i think it can't work cause of Laravel wich need absolutely an \Symfony\Component\HttpFoundation\File\UploadedFile instance. – Thomas Leduc Nov 28 '14 at 14:38
2

You can use this class \Illuminate\Http\UploadedFile to test your upload file as you can see on this answer

mine working using test case like this :

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);
  }


}

when you are testing upload using file please use \Illuminate\Http\UploadedFile instead of \Symfony\Component\HttpFoundation\File\UploadedFile

UPDATE when seeing your test I think it is incomplete you can get the response of your test like mine here :

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');
}

Note : I'm using laravel 5.2, and this test will write the file on your disk

Community
  • 1
  • 1
Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
-2

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

jackinos
  • 84
  • 4