Passed
Pull Request — master (#407)
by Kirill
11:08 queued 03:58
created

StorageConfigTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 360
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 153
c 2
b 0
f 0
dl 0
loc 360
rs 10
wmc 18

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testHasServer() 0 17 1
A testGetServersKeys() 0 19 1
A testBuildBucketInfoForMissedBucket() 0 27 1
A testConstructorWrongTmpDirThrowsException() 0 11 1
A getServersListForBuild() 0 5 1
A testConstructorWrongBucketKeyThrowsException() 0 8 1
A testConstructorNoBucketsDoNotThrowsException() 0 7 1
A testConstructorWrongServerKeyThrowsException() 0 8 1
A testGetBucketsKeys() 0 20 1
A testBuildFsInfo() 0 19 2
A testBuildFsInfoUnknownFs() 0 20 1
A testConstructorNoServersDoNotThrowsException() 0 5 1
A testGetTmpDir() 0 7 1
A testBuildFsInfoForLocalCheckForce() 0 26 1
A testBuildFsInfoUnknownAdapter() 0 23 1
A testBuildBucketInfo() 0 31 1
A testBuildFsInfoUnknownServer() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit\Config;
6
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use Spiral\Storage\Config\DTO\FileSystemInfo\FileSystemInfoInterface;
9
use Spiral\Storage\Exception\ConfigException;
10
use Spiral\Storage\Config\DTO\BucketInfoInterface;
11
use Spiral\Storage\Config\DTO\FileSystemInfo;
12
use Spiral\Storage\Config\StorageConfig;
13
use Spiral\Storage\Exception\StorageException;
14
use Spiral\Tests\Storage\Traits\AwsS3FsBuilderTrait;
15
use Spiral\Tests\Storage\Traits\LocalFsBuilderTrait;
16
use Spiral\Tests\Storage\Unit\UnitTestCase;
17
18
class StorageConfigTest extends UnitTestCase
19
{
20
    use LocalFsBuilderTrait;
21
    use AwsS3FsBuilderTrait;
22
23
    public function testConstructorWrongServerKeyThrowsException(): void
24
    {
25
        $this->expectException(ConfigException::class);
26
        $this->expectExceptionMessage('Storage server name must be a string, but int(0) defined');
27
28
        new StorageConfig([
29
            'servers' => [
30
                0 => $this->buildLocalInfoDescription()
31
            ]
32
        ]);
33
    }
34
35
    public function testConstructorWrongBucketKeyThrowsException(): void
36
    {
37
        $this->expectException(ConfigException::class);
38
        $this->expectExceptionMessage('Storage bucket name must be a string, but int(0) defined');
39
40
        new StorageConfig([
41
            'servers' => ['local' => $this->buildLocalInfoDescription()],
42
            'storages' => [0 => $this->buildBucketNameByServer('local')]
43
        ]);
44
    }
45
46
    public function testConstructorNoServersDoNotThrowsException(): void
47
    {
48
        $this->expectNotToPerformAssertions();
49
50
        new StorageConfig(['servers' => []]);
51
    }
52
53
    public function testConstructorNoBucketsDoNotThrowsException(): void
54
    {
55
        $this->expectNotToPerformAssertions();
56
57
        new StorageConfig([
58
            'servers' => ['local' => $this->buildLocalInfoDescription()],
59
            'storages' => []
60
        ]);
61
    }
62
63
    /**
64
     * @dataProvider getServersListForBuild
65
     *
66
     * @param string $serverName
67
     * @param array $serverDescription
68
     * @param string $class
69
     *
70
     * @throws StorageException
71
     */
72
    public function testBuildFsInfo(string $serverName, array $serverDescription, string $class): void
73
    {
74
        $bucketName = $this->buildBucketNameByServer($serverName);
75
        $config = new StorageConfig([
76
            'servers' => [
77
                $serverName => $serverDescription
78
            ],
79
            'storages' => [
80
                $this->buildBucketNameByServer($serverName) => $this->buildServerBucketInfoDesc($serverName),
81
            ]
82
        ]);
83
84
        /** @var FileSystemInfoInterface|FileSystemInfo\OptionsBasedInterface $fs */
85
        $fs = $config->buildFileSystemInfo($bucketName);
86
87
        $this->assertInstanceOf($class, $fs);
88
89
        foreach ($serverDescription[FileSystemInfo\OptionsBasedInterface::OPTIONS_KEY] as $optionKey => $optionVal) {
90
            $this->assertEquals($optionVal, $fs->getOption($optionKey));
0 ignored issues
show
Bug introduced by
The method getOption() does not exist on Spiral\Storage\Config\DT...FileSystemInfoInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Spiral\Storage\Config\DT...FileSystemInfoInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
            $this->assertEquals($optionVal, $fs->/** @scrutinizer ignore-call */ getOption($optionKey));
Loading history...
91
        }
92
    }
93
94
    /**
95
     * @throws StorageException
96
     */
97
    public function testBuildFsInfoForLocalCheckForce(): void
98
    {
99
        $localServer = 'local';
100
        $rootDir = '/debug/root';
101
102
        $bucket = $this->buildBucketNameByServer($localServer);
103
104
        $config = new StorageConfig([
105
            'servers' => [
106
                $localServer => [
107
                    FileSystemInfo\LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
108
                    FileSystemInfo\LocalInfo::OPTIONS_KEY => [
109
                        FileSystemInfo\LocalInfo::ROOT_DIR_KEY => $rootDir,
110
                        FileSystemInfo\LocalInfo::HOST_KEY => self::CONFIG_HOST,
111
                    ],
112
                ],
113
            ],
114
            'storages' => [
115
                $this->buildBucketNameByServer($localServer) => $this->buildServerBucketInfoDesc($localServer),
116
            ]
117
        ]);
118
119
        $fsInfo = $config->buildFileSystemInfo($bucket);
120
121
        $this->assertSame($fsInfo, $config->buildFileSystemInfo($bucket));
122
        $this->assertNotSame($fsInfo, $config->buildFileSystemInfo($bucket, true));
123
    }
124
125
    /**
126
     * @throws StorageException
127
     */
128
    public function testBuildFsInfoUnknownFs(): void
129
    {
130
        $localServer = 'local';
131
        $anotherFs = 'anotherBucket';
132
133
        $config = new StorageConfig([
134
            'servers' => [
135
                $localServer => [
136
                    FileSystemInfoInterface::ADAPTER_KEY => LocalFilesystemAdapter::class,
137
                ],
138
            ],
139
            'storages' => [
140
                $this->buildBucketNameByServer($localServer) => $this->buildServerBucketInfoDesc($localServer),
141
            ]
142
        ]);
143
144
        $this->expectException(ConfigException::class);
145
        $this->expectExceptionMessage(\sprintf('Bucket `%s` was not found', $anotherFs));
146
147
        $config->buildFileSystemInfo($anotherFs);
148
    }
149
150
    /**
151
     * @throws StorageException
152
     */
153
    public function testBuildFsInfoUnknownServer(): void
154
    {
155
        $this->expectException(ConfigException::class);
156
        $this->expectExceptionMessage(
157
            'Storage server `missedServer` of bucket `localBucket` has not been defined, one of [local] required'
158
        );
159
160
        $serverName = 'local';
161
        $bucketName = $this->buildBucketNameByServer($serverName);
162
163
        $config = new StorageConfig([
164
            'servers' => [
165
                $serverName => $this->buildLocalInfoDescription()
166
            ],
167
            'storages' => [
168
                $bucketName => $this->buildServerBucketInfoDesc('missedServer')
169
            ],
170
        ]);
171
172
        /** @var FileSystemInfoInterface|FileSystemInfo\OptionsBasedInterface $fs */
173
        $config->buildFileSystemInfo($bucketName);
174
    }
175
176
    /**
177
     * @throws StorageException
178
     */
179
    public function testBuildFsInfoUnknownAdapter(): void
180
    {
181
        $this->expectException(ConfigException::class);
182
        $this->expectExceptionMessage(
183
            'Storage server `another` adapter must be a class string that ' .
184
            'implements League\Flysystem\FilesystemAdapter interface, but string(DateTime) defined'
185
        );
186
187
        $serverName = 'another';
188
        $bucket = 'anotherBucket';
189
190
        $config = new StorageConfig([
191
            'servers' => [
192
                $serverName => [
193
                    FileSystemInfoInterface::ADAPTER_KEY => \DateTime::class,
194
                ],
195
            ],
196
            'storages' => [
197
                $bucket => $this->buildServerBucketInfoDesc($serverName)
198
            ]
199
        ]);
200
201
        $config->buildFileSystemInfo($bucket);
202
    }
203
204
    /**
205
     * @throws ConfigException
206
     */
207
    public function testGetServersKeys(): void
208
    {
209
        $localServer = 'local';
210
        $awsServer = 'aws';
211
212
        $config = new StorageConfig(
213
            [
214
                'servers' => [
215
                    $localServer => $this->buildLocalInfoDescription(),
216
                    $awsServer => $this->buildAwsS3ServerDescription(),
217
                ],
218
                'storages' => [
219
                    $this->buildBucketNameByServer($localServer) => $this->buildServerBucketInfoDesc($localServer),
220
                    $this->buildBucketNameByServer($awsServer) => $this->buildServerBucketInfoDesc($awsServer),
221
                ]
222
            ]
223
        );
224
225
        $this->assertEquals([$localServer, $awsServer], $config->getServersKeys());
226
    }
227
228
    /**
229
     * @throws ConfigException
230
     */
231
    public function testGetBucketsKeys(): void
232
    {
233
        $localServer = 'local';
234
235
        $config = new StorageConfig(
236
            [
237
                'servers' => [
238
                    $localServer => $this->buildLocalInfoDescription(),
239
                ],
240
                'storages' => [
241
                    'b1' => $this->buildServerBucketInfoDesc($localServer),
242
                    'b2' => [
243
                        'server' => $localServer,
244
                        'directory' => 'tmp/specDir/',
245
                    ],
246
                ],
247
            ]
248
        );
249
250
        $this->assertEquals(['b1', 'b2'], $config->getBucketsKeys());
251
    }
252
253
    /**
254
     * @throws ConfigException
255
     */
256
    public function testHasServer(): void
257
    {
258
        $localServer = 'local';
259
260
        $config = new StorageConfig(
261
            [
262
                'servers' => [
263
                    $localServer => $this->buildLocalInfoDescription(),
264
                ],
265
                'storages' => [
266
                    $this->buildBucketNameByServer($localServer) => $this->buildServerBucketInfoDesc($localServer),
267
                ],
268
            ]
269
        );
270
271
        $this->assertTrue($config->hasServer($localServer));
272
        $this->assertFalse($config->hasServer('missing'));
273
    }
274
275
    /**
276
     * @throws ConfigException
277
     */
278
    public function testGetTmpDir(): void
279
    {
280
        $config = new StorageConfig();
281
        $this->assertEquals(\sys_get_temp_dir(), $config->getTmpDir());
282
283
        $config = new StorageConfig(['tmp-dir' => __DIR__]);
284
        $this->assertEquals(__DIR__, $config->getTmpDir());
285
    }
286
287
    /**
288
     * @throws ConfigException
289
     */
290
    public function testConstructorWrongTmpDirThrowsException(): void
291
    {
292
        $tmpDir = '/my+=Dir/some#3Dir/tmp';
293
294
        $this->expectException(ConfigException::class);
295
        $this->expectExceptionMessage(\sprintf(
296
            'Storage temporary directory `%s` must be a valid directory',
297
            $tmpDir
298
        ));
299
300
        new StorageConfig(['tmp-dir' => $tmpDir]);
301
    }
302
303
    /**
304
     * @throws ConfigException
305
     * @throws StorageException
306
     */
307
    public function testBuildBucketInfo(): void
308
    {
309
        $localServer = 'local';
310
        $awsServer = 'aws';
311
312
        $localBucket1 = 'local1B';
313
        $localBucket2 = 'local2B';
314
315
        $config = new StorageConfig(
316
            [
317
                'servers' => [
318
                    $localServer => $this->buildLocalInfoDescription(),
319
                    $awsServer => $this->buildAwsS3ServerDescription(),
320
                ],
321
                'storages' => [
322
                    $localBucket1 => [
323
                        BucketInfoInterface::SERVER_KEY => $localServer,
324
                        BucketInfoInterface::DIRECTORY_KEY => '/dir1',
325
                    ],
326
                    $localBucket2 => [
327
                        BucketInfoInterface::SERVER_KEY => $localServer,
328
                        BucketInfoInterface::DIRECTORY_KEY => '/dir2',
329
                    ]
330
                ],
331
            ]
332
        );
333
334
        $bucketInfo = $config->buildBucketInfo($localBucket1);
335
        $this->assertInstanceOf(BucketInfoInterface::class, $bucketInfo);
336
337
        $this->assertSame($bucketInfo, $config->buildBucketInfo($localBucket1));
338
    }
339
340
    /**
341
     * @throws ConfigException
342
     * @throws StorageException
343
     */
344
    public function testBuildBucketInfoForMissedBucket(): void
345
    {
346
        $localServer = 'local';
347
        $awsServer = 'aws';
348
349
        $localBucket1 = 'localB';
350
        $missedBucket = 'missedB';
351
352
        $config = new StorageConfig(
353
            [
354
                'servers' => [
355
                    $localServer => $this->buildLocalInfoDescription(),
356
                    $awsServer => $this->buildAwsS3ServerDescription(),
357
                ],
358
                'storages' => [
359
                    $localBucket1 => [
360
                        BucketInfoInterface::SERVER_KEY => $localServer,
361
                        BucketInfoInterface::DIRECTORY_KEY => '/dir1',
362
                    ],
363
                ],
364
            ]
365
        );
366
367
        $this->expectException(StorageException::class);
368
        $this->expectExceptionMessage('Bucket `missedB` was not found');
369
370
        $config->buildBucketInfo($missedBucket);
371
    }
372
373
    public function getServersListForBuild(): array
374
    {
375
        return [
376
            ['local', $this->buildLocalInfoDescription(), FileSystemInfo\LocalInfo::class],
377
            ['awsS3', $this->buildAwsS3ServerDescription(), FileSystemInfo\Aws\AwsS3Info::class],
378
        ];
379
    }
380
}
381