Passed
Push — main ( f153c1...dae74f )
by Thierry
04:42
created

StorageTest::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
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $xUnknownStorage is dead and can be removed.
Loading history...
74
    }
75
76
    public function testErrorUnknownConfig()
77
    {
78
        $this->expectException(RequestException::class);
79
        $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...
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $xErrorStorage is dead and can be removed.
Loading history...
116
    }
117
}
118