Support the ongoing development of Laravel.io →
posted 9 years ago
Testing
Last updated 2 years ago.
0

Same question!

0

I wrote ArrayDriver for testing

0

@Londeren could you share this array driver please ? Thx !

0

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();
    }
Last updated 9 years ago.
0

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);
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.