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