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

LocalSystemResolverTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 178
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildUrl() 0 24 1
A testBuildUrlNoHost() 0 23 1
A testNormalizePathForServer() 0 14 1
A testWrongFsInfo() 0 19 1
A getUriListForNormalize() 0 36 2
A getFileUrlList() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit\Resolver;
6
7
use League\Flysystem\Local\LocalFilesystemAdapter;
8
use Spiral\Storage\Config\DTO\FileSystemInfo\Aws\AwsS3Info;
9
use Spiral\Storage\Config\DTO\FileSystemInfo\LocalInfo;
10
use Spiral\Storage\Config\StorageConfig;
11
use Spiral\Storage\Exception\ResolveException;
12
use Spiral\Storage\Exception\StorageException;
13
use Spiral\Storage\Resolver\LocalSystemResolver;
14
use Spiral\Tests\Storage\Traits\AwsS3FsBuilderTrait;
15
use Spiral\Tests\Storage\Traits\LocalFsBuilderTrait;
16
use Spiral\Tests\Storage\Unit\UnitTestCase;
17
18
class LocalSystemResolverTest extends UnitTestCase
19
{
20
    use LocalFsBuilderTrait;
21
    use AwsS3FsBuilderTrait;
22
23
    /**
24
     * @throws StorageException
25
     */
26
    public function testWrongFsInfo(): void
27
    {
28
        $server = 'aws';
29
30
        $this->expectException(StorageException::class);
31
        $this->expectExceptionMessage(
32
            \sprintf(
33
                'Wrong filesystem info (`%s`) for resolver `%s`',
34
                AwsS3Info::class,
35
                LocalSystemResolver::class
36
            )
37
        );
38
39
        new LocalSystemResolver(
40
            $this->getUriParser(),
41
            $this->buildStorageConfig(
42
                [$server => $this->buildAwsS3ServerDescription()]
43
            ),
44
            $this->buildBucketNameByServer($server)
45
        );
46
    }
47
48
    /**
49
     * @dataProvider getFileUrlList
50
     *
51
     * @param string $serverName
52
     * @param string $host
53
     * @param string $uri
54
     * @param string $rootDir
55
     * @param string $expectedUrl
56
     *
57
     * @throws StorageException
58
     */
59
    public function testBuildUrl(
60
        string $serverName,
61
        string $host,
62
        string $rootDir,
63
        string $uri,
64
        string $expectedUrl
65
    ): void {
66
        $resolver = new LocalSystemResolver(
67
            $this->getUriParser(),
68
            $this->buildStorageConfig(
69
                [
70
                    $serverName => [
71
                        LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
72
                        LocalInfo::OPTIONS_KEY => [
73
                            LocalInfo::ROOT_DIR_KEY => $rootDir,
74
                            LocalInfo::HOST_KEY => $host,
75
                        ],
76
                    ],
77
                ]
78
            ),
79
            $this->buildBucketNameByServer($serverName)
80
        );
81
82
        $this->assertEquals($expectedUrl, $resolver->buildUrl($uri));
83
    }
84
85
    /**
86
     * @throws StorageException
87
     */
88
    public function testBuildUrlNoHost(): void
89
    {
90
        $server = 'some';
91
92
        $resolver = new LocalSystemResolver(
93
            $this->getUriParser(),
94
            $this->buildStorageConfig(
95
                [
96
                    $server => [
97
                        LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
98
                        LocalInfo::OPTIONS_KEY => [
99
                            LocalInfo::ROOT_DIR_KEY => 'rootDir',
100
                        ],
101
                    ]
102
                ]
103
            ),
104
            $this->buildBucketNameByServer($server)
105
        );
106
107
        $this->expectException(ResolveException::class);
108
        $this->expectExceptionMessage('Url can\'t be built for filesystem `someBucket` - host was not defined');
109
110
        $resolver->buildUrl('file1.txt');
111
    }
112
113
    /**
114
     * @dataProvider getUriListForNormalize
115
     *
116
     * @param string $filePath
117
     * @param string $uri
118
     *
119
     * @throws StorageException
120
     */
121
    public function testNormalizePathForServer(string $filePath, string $uri): void
122
    {
123
        $server = 'local';
124
        $resolver = new LocalSystemResolver(
125
            $this->getUriParser(),
126
            $this->buildStorageConfig(
127
                [
128
                    $server => $this->buildLocalInfoDescription(),
129
                ]
130
            ),
131
            $this->buildBucketNameByServer($server)
132
        );
133
134
        $this->assertEquals($uri, $resolver->normalizeFilePath($filePath));
135
    }
136
137
    public function getFileUrlList(): array
138
    {
139
        $fileTxt = 'file.txt';
140
        $specificCsvFile = '/some/specific/dir/file1.csv';
141
142
        return [
143
            [
144
                self::SERVER_NAME,
145
                self::CONFIG_HOST,
146
                self::ROOT_DIR,
147
                $fileTxt,
148
                \sprintf('%s%s', self::CONFIG_HOST, $fileTxt),
149
            ],
150
            [
151
                self::SERVER_NAME,
152
                self::CONFIG_HOST,
153
                self::ROOT_DIR,
154
                $specificCsvFile,
155
                \sprintf('%s%s', self::CONFIG_HOST, $specificCsvFile),
156
            ],
157
        ];
158
    }
159
160
    public function getUriListForNormalize(): array
161
    {
162
        $bucketName = $this->buildBucketNameByServer(self::SERVER_NAME);
163
164
        $result = [
165
            [
166
                \sprintf('%s://some/dir/%s', $bucketName, 'file.txt'),
167
                'some/dir/file.txt',
168
            ],
169
            [
170
                \sprintf('%s//%s', $bucketName, 'file.txt'),
171
                \sprintf('%s//%s', $bucketName, 'file.txt'),
172
            ],
173
        ];
174
175
        $filesList = [
176
            'file.txt',
177
            'file2-.txt',
178
            'file_4+.gif',
179
            '412391*.jpg',
180
            'file+*(1)128121644.png',
181
            'file spaces-and-some-chars 2.jpg',
182
            'File(part 1).png',
183
            'File-part+2_.png',
184
        ];
185
186
        foreach ($filesList as $fileName) {
187
            $result[] = [
188
                \sprintf('%s://%s', $bucketName, $fileName),
189
                $fileName,
190
            ];
191
192
            $result[] = [$fileName, $fileName];
193
        }
194
195
        return $result;
196
    }
197
}
198