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.

4
  • Intervention\Image\Exception\NotWritableException: Can't write image data to path (/home/travis/build/tomapp/trombinoscope/public/avatars/AnosOVIO.png) is pretty clear... Commented Nov 26, 2014 at 14:26
  • For sure, but i have no idea to do otherwise... Commented Nov 26, 2014 at 15:10
  • did you checked write permissions? Commented Aug 13, 2015 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 (laracasts.com/lessons/testing-with-virtual-file-systems) Commented Sep 22, 2016 at 4:51

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.