I came up with the following code:
public function testSomethingWithStorage()
{
$storage = $this->mockStorage();
$storage->shouldReceive('get')->once()->andReturn('test');
$storage->shouldReceive('put')->once()->andThrow(new Exception());
/* another code here */
}
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();
}
It's easier than that. You handle it as a chained object:
Storage::shouldReceive('disk->get')->once()->andReturn($contentString);
That was tested and works for me. But if you want to test the argument being sent to the 'disk' method as well, I think you can do something like this:
Storage::shouldReceive('disk')->with($diskName)->shouldReceive('get')->once()->andReturn($contentString);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community