Passed
Pull Request — master (#407)
by Kirill
06:59
created

AwsS3InfoTest::testIsAdvancedUsage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 28
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit\Config\DTO\FileSystemInfo\Aws;
6
7
use Aws\S3\S3Client;
8
use League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter;
9
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
10
use League\Flysystem\AwsS3V3\PortableVisibilityConverter;
11
use Spiral\Storage\Exception\ConfigException;
12
use Spiral\Storage\Config\DTO\FileSystemInfo\Aws\AwsS3Info;
13
use Spiral\Storage\Exception\StorageException;
14
use Spiral\Tests\Storage\Traits\AwsS3FsBuilderTrait;
15
use Spiral\Tests\Storage\Unit\UnitTestCase;
16
17
class AwsS3InfoTest extends UnitTestCase
18
{
19
    use AwsS3FsBuilderTrait;
20
21
    /**
22
     * @throws StorageException
23
     */
24
    public function testValidateSimple(): void
25
    {
26
        $options = [
27
            AwsS3Info::BUCKET_KEY => 'debugBucket',
28
            AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
29
        ];
30
31
        $fsInfo = new AwsS3Info(
32
            'some',
33
            [
34
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
35
                AwsS3Info::OPTIONS_KEY => $options,
36
            ]
37
        );
38
39
        $this->assertEquals(AwsS3V3Adapter::class, $fsInfo->getAdapterClass());
40
41
        foreach ($options as $optionKey => $optionVal) {
42
            $this->assertEquals($optionVal, $fsInfo->getOption($optionKey));
43
        }
44
45
        $this->assertNull($fsInfo->getVisibilityConverter());
46
    }
47
48
    /**
49
     * @throws StorageException
50
     */
51
    public function testValidateSimpleAsync(): void
52
    {
53
        $options = [
54
            AwsS3Info::BUCKET_KEY => 'debugBucket',
55
            AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
56
        ];
57
58
        $fsInfo = new AwsS3Info(
59
            'some',
60
            [
61
                AwsS3Info::ADAPTER_KEY => AsyncAwsS3Adapter::class,
62
                AwsS3Info::OPTIONS_KEY => $options,
63
            ]
64
        );
65
66
        $this->assertEquals(AsyncAwsS3Adapter::class, $fsInfo->getAdapterClass());
67
68
        foreach ($options as $optionKey => $optionVal) {
69
            $this->assertEquals($optionVal, $fsInfo->getOption($optionKey));
70
        }
71
    }
72
73
    /**
74
     * @throws StorageException
75
     */
76
    public function testAdvancedUsage(): void
77
    {
78
        $options = [
79
            AwsS3Info::BUCKET_KEY => 'debugBucket',
80
            AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
81
            AwsS3Info::PATH_PREFIX_KEY => 'somePrefix',
82
            AwsS3Info::VISIBILITY_KEY => $this->getAwsS3VisibilityOption(),
83
        ];
84
85
        $fsInfo = new AwsS3Info(
86
            'some',
87
            [
88
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
89
                AwsS3Info::OPTIONS_KEY => $options,
90
            ]
91
        );
92
93
        $this->assertTrue($fsInfo->isAdvancedUsage());
94
        foreach ($options as $optionKey => $optionVal) {
95
            $this->assertEquals($optionVal, $fsInfo->getOption($optionKey));
96
        }
97
98
        $visibilityConvertor = $fsInfo->getVisibilityConverter();
99
        $this->assertInstanceOf(PortableVisibilityConverter::class, $visibilityConvertor);
100
        $this->assertSame($visibilityConvertor, $fsInfo->getVisibilityConverter());
101
    }
102
103
    /**
104
     * @throws StorageException
105
     */
106
    public function testAdvancedUsageAsync(): void
107
    {
108
        $options = [
109
            AwsS3Info::BUCKET_KEY => 'debugBucket',
110
            AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
111
            AwsS3Info::PATH_PREFIX_KEY => 'somePrefix',
112
            AwsS3Info::VISIBILITY_KEY => $this->getAwsS3VisibilityOption(),
113
        ];
114
115
        $advancedAwsS3Info = new AwsS3Info(
116
            'some',
117
            [
118
                AwsS3Info::ADAPTER_KEY => AsyncAwsS3Adapter::class,
119
                AwsS3Info::OPTIONS_KEY => $options,
120
            ]
121
        );
122
123
        $this->assertTrue($advancedAwsS3Info->isAdvancedUsage());
124
        foreach ($options as $optionKey => $optionVal) {
125
            $this->assertEquals($optionVal, $advancedAwsS3Info->getOption($optionKey));
126
        }
127
128
        $visibilityConvertor = $advancedAwsS3Info->getVisibilityConverter();
129
        $this->assertInstanceOf(PortableVisibilityConverter::class, $visibilityConvertor);
130
        $this->assertSame($visibilityConvertor, $advancedAwsS3Info->getVisibilityConverter());
131
    }
132
133
    /**
134
     * @throws StorageException
135
     */
136
    public function testGetClient(): void
137
    {
138
        $options = [
139
            AwsS3Info::BUCKET_KEY => 'debugBucket',
140
            AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
141
        ];
142
143
        $fsInfo = new AwsS3Info(
144
            'some',
145
            [
146
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
147
                AwsS3Info::OPTIONS_KEY => $options,
148
            ]
149
        );
150
151
        $client = $fsInfo->getClient();
152
        $this->assertInstanceOf(S3Client::class, $client);
153
        $this->assertSame($client, $fsInfo->getClient());
154
    }
155
156
    /**
157
     * @dataProvider getMissedRequiredOptions
158
     *
159
     * @param string $fsName
160
     * @param array $options
161
     * @param string $exceptionMsg
162
     *
163
     * @throws StorageException
164
     */
165
    public function testValidateRequiredOptionsFailed(string $fsName, array $options, string $exceptionMsg): void
166
    {
167
        $this->expectException(ConfigException::class);
168
        $this->expectExceptionMessage($exceptionMsg);
169
170
        new AwsS3Info(
171
            $fsName,
172
            [
173
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
174
                AwsS3Info::OPTIONS_KEY => $options,
175
            ]
176
        );
177
    }
178
179
    /**
180
     * @throws StorageException
181
     */
182
    public function testValidateVisibilityOptionWrongTypeFailed(): void
183
    {
184
        $this->expectException(ConfigException::class);
185
        $this->expectExceptionMessage(
186
            'Option `visibility` defined in wrong format for filesystem `some`, array expected'
187
        );
188
189
        new AwsS3Info(
190
            'some',
191
            [
192
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
193
                AwsS3Info::OPTIONS_KEY => [
194
                    AwsS3Info::BUCKET_KEY => 'someBucket',
195
                    AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
196
                    AwsS3Info::VISIBILITY_KEY => 12,
197
                ],
198
            ]
199
        );
200
    }
201
202
    /**
203
     * @throws StorageException
204
     */
205
    public function testValidateVisibilityOptionWrongValueFailed(): void
206
    {
207
        $this->expectException(ConfigException::class);
208
        $this->expectExceptionMessage('`visibility` should be defined with one of values: public,private');
209
210
        new AwsS3Info(
211
            'some',
212
            [
213
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
214
                AwsS3Info::OPTIONS_KEY => [
215
                    AwsS3Info::BUCKET_KEY => 'someBucket',
216
                    AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
217
                    AwsS3Info::VISIBILITY_KEY => [
218
                        AwsS3Info::CLASS_KEY => PortableVisibilityConverter::class,
219
                        AwsS3Info::OPTIONS_KEY => [
220
                            AwsS3Info::VISIBILITY_KEY => 12,
221
                        ]
222
                    ],
223
                ],
224
            ]
225
        );
226
    }
227
228
    /**
229
     * @throws StorageException
230
     */
231
    public function testValidateOptionalOptionsPathPrefixFailed(): void
232
    {
233
        $this->expectException(ConfigException::class);
234
        $this->expectExceptionMessage(
235
            'Option `path-prefix` defined in wrong format for filesystem `some`, string expected'
236
        );
237
238
        new AwsS3Info(
239
            'some',
240
            [
241
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
242
                AwsS3Info::OPTIONS_KEY => [
243
                    AwsS3Info::BUCKET_KEY => 'someBucket',
244
                    AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
245
                    AwsS3Info::PATH_PREFIX_KEY => [1, 2],
246
                ],
247
            ]
248
        );
249
    }
250
251
    /**
252
     * @throws StorageException
253
     */
254
    public function testIsAdvancedUsage(): void
255
    {
256
        $simpleAwsS3 = new AwsS3Info(
257
            'some',
258
            [
259
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
260
                AwsS3Info::OPTIONS_KEY => [
261
                    AwsS3Info::BUCKET_KEY => 'debugBucket',
262
                    AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
263
                ],
264
            ]
265
        );
266
267
        $this->assertFalse($simpleAwsS3->isAdvancedUsage());
268
269
        $advancedAwsS3Info = new AwsS3Info(
270
            'some',
271
            [
272
                AwsS3Info::ADAPTER_KEY => AwsS3V3Adapter::class,
273
                AwsS3Info::OPTIONS_KEY => [
274
                    AwsS3Info::BUCKET_KEY => 'debugBucket',
275
                    AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
276
                    AwsS3Info::PATH_PREFIX_KEY => 'somePrefix',
277
                ],
278
            ]
279
        );
280
281
        $this->assertTrue($advancedAwsS3Info->isAdvancedUsage());
282
    }
283
284
    public function getWrongUrlExpiresList(): array
285
    {
286
        $fsName = self::SERVER_NAME;
287
        $errorMsgPrefix = 'Url expires should be string or DateTimeInterface implemented object for filesystem ';
288
289
        return [
290
            [
291
                $fsName,
292
                [new \DateTime('+1 hour')],
293
                $errorMsgPrefix . self::SERVER_NAME,
294
            ],
295
            [
296
                $fsName,
297
                null,
298
                $errorMsgPrefix . self::SERVER_NAME,
299
            ],
300
            [
301
                'some',
302
                true,
303
                $errorMsgPrefix . 'some',
304
            ],
305
        ];
306
    }
307
308
    public function getMissedRequiredOptions(): array
309
    {
310
        $fsName = self::SERVER_NAME;
311
312
        return [
313
            [
314
                $fsName,
315
                [],
316
                \sprintf('Option `bucket` not detected for filesystem `%s`', $fsName),
317
            ],
318
            [
319
                $fsName,
320
                [AwsS3Info::CLIENT_KEY => 'client'],
321
                \sprintf('Option `bucket` not detected for filesystem `%s`', $fsName),
322
            ],
323
            [
324
                'some',
325
                [AwsS3Info::BUCKET_KEY => 'someBucket'],
326
                'Option `client` not detected for filesystem `some`',
327
            ],
328
        ];
329
    }
330
}
331