Passed
Push — main ( 95b356...ef02f7 )
by Thierry
03:58
created

StorageWithoutJaxonTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
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\Exception;
8
use Jaxon\Storage\StorageManager;
9
use League\Flysystem\CorruptedPathDetected;
10
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
11
use PHPUnit\Framework\TestCase;
12
13
use function dirname;
14
use function file_get_contents;
15
16
class StorageWithoutJaxonTest extends TestCase
17
{
18
    /**
19
     * @var StorageManager
20
     */
21
    protected $xManager;
22
23
    /**
24
     * @var ConfigSetter
25
     */
26
    protected $xConfigSetter;
27
28
    /**
29
     * @var string
30
     */
31
    protected $sInputDir;
32
33
    public function setUp(): void
34
    {
35
        $this->sInputDir = dirname(__DIR__) . '/files';
36
        $this->xManager = new StorageManager();
37
        $this->xConfigSetter = new ConfigSetter();
38
    }
39
40
    public function tearDown(): void
41
    {
42
        jaxon()->reset();
43
        parent::tearDown();
44
    }
45
46
    private function setConfigOptions(array $aOptions, string $sPrefix = '')
47
    {
48
        $this->xManager->setConfigGetter(fn(): Config =>
49
            $this->xConfigSetter->newConfig($aOptions, $sPrefix));
50
    }
51
52
    /**
53
     * @throws Exception
54
     */
55
    public function testStorageReader()
56
    {
57
        $xInputStorage = $this->xManager->adapter('local')->make($this->sInputDir);
58
        $sInputContent = $xInputStorage->read('hello.txt');
59
60
        $this->assertEquals(file_get_contents("{$this->sInputDir}/hello.txt"), $sInputContent);
61
    }
62
63
    public function testAdapterAndDirOptions()
64
    {
65
        $this->setConfigOptions([
66
            'adapters' => [
67
                'files' => [
68
                    'alias' => 'local',
69
                    'options' => [
70
                        'lazyRootCreation' => false, // Create dirs if they don't exist.
71
                    ],
72
                ],
73
            ],
74
            'stores' => [
75
                'files' => [
76
                    'adapter' => 'files',
77
                    'dir' => $this->sInputDir,
78
                    'options' => [
79
                        'config' => [
80
                            'public_url' => '/static/files',
81
                        ],
82
                    ],
83
                ],
84
            ],
85
        ]);
86
87
        $xInputStorage = $this->xManager->get('files');
88
        $sInputContent = $xInputStorage->read('hello.txt');
89
90
        $this->assertEquals(file_get_contents("{$this->sInputDir}/hello.txt"), $sInputContent);
91
        $this->assertEquals('/static/files/hello.txt', $xInputStorage->publicUrl('hello.txt'));
92
    }
93
94
    public function testWriteError()
95
    {
96
        $this->setConfigOptions([
97
            'adapters' => [
98
                'files' => [
99
                    'alias' => 'local',
100
                    'options' => [
101
                        'lazyRootCreation' => true, // Don't create dirs if they don't exist.
102
                    ],
103
                ],
104
            ],
105
            'stores' => [
106
                'files' => [
107
                    'adapter' => 'files',
108
                    'dir' => dirname(__DIR__ . '/files'),
109
                    'options' => [
110
                        'config' => [
111
                            'public_url' => '/static/files',
112
                        ],
113
                    ],
114
                ],
115
            ],
116
        ]);
117
118
        $this->expectException(CorruptedPathDetected::class);
119
        $xInputStorage = $this->xManager->get('files');
120
        $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...
121
    }
122
123
    public function testStorageWriter()
124
    {
125
        $this->xManager->register('memory', fn() => new InMemoryFilesystemAdapter());
126
        $this->setConfigOptions([
127
            'adapter' => 'memory',
128
            'dir' => 'files',
129
            'options' => [],
130
        ], 'stores.memory');
131
132
        $xInputStorage = $this->xManager->adapter('local')->make($this->sInputDir);
133
        $sInputContent = $xInputStorage->read('hello.txt');
134
135
        $xOutputStorage = $this->xManager->get('memory');
136
        $xOutputStorage->write('hello.txt', $sInputContent);
137
        $sOutputContent = $xOutputStorage->read('hello.txt');
138
139
        $this->assertEquals($sOutputContent, $sInputContent);
140
    }
141
142
    public function testErrorUnknownAdapter()
143
    {
144
        $this->expectException(Exception::class);
145
        $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...
146
    }
147
148
    public function testErrorUnknownConfig()
149
    {
150
        $this->expectException(Exception::class);
151
        $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...
152
    }
153
154
    public function testErrorIncorrectConfigAdapter()
155
    {
156
        $this->setConfigOptions([
157
            'adapter' => null,
158
            'dir' => 'files',
159
            'options' => [],
160
        ], 'stores.custom');
161
162
        $this->expectException(Exception::class);
163
        $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...
164
    }
165
166
    public function testErrorIncorrectConfigDir()
167
    {
168
        $this->setConfigOptions([
169
            'adapter' => 'memory',
170
            'dir' => null,
171
            'options' => [],
172
        ], 'stores.custom');
173
174
        $this->expectException(Exception::class);
175
        $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...
176
    }
177
178
    public function testErrorIncorrectConfigOptions()
179
    {
180
        $this->setConfigOptions([
181
            'adapter' => 'memory',
182
            'dir' => 'files',
183
            'options' => null,
184
        ], 'stores.custom');
185
186
        $this->expectException(Exception::class);
187
        $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...
188
    }
189
}
190