|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jaxon\Storage\Tests\TestStorage; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Exception\RequestException; |
|
6
|
|
|
use Jaxon\Storage\StorageManager; |
|
7
|
|
|
use League\Flysystem\InMemory\InMemoryFilesystemAdapter; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
|
|
10
|
|
|
use function Jaxon\jaxon; |
|
11
|
|
|
use function Jaxon\Storage\_register; |
|
12
|
|
|
use function file_get_contents; |
|
13
|
|
|
|
|
14
|
|
|
class StorageTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var StorageManager |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $xManager; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $sInputDir; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp(): void |
|
27
|
|
|
{ |
|
28
|
|
|
_register(); |
|
29
|
|
|
|
|
30
|
|
|
$this->sInputDir = __DIR__ . '/../files'; |
|
31
|
|
|
$this->xManager = jaxon()->di()->g(StorageManager::class); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function tearDown(): void |
|
35
|
|
|
{ |
|
36
|
|
|
jaxon()->reset(); |
|
37
|
|
|
parent::tearDown(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws RequestException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testStorageReader() |
|
44
|
|
|
{ |
|
45
|
|
|
$xInputStorage = $this->xManager->make('local', $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 testStorageWriter() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->xManager->register('memory', fn() => new InMemoryFilesystemAdapter()); |
|
54
|
|
|
jaxon()->config()->setAppOptions([ |
|
55
|
|
|
'adapter' => 'memory', |
|
56
|
|
|
'dir' => 'files', |
|
57
|
|
|
'options' => [], |
|
58
|
|
|
], 'storage.memory'); |
|
59
|
|
|
|
|
60
|
|
|
$xInputStorage = $this->xManager->make('local', $this->sInputDir); |
|
61
|
|
|
$sInputContent = $xInputStorage->read('hello.txt'); |
|
62
|
|
|
|
|
63
|
|
|
$xOutputStorage = $this->xManager->get('memory'); |
|
64
|
|
|
$xOutputStorage->write('hello.txt', $sInputContent); |
|
65
|
|
|
$sOutputContent = $xOutputStorage->read('hello.txt'); |
|
66
|
|
|
|
|
67
|
|
|
$this->assertEquals($sOutputContent, $sInputContent); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testErrorUnknownAdapter() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(RequestException::class); |
|
73
|
|
|
$xUnknownStorage = $this->xManager->make('unknown', $this->sInputDir); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testErrorUnknownConfig() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->expectException(RequestException::class); |
|
79
|
|
|
$xUnknownStorage = $this->xManager->get('unknown'); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testErrorIncorrectConfigAdapter() |
|
83
|
|
|
{ |
|
84
|
|
|
jaxon()->config()->setAppOptions([ |
|
85
|
|
|
'adapter' => null, |
|
86
|
|
|
'dir' => 'files', |
|
87
|
|
|
'options' => [], |
|
88
|
|
|
], 'storage.custom'); |
|
89
|
|
|
|
|
90
|
|
|
$this->expectException(RequestException::class); |
|
91
|
|
|
$xErrorStorage = $this->xManager->get('custom'); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testErrorIncorrectConfigDir() |
|
95
|
|
|
{ |
|
96
|
|
|
jaxon()->config()->setAppOptions([ |
|
97
|
|
|
'adapter' => 'memory', |
|
98
|
|
|
'dir' => null, |
|
99
|
|
|
'options' => [], |
|
100
|
|
|
], 'storage.custom'); |
|
101
|
|
|
|
|
102
|
|
|
$this->expectException(RequestException::class); |
|
103
|
|
|
$xErrorStorage = $this->xManager->get('custom'); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testErrorIncorrectConfigOptions() |
|
107
|
|
|
{ |
|
108
|
|
|
jaxon()->config()->setAppOptions([ |
|
109
|
|
|
'adapter' => 'memory', |
|
110
|
|
|
'dir' => 'files', |
|
111
|
|
|
'options' => null, |
|
112
|
|
|
], 'storage.custom'); |
|
113
|
|
|
|
|
114
|
|
|
$this->expectException(RequestException::class); |
|
115
|
|
|
$xErrorStorage = $this->xManager->get('custom'); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|