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

AdapterFactoryTest::testBuildAdvancedLocalFs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
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 44
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
            // Not required
47
            FileSystemInfo\LocalInfo::LINK_HANDLING_KEY => LocalFilesystemAdapter::SKIP_LINKS,
48
            FileSystemInfo\LocalInfo::VISIBILITY_KEY => [
49
                'file' => [
50
                    'public' => 0777,
51
                    'private' => 0644,
52
                ],
53
                'dir' => [
54
                    'public' => 0776,
55
                    'private' => 0444,
56
                ],
57
            ],
58
        ];
59
60
        $info = new FileSystemInfo\LocalInfo(
61
            'debugLocal',
62
            [
63
                FileSystemInfo\LocalInfo::ADAPTER_KEY => LocalFilesystemAdapter::class,
64
                FileSystemInfo\LocalInfo::OPTIONS_KEY => $options,
65
            ]
66
        );
67
68
        $adapter = AdapterFactory::build($info);
69
70
        $this->assertInstanceOf(LocalFilesystemAdapter::class, $adapter);
71
72
73
        $this->assertEquals(
74
            $options[FileSystemInfo\LocalInfo::LINK_HANDLING_KEY],
75
            $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

75
            /** @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...
76
        );
77
        $this->assertEquals(
78
            $options[FileSystemInfo\LocalInfo::WRITE_FLAGS_KEY],
79
            $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

79
            /** @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...
80
        );
81
        $this->assertEquals(
82
            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

82
            PortableVisibilityConverter::fromArray(/** @scrutinizer ignore-type */ $options[FileSystemInfo\LocalInfo::VISIBILITY_KEY]),
Loading history...
83
            $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

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

102
            /** @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...
103
        );
104
        $this->assertSame(
105
            $fsInfo->getClient(),
106
            $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

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

138
            /** @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...
139
        );
140
        $this->assertEquals(
141
            $info->getVisibilityConverter(),
142
            $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

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