I am creating a test for a class that uses the Storage facade in Laravel 5. I Get the error, "ReflectionException: Class filesystem does not exist" when I run the test.
Here is the code for the test
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Storage as Storage;
use App\Fourpoint\services\Material;
use \Illuminate\Container\Container as Container;
use \Illuminate\Support\Facades\Facade as Facade;
class MaterialServiceTest extends TestCase
{
use DatabaseMigrations;
protected $directory;
protected $file;
public function setUp(){
$app = new Container();
$app->singleton('app', 'Illuminate\Container\Container');
Facade::setFacadeApplication($app);
$this -> mat = new \App\FourPoint\services\Material();
$this -> directory = 'home/Code/project4point/tests/mockFiles';
$this -> file = new \Symfony\Component\HttpFoundation\File\UploadedFile('tests/mockFiles/testPDF.pdf', 'testPDF.pdf');
}
public function testNewMaterial(){
Storage::shouldReceive('put')
->andReturn(true);
Storage::shouldReceive('exists')
->andReturn(true);
$this -> mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
}
The error is caused by "Storage::shouldReceive('put')"
It's because your class extends from testCase which already has its own method "setUp" that is supposed to run. When you define the method "setUp" in child classes, you overwrite the method that the parent class has.
Solution: Call the parent class method first like this
public function setUp(){
parent::setUp();
... your own setup
}
You can modify your function as:
public function testNewMaterial()
{
$storage = $this->mockStorage();
$storage->shouldReceive('put')->andReturn(true);
$storage->shouldReceive('exists')->andReturn(true);
$this->mat->newMaterial('pdf',1,1,'testPDF',$this -> file,1,1);
}
protected function mockStorage()
{
Storage::extend('mock', function() {
return \Mockery::mock(\Illuminate\Contracts\Filesystem\Filesystem::class);
});
Config::set('filesystems.disks.mock', ['driver' => 'mock']);
Config::set('filesystems.default', 'mock');
return Storage::disk();
}