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

getFsInfoListForResolversPrepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit;
6
7
use Spiral\Storage\Config\DTO\FileSystemInfo\FileSystemInfoInterface;
8
use Spiral\Storage\Exception\ConfigException;
9
use Spiral\Storage\Exception\ResolveException;
10
use Spiral\Storage\Exception\StorageException;
11
use Spiral\Storage\Exception\UriException;
12
use Spiral\Storage\Parser\UriParserInterface;
13
use Spiral\Storage\Resolver;
14
use Spiral\Storage\Resolver\AdapterResolverInterface;
15
use Spiral\Storage\Resolver\LocalSystemResolver;
16
use Spiral\Storage\UriResolver;
17
use Spiral\Tests\Storage\Traits\AwsS3FsBuilderTrait;
18
use Spiral\Tests\Storage\Traits\LocalFsBuilderTrait;
19
20
class ResolveManagerTest extends UnitTestCase
21
{
22
    use LocalFsBuilderTrait;
23
    use AwsS3FsBuilderTrait;
24
25
    /**
26
     * @var string
27
     */
28
    private const LOCAL_SERVER_1 = 'local';
29
30
    /**
31
     * @var string
32
     */
33
    private const LOCAL_SERVER_2 = 'local2';
34
35
    /**
36
     * @var string
37
     */
38
    private const LOCAL_SERVER_ROOT_2 = '/some/specific/root/';
39
40
    /**
41
     * @var string
42
     */
43
    private const LOCAL_SERVER_HOST_2 = 'http://my.images.com/';
44
45
    /**
46
     * @throws ConfigException
47
     * @throws \ReflectionException
48
     */
49
    public function testGetResolverFailed(): void
50
    {
51
        $this->notice('This is an unreliable test because it invokes a non-public implementation method');
52
53
        $config = $this->buildStorageConfig([
54
            'local' => $this->buildLocalInfoDescription(),
55
        ]);
56
57
        $resolver = new UriResolver($config, $this->getUriParser());
58
59
        $missedFs = 'missedFs';
60
61
        $this->expectException(ConfigException::class);
62
        $this->expectExceptionMessage(\sprintf('Bucket `%s` was not found', $missedFs));
63
64
        $this->callNotPublicMethod($resolver, 'getResolver', [$missedFs]);
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

64
        /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($resolver, 'getResolver', [$missedFs]);

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...
65
    }
66
67
    /**
68
     * @dataProvider getFsInfoListForResolversPrepare
69
     *
70
     * @param FileSystemInfoInterface $fsInfo
71
     * @param string $expectedClass
72
     *
73
     * @throws \ReflectionException
74
     * @throws ConfigException
75
     */
76
    public function testPrepareResolverForFileSystem(FileSystemInfoInterface $fsInfo, string $expectedClass): void
77
    {
78
        $this->notice('This is an unreliable test because it invokes a non-public implementation method');
79
80
        $config = $this->buildStorageConfig([
81
            'local' => $this->buildLocalInfoDescription(),
82
            'aws'   => $this->buildAwsS3ServerDescription(),
83
        ]);
84
85
        $resolver = new UriResolver($config, $this->getUriParser());
86
87
        /** @var AdapterResolverInterface $resolver */
88
        $resolver = $this->callNotPublicMethod($resolver, 'prepareResolverForFileSystem', [$fsInfo]);
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

88
        $resolver = /** @scrutinizer ignore-deprecated */ $this->callNotPublicMethod($resolver, 'prepareResolverForFileSystem', [$fsInfo]);

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...
89
90
        $this->assertInstanceOf($expectedClass, $resolver);
91
    }
92
93
    /**
94
     * @throws StorageException
95
     */
96
    public function testBuildUrlThrowException(): void
97
    {
98
        $uri = 'some:/+/someFile.txt';
99
100
        $config = $this->buildStorageConfig([
101
            self::LOCAL_SERVER_1 => $this->buildLocalInfoDescription(),
102
        ]);
103
104
        $resolver = new UriResolver($config, $this->getUriParser());
105
106
        $this->expectException(UriException::class);
107
        $this->expectExceptionMessage('Filesystem pathname can not be empty');
108
109
        $resolver->resolve($uri);
110
    }
111
112
    /**
113
     * @throws StorageException
114
     */
115
    public function testBuildUrlThrowableException(): void
116
    {
117
        $uri = 'local://someFile.txt';
118
119
        $exceptionMsg = 'Some unhandled exception';
120
121
        $uriParser = $this->createMock(UriParserInterface::class);
122
        $uriParser->expects($this->once())
123
            ->method('parse')
124
            ->willThrowException(new \Exception($exceptionMsg))
125
        ;
126
127
        $config = $this->buildStorageConfig([
128
            self::LOCAL_SERVER_1 => $this->buildLocalInfoDescription()
129
        ]);
130
131
        $resolver = new UriResolver($config, $uriParser);
132
133
        $this->expectException(ResolveException::class);
134
        $this->expectExceptionMessage($exceptionMsg);
135
136
        $resolver->resolve($uri);
137
    }
138
139
    /**
140
     * @throws StorageException
141
     */
142
    public function testBuildUrlWrongFormatThrowsException(): void
143
    {
144
        $config = $this->buildStorageConfig([
145
            self::LOCAL_SERVER_1 => $this->buildLocalInfoDescription()
146
        ]);
147
148
        $resolver = new UriResolver($config, $this->getUriParser());
149
150
        $this->expectException(ConfigException::class);
151
        $this->expectExceptionMessage('Bucket `unknown` was not found');
152
153
        $resolver->resolve('unknown://someFile.txt');
154
    }
155
156
    /**
157
     * @return array[]
158
     *
159
     * @throws StorageException
160
     */
161
    public function getFsInfoListForResolversPrepare(): array
162
    {
163
        return [
164
            [$this->buildLocalInfo('localBucket'), LocalSystemResolver::class],
165
            [$this->buildAwsS3Info('awsBucket'), Resolver\AwsS3Resolver::class],
166
        ];
167
    }
168
}
169