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

AdapterFactoryTest::testBuildAdvancedLocalFs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 27
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 43
rs 9.488
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Tests\Storage\Unit\Builder;
6
7
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
8
use League\Flysystem\Local\LocalFilesystemAdapter;
9
use League\Flysystem\PathPrefixer;
10
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
11
use PHPUnit\Framework\MockObject\MockObject;
12
use Spiral\Storage\Builder\AdapterFactory;
13
use Spiral\Storage\Config\DTO\FileSystemInfo;
14
use Spiral\Storage\Exception\StorageException;
15
use Spiral\Tests\Storage\Traits\AwsS3FsBuilderTrait;
16
use Spiral\Tests\Storage\Traits\LocalFsBuilderTrait;
17
use Spiral\Tests\Storage\Unit\UnitTestCase;
18
19
class AdapterFactoryTest extends UnitTestCase
20
{
21
    use LocalFsBuilderTrait;
22
    use AwsS3FsBuilderTrait;
23
24
    /**
25
     * @throws StorageException
26
     */
27
    public function testBuildSimpleLocalFs(): void
28
    {
29
        $info = $this->buildLocalInfo();
30
31
        $adapter = AdapterFactory::build($info);
32
33
        $this->assertInstanceOf(LocalFilesystemAdapter::class, $adapter);
34
    }
35
36
    /**
37
     * @throws StorageException
38
     * @throws \ReflectionException
39
     */
40
    public function testBuildAdvancedLocalFs(): void
41
    {
42
        $options = [
43
            FileSystemInfo\LocalInfo::ROOT_DIR_KEY => self::ROOT_DIR,
44
            FileSystemInfo\LocalInfo::HOST_KEY => self::CONFIG_HOST,
45
            FileSystemInfo\LocalInfo::WRITE_FLAGS_KEY => LOCK_NB,
46
            FileSystemInfo\LocalInfo::LINK_HANDLING_KEY => LocalFilesystemAdapter::SKIP_LINKS,
47
            FileSystemInfo\LocalInfo::VISIBILITY_KEY => [
48
                'file' => [
49
                    'public' => 0777,
50
                    'private' => 0644,
51
                ],
52
                'dir' => [
53
                    'public' => 0776,
54
                    'private' => 0444,
55
                ],
56
            ],
57
        ];
58
59
        $info = new FileSystemInfo\LocalInfo(
60
            'debugLocal',
61
            [
62
                FileSystemInfo\LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
63
                FileSystemInfo\LocalInfo::OPTIONS_KEY => $options,
64
            ]
65
        );
66
67
        $adapter = AdapterFactory::build($info);
68
69
        $this->assertInstanceOf(LocalFilesystemAdapter::class, $adapter);
70
71
72
        $this->assertEquals(
73
            $options[FileSystemInfo\LocalInfo::LINK_HANDLING_KEY],
74
            $this->getNotPublicProperty($adapter, 'linkHandling')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

74
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'linkHandling')

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...
75
        );
76
        $this->assertEquals(
77
            $options[FileSystemInfo\LocalInfo::WRITE_FLAGS_KEY],
78
            $this->getNotPublicProperty($adapter, 'writeFlags')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

78
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'writeFlags')

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...
79
        );
80
        $this->assertEquals(
81
            PortableVisibilityConverter::fromArray($options[FileSystemInfo\LocalInfo::VISIBILITY_KEY]),
0 ignored issues
show
Bug introduced by
It seems like $options[Spiral\Storage\...alInfo::VISIBILITY_KEY] can also be of type integer and string; however, parameter $permissionMap of League\Flysystem\UnixVis...yConverter::fromArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

81
            PortableVisibilityConverter::fromArray(/** @scrutinizer ignore-type */ $options[FileSystemInfo\LocalInfo::VISIBILITY_KEY]),
Loading history...
82
            $this->getNotPublicProperty($adapter, 'visibility')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

82
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'visibility')

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...
83
        );
84
    }
85
86
    /**
87
     * @throws StorageException
88
     * @throws \ReflectionException
89
     */
90
    public function testBuildSimpleAwsS3Fs(): void
91
    {
92
        $fsDescription = $this->buildAwsS3ServerDescription();
93
        $fsInfo = new FileSystemInfo\Aws\AwsS3Info('awsS3', $fsDescription);
94
95
        $adapter = AdapterFactory::build($fsInfo);
96
97
        $this->assertInstanceOf(AwsS3V3Adapter::class, $adapter);
98
99
        $this->assertEquals(
100
            $fsDescription[FileSystemInfo\Aws\AwsS3Info::OPTIONS_KEY][FileSystemInfo\Aws\AwsS3Info::BUCKET_KEY],
101
            $this->getNotPublicProperty($adapter, 'bucket')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

101
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'bucket')

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...
102
        );
103
        $this->assertSame(
104
            $fsInfo->getClient(),
105
            $this->getNotPublicProperty($adapter, 'client')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

105
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'client')

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...
106
        );
107
    }
108
109
    /**
110
     * @throws StorageException
111
     * @throws \ReflectionException
112
     */
113
    public function testBuildAdvancedAwsS3Fs(): void
114
    {
115
        $options = [
116
            FileSystemInfo\Aws\AwsS3Info::BUCKET_KEY => 'testBucket',
117
            FileSystemInfo\Aws\AwsS3Info::CLIENT_KEY => $this->getAwsS3Client(),
118
            FileSystemInfo\Aws\AwsS3Info::PATH_PREFIX_KEY => '/some/prefix/',
119
            FileSystemInfo\Aws\AwsS3Info::VISIBILITY_KEY => $this->getAwsS3VisibilityOption(),
120
        ];
121
122
        $info = new FileSystemInfo\Aws\AwsS3Info(
123
            'debugAwsS3',
124
            [
125
                FileSystemInfo\LocalInfo::ADAPTER_KEY => AwsS3V3Adapter::class,
126
                FileSystemInfo\LocalInfo::OPTIONS_KEY => $options,
127
            ]
128
        );
129
130
        $adapter = AdapterFactory::build($info);
131
132
        $this->assertInstanceOf(AwsS3V3Adapter::class, $adapter);
133
134
135
        $this->assertEquals(
136
            new PathPrefixer($options[FileSystemInfo\Aws\AwsS3Info::PATH_PREFIX_KEY]),
137
            $this->getNotPublicProperty($adapter, 'prefixer')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

137
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'prefixer')

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...
138
        );
139
        $this->assertEquals(
140
            $info->getVisibilityConverter(),
141
            $this->getNotPublicProperty($adapter, 'visibility')
0 ignored issues
show
Deprecated Code introduced by
The function Spiral\Tests\Storage\Uni...:getNotPublicProperty() 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

141
            /** @scrutinizer ignore-deprecated */ $this->getNotPublicProperty($adapter, 'visibility')

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...
142
        );
143
    }
144
145
    public function testWrongFsInfoUsage(): void
146
    {
147
        $this->expectException(StorageException::class);
148
        $this->expectExceptionMessage('Adapter can\'t be built by filesystem info');
149
150
        /** @var MockObject|FileSystemInfo\FileSystemInfo $info */
151
        $info = $this->getMockForAbstractClass(
152
            FileSystemInfo\FileSystemInfo::class,
153
            [
154
                'someName',
155
                [
156
                    FileSystemInfo\FileSystemInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
157
                    FileSystemInfo\FileSystemInfo::OPTIONS_KEY => [],
158
                ],
159
            ]
160
        );
161
162
        AdapterFactory::build($info);
163
    }
164
}
165