Framework: unable to test filesystem temporaryUrl

Created on 21 Oct 2017  路  9Comments  路  Source: laravel/framework

  • Laravel Version: 5.5.11
  • PHP Version: 7.1
  • Database Driver & Version: MySQL

Description:

I have written some test to verify that a file exists on s3 and download when tried to test Storage::disk('s3')->temporaryUrl($url, $urlExpires); It gives 500 error with this msg but the code works when test by the browser.

This driver does not support creating temporary URLs

Steps To Reproduce:

Test

Storage::fake('s3');

// Create attachment
$attachment = Attachment::create([
    'name' => 'PDF Doc',
    'url' => Storage::disk('s3')->put(
        'attachments', 
        UploadedFile::fake()->create('document.pdf'), 
    1000)
]);

Storage::disk('s3')->assertExists($attachment->url);
...
$this->actingAs($user)
    ->get(route('attachments.download', $attachment->id))
    ->assertRedirect()
// it fails with with 500

Implementaion

$url = Attachment::find($id)->url;

if( Storage::disk('s3')->exists($url) ) {
    // link expiration time
    $urlExpires = Carbon::now()->addMinutes(10);

    try {
        $tempUrl = Storage::disk('s3')->temporaryUrl($url, $urlExpires);
        return redirect($tempUrl);
    } catch ( \Exception $e ) {
        // Unable to test temporaryUrl, its giving driver dont support it issue.
        return response($e->getMessage());
    }
}

One more question?
Is there any way to customize the URL generated by temporaryUrl(), problem is downloaded filename is some random string rlbPsVUhqubaXGoEQk96wjPXfQTqurJvNgVjRMZ8.doc which is not very good, I like to customize the name of file to something like project-details.doc etc 馃毝

Most helpful comment

This is actually a bug, though. Minimal repro case:

public function test_foo()
{
    Storage::fake('s3');
    Storage::disk('s3')->temporaryUrl('some-url-here', Carbon::parse('tomorrow'));
}

You'll get this error:
[2018-04-09 13:44:49] testing.ERROR: RuntimeException: This driver does not support creating temporary URL s.

Shouldn't the fake implementation respond to all of the same methods the real one does? At the very least, return the URL it was given, or no-op so that my test case doesn't fail just because I can't call this method on this adapter. This will muddy up the real test case which I need this behavior.

All 9 comments

This repo is for bug tracking. Use the forums or slack channel for solving your issue

Please ask on the forums, this repo is for bug reporting only. You can use https://laracasts.com/discuss or https://laravel.io/forum which are forums with a very large community of developers helping each other.

This is actually a bug, though. Minimal repro case:

public function test_foo()
{
    Storage::fake('s3');
    Storage::disk('s3')->temporaryUrl('some-url-here', Carbon::parse('tomorrow'));
}

You'll get this error:
[2018-04-09 13:44:49] testing.ERROR: RuntimeException: This driver does not support creating temporary URL s.

Shouldn't the fake implementation respond to all of the same methods the real one does? At the very least, return the URL it was given, or no-op so that my test case doesn't fail just because I can't call this method on this adapter. This will muddy up the real test case which I need this behavior.

Can't we just return the $path if the filesystem adapter doesn't support temporary URLs, instead of throwing an exception?

I just experienced the same issue. We solved it by doing something like this:

Storage::shouldReceive('cloud')->twice()->andReturn(new class() {
    public function temporaryUrl() {
        return 'foo';
    }
});

Hi @driesvints, could you take a second look at this?
Just returning a URL or a fake singed URL when faking a disk seems like a good solution.

We don't support this Laravel version anymore.

@driesvints This still exists in the current version of Laravel.

I think it's best that you send in a PR if something is broken here. There's a good workaround this right above here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kerbylav picture kerbylav  路  3Comments

shopblocks picture shopblocks  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

YannPl picture YannPl  路  3Comments

ghost picture ghost  路  3Comments