Passed
Push — main ( 259db7...ab0778 )
by Thierry
04:06
created

StorageTest.php$0 ➔ testWriteError()   A

Complexity

Conditions 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
namespace Jaxon\Storage\Tests\TestStorage;
4
5
use Jaxon\Config\Config;
6
use Jaxon\Config\ConfigSetter;
7
use Jaxon\Storage\StorageException;
8
use Jaxon\Storage\StorageManager;
9
use Lagdo\Facades\ContainerWrapper;
10
use League\Flysystem\CorruptedPathDetected;
11
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
12
use PHPUnit\Framework\TestCase;
13
use Psr\Container\ContainerInterface;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
17
use function dirname;
18
use function file_get_contents;
19
20
class StorageTest extends TestCase
21
{
22
    /**
23
     * @var StorageManager
24
     */
25
    protected $xManager;
26
27
    /**
28
     * @var ConfigSetter
29
     */
30
    protected $xConfigSetter;
31
32
    /**
33
     * @var string
34
     */
35
    protected $sInputDir;
36
37
    public static function setUpBeforeClass(): void
38
    {
39
        ContainerWrapper::setContainer(new class implements ContainerInterface {
40
            private $xLogger = null;
41
42
            public function has(string $class): bool
43
            {
44
                return $class === LoggerInterface::class;
45
            }
46
47
            public function get(string $class): mixed
48
            {
49
                return $class !== LoggerInterface::class ? null :
50
                    ($this->xLogger ??= new NullLogger());
51
            }
52
        }, false);
53
    }
54
55
    public function setUp(): void
56
    {
57
        $this->sInputDir = dirname(__DIR__) . '/files';
58
        $this->xManager = new StorageManager();
59
        $this->xConfigSetter = new ConfigSetter();
60
    }
61
62
    private function setConfigOptions(array $aOptions, string $sPrefix = '')
63
    {
64
        $this->xManager->setConfigGetter(fn(): Config =>
65
            $this->xConfigSetter->newConfig($aOptions, $sPrefix));
66
    }
67
68
    /**
69
     * @throws StorageException
70
     */
71
    public function testStorageReader()
72
    {
73
        $xInputStorage = $this->xManager->adapter('local')->make($this->sInputDir);
74
        $sInputContent = $xInputStorage->read('hello.txt');
75
76
        $this->assertEquals(file_get_contents("{$this->sInputDir}/hello.txt"), $sInputContent);
77
    }
78
79
    public function testAdapterAndDirOptions()
80
    {
81
        $this->setConfigOptions([
82
            'adapters' => [
83
                'files' => [
84
                    'alias' => 'local',
85
                    'options' => [
86
                        'lazyRootCreation' => false, // Create dirs if they don't exist.
87
                    ],
88
                ],
89
            ],
90
            'stores' => [
91
                'files' => [
92
                    'adapter' => 'files',
93
                    'dir' => $this->sInputDir,
94
                    'options' => [
95
                        'config' => [
96
                            'public_url' => '/static/files',
97
                        ],
98
                    ],
99
                ],
100
            ],
101
        ]);
102
103
        $xInputStorage = $this->xManager->get('files');
104
        $sInputContent = $xInputStorage->read('hello.txt');
105
106
        $this->assertEquals(file_get_contents("{$this->sInputDir}/hello.txt"), $sInputContent);
107
        $this->assertEquals('/static/files/hello.txt', $xInputStorage->publicUrl('hello.txt'));
108
    }
109
110
    public function testWriteError()
111
    {
112
        $this->setConfigOptions([
113
            'adapters' => [
114
                'files' => [
115
                    'alias' => 'local',
116
                    'options' => [
117
                        'lazyRootCreation' => true, // Don't create dirs if they don't exist.
118
                    ],
119
                ],
120
            ],
121
            'stores' => [
122
                'files' => [
123
                    'adapter' => 'files',
124
                    'dir' => dirname(__DIR__ . '/files'),
125
                    'options' => [
126
                        'config' => [
127
                            'public_url' => '/static/files',
128
                        ],
129
                    ],
130
                ],
131
            ],
132
        ]);
133
134
        $this->expectException(CorruptedPathDetected::class);
135
        $xInputStorage = $this->xManager->get('files');
136
        $sInputContent = $xInputStorage->read("\0hello.txt");
0 ignored issues
show
Unused Code introduced by
The assignment to $sInputContent is dead and can be removed.
Loading history...
137
    }
138
139
    public function testStorageWriter()
140
    {
141
        $this->xManager->register('memory', fn() => new InMemoryFilesystemAdapter());
142
        $this->setConfigOptions([
143
            'adapter' => 'memory',
144
            'dir' => 'files',
145
            'options' => [],
146
        ], 'stores.memory');
147
148
        $xInputStorage = $this->xManager->adapter('local')->make($this->sInputDir);
149
        $sInputContent = $xInputStorage->read('hello.txt');
150
151
        $xOutputStorage = $this->xManager->get('memory');
152
        $xOutputStorage->write('hello.txt', $sInputContent);
153
        $sOutputContent = $xOutputStorage->read('hello.txt');
154
155
        $this->assertEquals($sOutputContent, $sInputContent);
156
    }
157
158
    public function testErrorUnknownAdapter()
159
    {
160
        $this->expectException(StorageException::class);
161
        $xUnknownStorage = $this->xManager->adapter('unknown')->make($this->sInputDir);
0 ignored issues
show
Unused Code introduced by
The assignment to $xUnknownStorage is dead and can be removed.
Loading history...
162
    }
163
164
    public function testErrorUnknownConfig()
165
    {
166
        $this->expectException(StorageException::class);
167
        $xUnknownStorage = $this->xManager->get('unknown');
0 ignored issues
show
Unused Code introduced by
The assignment to $xUnknownStorage is dead and can be removed.
Loading history...
168
    }
169
170
    public function testErrorIncorrectConfigAdapter()
171
    {
172
        $this->setConfigOptions([
173
            'adapter' => null,
174
            'dir' => 'files',
175
            'options' => [],
176
        ], 'stores.custom');
177
178
        $this->expectException(StorageException::class);
179
        $xErrorStorage = $this->xManager->get('custom');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
180
    }
181
182
    public function testErrorIncorrectConfigDir()
183
    {
184
        $this->setConfigOptions([
185
            'adapter' => 'memory',
186
            'dir' => null,
187
            'options' => [],
188
        ], 'stores.custom');
189
190
        $this->expectException(StorageException::class);
191
        $xErrorStorage = $this->xManager->get('custom');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
192
    }
193
194
    public function testErrorIncorrectConfigOptions()
195
    {
196
        $this->setConfigOptions([
197
            'adapter' => 'memory',
198
            'dir' => 'files',
199
            'options' => null,
200
        ], 'stores.custom');
201
202
        $this->expectException(StorageException::class);
203
        $xErrorStorage = $this->xManager->get('custom');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
204
    }
205
}
206