0

I am trying to create a test in laravel 5.7 that involves creating a fake image object which is further consumed by a service that moves it to a desired directory in public folder. My service works fine if I upload file through browser but if I run through tests , it creates a folder in root and uploads image there.

Service code:

$data['icon'] = time().'.'.$file->getClientOriginalExtension();
$file->move('uploads/avatar/icon', $data['icon']);

Test file code:

$file = UploadedFile::fake()->image('random.jpg');

Now if I specify path in service code , it gives desired result

$data['icon'] = time().'.'.$file->getClientOriginalExtension();
$file->move(public_path('uploads/avatar/icon'), $data['icon']);

Is there any way without hard-coding the public_path dependency?

James Z
  • 12,209
  • 10
  • 24
  • 44
DevOps
  • 438
  • 6
  • 21

1 Answers1

1

Seems like you are using a different filesystem disc which has a different root path in your testing environment. I recommend setting up a .env.testing file.

  • I am already using .env.testing but what changes do I need to make for testing upload disc – DevOps Jan 28 '19 at 13:19
  • Check what disk you are actually using in your testing environement and what the root path of that disc is. If it's not set to `public` that's probably what's wrong. Make sure `FILESYSTEM_DRIVER` is set correctly in `.env.testing` and that it's not being changed at runtime. –  Jan 28 '19 at 13:30
  • I have tried but not working. Both test and live environments are using local driver – DevOps Jan 28 '19 at 13:46
  • What is `$file->move()` actually doing? Seems like it's not the same as `Storage::move();` –  Jan 28 '19 at 14:18
  • Both are used for moving files – DevOps Jan 29 '19 at 06:31
  • I gathered as much but you're asking why it behaves differently dependant on environment. Maybe a good idea to post the code or simply use `Storage::move` which behaves predictably. –  Jan 29 '19 at 08:07
  • My main concern is why move() function is behaving strange in normal case vs test case. Its uploading files to public directory as default but failed to do so in test case even using same default drivers. – DevOps Jan 29 '19 at 08:58