Passed
Pull Request — master (#407)
by Kirill
05:29
created

testDetermineFilesystemAndPathWrongFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit;
6
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\FilesystemOperator;
9
use Spiral\Storage\Builder\AdapterFactory;
10
use Spiral\Storage\Config\StorageConfig;
11
use Spiral\Storage\Exception\MountException;
12
use Spiral\Storage\Exception\StorageException;
13
use Spiral\Storage\Exception\UriException;
14
use Spiral\Storage\Storage;
15
16
/**
17
 * tests for basic StorageEngine methods
18
 */
19
class StorageEngineTest extends StorageEngineAbstractTest
20
{
21
    /**
22
     * @var string
23
     */
24
    private const DEFAULT_FS = 'default';
25
26
    /**
27
     * @var Storage
28
     */
29
    private $storage;
30
31
    /**
32
     * @var FilesystemOperator
33
     */
34
    private $localFileSystem;
35
36
    /**
37
     * @throws StorageException
38
     * @throws \ReflectionException
39
     */
40
    protected function setUp(): void
41
    {
42
        parent::setUp();
43
44
        $this->localFileSystem = new Filesystem(
45
            AdapterFactory::build(
46
                $this->buildLocalInfo(self::SERVER_NAME, false)
47
            )
48
        );
49
50
        $storageConfig = $this->buildStorageConfig([
51
            self::DEFAULT_FS => $this->buildLocalInfoDescription()
52
        ]);
53
54
        $this->storage = new Storage($storageConfig, $this->getUriParser());
55
        $this->mountStorageEngineFileSystem(
56
            $this->storage,
57
            $this->buildBucketNameByServer(self::SERVER_NAME),
58
            $this->localFileSystem
59
        );
60
    }
61
62
    /**
63
     * @throws StorageException
64
     */
65
    public function testConstructorWithFileSystems(): void
66
    {
67
        $local1Name = 'local1';
68
        $local2Name = 'local2';
69
70
        $fsList = [
71
            $this->buildBucketNameByServer($local1Name),
72
            $this->buildBucketNameByServer($local2Name)
73
        ];
74
75
        $storageConfig = $this->buildStorageConfig([
76
            $local1Name => $this->buildLocalInfoDescription(),
77
            $local2Name => $this->buildLocalInfoDescription(),
78
        ]);
79
80
        $storage = new Storage($storageConfig, $this->getUriParser());
81
82
        foreach ($fsList as $key) {
83
            $this->assertInstanceOf(FilesystemOperator::class, $storage->getFileSystem($key));
84
        }
85
86
        $this->assertEquals($fsList, $storage->getFileSystemsNames());
87
    }
88
89
    /**
90
     * @throws StorageException
91
     */
92
    public function testMountSystemsByConfig(): void
93
    {
94
        $local1Name = 'local1';
95
        $local2Name = 'local2';
96
97
        $fsList = [$this->buildBucketNameByServer($local1Name), $this->buildBucketNameByServer($local2Name)];
98
99
        $storageConfig = $this->buildStorageConfig(
100
            [
101
                $local1Name => $this->buildLocalInfoDescription(),
102
                $local2Name => $this->buildLocalInfoDescription(),
103
            ]
104
        );
105
106
        $storage = new Storage($storageConfig, $this->getUriParser());
107
108
        foreach ($fsList as $key) {
109
            $this->assertInstanceOf(FilesystemOperator::class, $storage->getFileSystem($key));
110
        }
111
112
        $this->assertEquals($fsList, $storage->getFileSystemsNames());
113
    }
114
115
    public function testIsFileSystemExists(): void
116
    {
117
        $this->assertTrue(
118
            $this->callNotPublicMethod(
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

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

118
            /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
119
                $this->storage,
120
                'isFileSystemExists',
121
                [$this->buildBucketNameByServer(self::SERVER_NAME)]
122
            )
123
        );
124
        $this->assertFalse(
125
            $this->callNotPublicMethod(
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

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

125
            /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
126
                $this->storage,
127
                'isFileSystemExists',
128
                ['missed']
129
            )
130
        );
131
    }
132
133
    /**
134
     * @throws MountException
135
     */
136
    public function testGetFileSystem(): void
137
    {
138
        $this->assertSame(
139
            $this->localFileSystem,
140
            $this->storage->getFileSystem($this->buildBucketNameByServer(self::SERVER_NAME))
141
        );
142
    }
143
144
    /**
145
     * @throws MountException
146
     */
147
    public function testGetMissedFileSystem(): void
148
    {
149
        $this->expectException(MountException::class);
150
        $this->expectExceptionMessage('Filesystem `missed` has not been defined');
151
152
        $this->storage->getFileSystem('missed');
153
    }
154
155
    public function testExtractMountedFileSystemsKeys(): void
156
    {
157
        $this->assertEquals(
158
            [
159
                $this->buildBucketNameByServer(static::DEFAULT_FS),
160
                $this->buildBucketNameByServer(self::SERVER_NAME)
161
            ],
162
            $this->storage->getFileSystemsNames()
163
        );
164
    }
165
166
    /**
167
     * @throws StorageException
168
     * @throws \ReflectionException
169
     */
170
    public function testMountExistingFileSystemKeyThrowsException(): void
171
    {
172
        $bucket = $this->buildBucketNameByServer(self::SERVER_NAME);
173
174
        $newFileSystem = new Filesystem(
175
            AdapterFactory::build(
176
                $this->buildLocalInfo(self::SERVER_NAME, false)
177
            )
178
        );
179
180
        $this->expectException(MountException::class);
181
        $this->expectExceptionMessage(
182
            \sprintf('Filesystem %s is already mounted', $bucket)
183
        );
184
185
        $this->mountStorageEngineFileSystem($this->storage, $bucket, $newFileSystem);
186
187
        $this->assertSame($this->storage->getFileSystem($bucket), $this->localFileSystem);
188
    }
189
190
    /**
191
     * @dataProvider getUrisInfoToDetermine
192
     *
193
     * @param Storage $storage
194
     * @param string $uri
195
     * @param FilesystemOperator $filesystem
196
     * @param string $filePath
197
     *
198
     * @throws \ReflectionException
199
     */
200
    public function testDetermineFilesystemAndPath(
201
        Storage $storage,
202
        string $uri,
203
        FilesystemOperator $filesystem,
204
        string $filePath
205
    ): void {
206
        $this->notice('This is an unreliable test because it invokes a non-public implementation method');
207
208
        $determined = $this->callNotPublicMethod($storage, 'determineFilesystemAndPath', [$uri]);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

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

208
        $determined = /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($storage, 'determineFilesystemAndPath', [$uri]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
209
210
        $this->assertEquals($determined[0], $filesystem);
211
        $this->assertEquals($determined[1], $filePath);
212
    }
213
214
    /**
215
     * @throws \ReflectionException
216
     */
217
    public function testDetermineFilesystemAndPathUnknownFs(): void
218
    {
219
        $this->notice('This is an unreliable test because it invokes a non-public implementation method');
220
221
        $this->expectException(StorageException::class);
222
        $this->expectExceptionMessage('Filesystem `missed` has not been defined');
223
224
        $this->callNotPublicMethod($this->storage, 'determineFilesystemAndPath', ['missed://file.txt']);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

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

224
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($this->storage, 'determineFilesystemAndPath', ['missed://file.txt']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
225
    }
226
227
    /**
228
     * @throws \ReflectionException
229
     */
230
    public function testDetermineFilesystemAndPathWrongFormat(): void
231
    {
232
        $this->notice('This is an unreliable test because it invokes a non-public implementation method');
233
234
        $file = 'missed:/-/file.txt';
235
        $this->expectException(UriException::class);
236
        $this->expectExceptionMessage('Filesystem pathname can not be empty');
237
238
        $this->callNotPublicMethod($this->storage, 'determineFilesystemAndPath', [$file]);
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...::callNotPublicMethod() has been deprecated: Tests should not use this method to call internal implementation ( Ignorable by Annotation )

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

238
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($this->storage, 'determineFilesystemAndPath', [$file]);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
239
    }
240
241
    /**
242
     * @return array[]
243
     *
244
     * @throws StorageException
245
     */
246
    public function getUrisInfoToDetermine(): array
247
    {
248
        $localName = 'local';
249
        $localFs = new Filesystem(AdapterFactory::build($this->buildLocalInfo($localName)));
250
251
        $local2Name = 'local2';
252
        $local2Fs = new Filesystem(AdapterFactory::build($this->buildLocalInfo($local2Name)));
253
254
        $storageConfig = $this->buildStorageConfig([
255
            $localName => $this->buildLocalInfoDescription(),
256
            $local2Name => $this->buildLocalInfoDescription(),
257
        ]);
258
259
        $storage = new Storage($storageConfig, $this->getUriParser());
260
261
        return [
262
            [
263
                $storage,
264
                'localBucket://myDir/somefile.txt',
265
                $localFs,
266
                'myDir/somefile.txt',
267
            ],
268
            [
269
                $storage,
270
                'local2Bucket://somefile.txt',
271
                $local2Fs,
272
                'somefile.txt',
273
            ]
274
        ];
275
    }
276
}
277