Completed
Push — 2.x ( 843cc1...43b4ce )
by Frank
01:05
created

AwsS3V3AdapterTest.php (18 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Flysystem\AwsS3V3;
6
7
use Aws\Result;
8
use Aws\S3\S3Client;
9
use Aws\S3\S3ClientInterface;
10
use Exception;
11
use Generator;
12
use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase;
13
use League\Flysystem\Config;
14
use League\Flysystem\FileAttributes;
15
use League\Flysystem\FilesystemAdapter;
16
use League\Flysystem\StorageAttributes;
17
use League\Flysystem\UnableToCheckFileExistence;
18
use League\Flysystem\UnableToDeleteFile;
19
use League\Flysystem\UnableToMoveFile;
20
use League\Flysystem\UnableToRetrieveMetadata;
21
use League\Flysystem\UnableToWriteFile;
22
use RuntimeException;
23
24
/**
25
 * @group aws
26
 */
27
class AwsS3V3AdapterTest extends FilesystemAdapterTestCase
28
{
29
    /**
30
     * @var bool
31
     */
32
    private $shouldCleanUp = false;
33
34
    /**
35
     * @var string
36
     */
37
    private static $adapterPrefix = 'test-prefix';
38
39
    /**
40
     * @var S3ClientInterface|null
41
     */
42
    private static $s3Client;
43
44
    /**
45
     * @var S3ClientStub
46
     */
47
    private static $stubS3Client;
48
49
    public static function setUpBeforeClass(): void
50
    {
51
        static::$adapterPrefix = 'ci/' . bin2hex(random_bytes(10));
0 ignored issues
show
Since $adapterPrefix is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $adapterPrefix to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
52
    }
53
54
    protected function tearDown(): void
55
    {
56
        if ( ! $this->shouldCleanUp) {
57
            return;
58
        }
59
60
        $adapter = $this->adapter();
61
        $adapter->deleteDirectory('/');
62
        /** @var StorageAttributes[] $listing */
63
        $listing = $adapter->listContents('', false);
64
65
        foreach ($listing as $item) {
66
            if ($item->isFile()) {
67
                $adapter->delete($item->path());
68
            } else {
69
                $adapter->deleteDirectory($item->path());
70
            }
71
        }
72
73
        self::$adapter = null;
74
    }
75
76
    private static function s3Client(): S3ClientInterface
77
    {
78
        if (static::$s3Client instanceof S3ClientInterface) {
0 ignored issues
show
Since $s3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $s3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
79
            return static::$s3Client;
0 ignored issues
show
Since $s3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $s3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
80
        }
81
82
        $key = getenv('FLYSYSTEM_AWS_S3_KEY');
83
        $secret = getenv('FLYSYSTEM_AWS_S3_SECRET');
84
        $bucket = getenv('FLYSYSTEM_AWS_S3_BUCKET');
85
        $region = getenv('FLYSYSTEM_AWS_S3_REGION') ?: 'eu-central-1';
86
87
        if ( ! $key || ! $secret || ! $bucket) {
88
            self::markTestSkipped('No AWS credentials present for testing.');
89
        }
90
91
        $options = ['version' => 'latest', 'credentials' => compact('key', 'secret'), 'region' => $region];
92
93
        return static::$s3Client = new S3Client($options);
0 ignored issues
show
Since $s3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $s3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function writing_with_a_specific_mime_type(): void
100
    {
101
        $adapter = $this->adapter();
102
        $adapter->write('some/path.txt', 'contents', new Config(['ContentType' => 'text/plain+special']));
103
        $mimeType = $adapter->mimeType('some/path.txt')->mimeType();
104
        $this->assertEquals('text/plain+special', $mimeType);
105
    }
106
107
    /**
108
     * @test
109
     */
110
    public function listing_contents_recursive(): void
111
    {
112
        $adapter = $this->adapter();
113
        $adapter->write('something/0/here.txt', 'contents', new Config());
114
        $adapter->write('something/1/also/here.txt', 'contents', new Config());
115
116
        $contents = iterator_to_array($adapter->listContents('', true));
117
118
        $this->assertCount(2, $contents);
119
        $this->assertContainsOnlyInstancesOf(FileAttributes::class, $contents);
120
        /** @var FileAttributes $file */
121
        $file = $contents[0];
122
        $this->assertEquals('something/0/here.txt', $file->path());
123
        /** @var FileAttributes $file */
124
        $file = $contents[1];
125
        $this->assertEquals('something/1/also/here.txt', $file->path());
126
    }
127
128
    /**
129
     * @test
130
     */
131 View Code Duplication
    public function failing_to_delete_while_moving(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        $adapter = $this->adapter();
134
        $adapter->write('source.txt', 'contents to be copied', new Config());
135
        static::$stubS3Client->failOnNextCopy();
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
136
137
        $this->expectException(UnableToMoveFile::class);
138
139
        $adapter->move('source.txt', 'destination.txt', new Config());
140
    }
141
142
    /**
143
     * @test
144
     */
145 View Code Duplication
    public function failing_to_write_a_file(): void
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $adapter = $this->adapter();
148
        static::$stubS3Client->throwDuringUpload(new RuntimeException('Oh no'));
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
149
150
        $this->expectException(UnableToWriteFile::class);
151
152
        $adapter->write('path.txt', 'contents', new Config());
153
    }
154
155
    /**
156
     * @test
157
     */
158
    public function failing_to_delete_a_file(): void
159
    {
160
        $adapter = $this->adapter();
161
        static::$stubS3Client->throwExceptionWhenExecutingCommand('DeleteObject');
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
162
163
        $this->expectException(UnableToDeleteFile::class);
164
165
        $adapter->delete('path.txt');
166
    }
167
168
    /**
169
     * @test
170
     */
171
    public function fetching_unknown_mime_type_of_a_file(): void
172
    {
173
        $this->adapter();
174
        $result = new Result([
175
            'Key' => static::$adapterPrefix . '/unknown-mime-type.md5',
0 ignored issues
show
Since $adapterPrefix is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $adapterPrefix to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
176
        ]);
177
        static::$stubS3Client->stageResultForCommand('HeadObject', $result);
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
178
179
        parent::fetching_unknown_mime_type_of_a_file();
180
    }
181
182
    /**
183
     * @test
184
     * @dataProvider dpFailingMetadataGetters
185
     */
186
    public function failing_to_retrieve_metadata(Exception $exception, string $getterName): void
187
    {
188
        $adapter = $this->adapter();
189
        $result = new Result([
190
             'Key' => static::$adapterPrefix . '/filename.txt',
0 ignored issues
show
Since $adapterPrefix is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $adapterPrefix to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
191
        ]);
192
        static::$stubS3Client->stageResultForCommand('HeadObject', $result);
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
193
194
        $this->expectExceptionObject($exception);
195
196
        $adapter->{$getterName}('filename.txt');
197
    }
198
199
    public function dpFailingMetadataGetters(): iterable
200
    {
201
        yield "mimeType" => [UnableToRetrieveMetadata::mimeType('filename.txt'), 'mimeType'];
202
        yield "lastModified" => [UnableToRetrieveMetadata::lastModified('filename.txt'), 'lastModified'];
203
        yield "fileSize" => [UnableToRetrieveMetadata::fileSize('filename.txt'), 'fileSize'];
204
    }
205
206
    /**
207
     * @test
208
     */
209
    public function failing_to_check_for_file_existence(): void
210
    {
211
        $adapter = $this->adapter();
212
213
        static::$stubS3Client->throw500ExceptionWhenExecutingCommand('HeadObject');
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
214
215
        $this->expectException(UnableToCheckFileExistence::class);
216
217
        $adapter->fileExists('something-that-does-exist.txt');
218
    }
219
220
    /**
221
     * @test
222
     * @dataProvider casesWhereHttpStreamingInfluencesSeekability
223
     */
224
    public function streaming_reads_are_not_seekable_and_non_streaming_are(bool $streaming, bool $seekable): void
225
    {
226
        if (getenv('COMPOSER_OPTS') === '--prefer-lowest') {
227
            $this->markTestSkipped('The SDK does not support streaming in low versions.');
228
        }
229
230
        $adapter = $this->useAdapter($this->createFilesystemAdapter($streaming));
231
        $this->givenWeHaveAnExistingFile('path.txt');
232
233
        $resource = $adapter->readStream('path.txt');
234
        $metadata = stream_get_meta_data($resource);
235
        fclose($resource);
236
237
        $this->assertEquals($seekable, $metadata['seekable']);
238
    }
239
240
    public function casesWhereHttpStreamingInfluencesSeekability(): Generator
241
    {
242
        yield "not streaming reads have seekable stream" => [false, true];
243
        yield "streaming reads have non-seekable stream" => [true, false];
244
    }
245
246
    /**
247
     * @test
248
     * @dataProvider casesWhereHttpStreamingInfluencesSeekability
249
     */
250
    public function configuring_http_streaming_via_options(bool $streaming): void
251
    {
252
        $adapter = $this->useAdapter($this->createFilesystemAdapter($streaming, ['@http' => ['stream' => false]]));
253
        $this->givenWeHaveAnExistingFile('path.txt');
254
255
        $resource = $adapter->readStream('path.txt');
256
        $metadata = stream_get_meta_data($resource);
257
        fclose($resource);
258
259
        $this->assertTrue($metadata['seekable']);
260
    }
261
262
    /**
263
     * @test
264
     */
265
    public function moving_with_updated_metadata(): void
266
    {
267
        $adapter = $this->adapter();
268
        $adapter->write('source.txt', 'contents to be moved', new Config(['ContentType' => 'text/plain']));
269
        $mimeTypeSource = $adapter->mimeType('source.txt')->mimeType();
270
        $this->assertSame('text/plain', $mimeTypeSource);
271
272
        $adapter->move('source.txt', 'destination.txt', new Config(
273
            ['ContentType' => 'text/plain+special', 'MetadataDirective' => 'REPLACE']
274
        ));
275
        $mimeTypeDestination = $adapter->mimeType('destination.txt')->mimeType();
276
        $this->assertSame('text/plain+special', $mimeTypeDestination);
277
    }
278
279
    protected static function createFilesystemAdapter(bool $streaming = true, array $options = []): FilesystemAdapter
280
    {
281
        static::$stubS3Client = new S3ClientStub(static::s3Client());
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Since s3Client() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of s3Client() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
282
        /** @var string $bucket */
283
        $bucket = getenv('FLYSYSTEM_AWS_S3_BUCKET');
284
        $prefix = getenv('FLYSYSTEM_AWS_S3_PREFIX') ?: static::$adapterPrefix;
0 ignored issues
show
Since $adapterPrefix is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $adapterPrefix to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
285
286
        return new AwsS3V3Adapter(static::$stubS3Client, $bucket, $prefix, null, null, $options, $streaming);
0 ignored issues
show
Since $stubS3Client is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $stubS3Client to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
287
    }
288
}
289