Passed
Push — master ( af3dcf...3709c1 )
by Petr
02:16
created

StorageTest::testProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.9666
1
<?php
2
3
namespace MetaSourceTests;
4
5
6
use kalanis\kw_menu\Menu\Menu;
7
use kalanis\kw_menu\MetaSource;
8
use kalanis\kw_menu\MenuException;
9
use kalanis\kw_storage\Storage\Key;
10
use kalanis\kw_storage\Storage\Storage;
11
use kalanis\kw_storage\Storage\Target;
12
13
14
class StorageTest extends \CommonTestClass
15
{
16
    /**
17
     * @throws MenuException
18
     */
19
    public function testProcess(): void
20
    {
21
        $lib = $this->getLib();
22
        $lib->setSource(['none']);
23
        $this->assertFalse($lib->exists());
24
        $lib->setSource(['target.meta']);
25
        $this->assertTrue($lib->exists());
26
        $content = $lib->load();
27
        $this->assertNotEmpty($content);
28
        $lib->setSource(['copy.meta']);
29
        $this->assertTrue($lib->save($content));
30
    }
31
32
    /**
33
     * @throws MenuException
34
     */
35
    public function testCannotRead(): void
36
    {
37
        $lib = $this->getLib();
38
        chmod($this->getTargetPath() . 'unable.meta', 0222);
39
        $lib->setSource(['unable.meta']);
40
        $this->expectException(MenuException::class);
41
        $lib->load();
42
    }
43
44
    /**
45
     * @throws MenuException
46
     */
47
    public function testFailedMeta(): void
48
    {
49
        $lib = $this->getFailLib();
50
        $this->expectException(MenuException::class);
51
        $lib->exists();
52
    }
53
54
    /**
55
     * @throws MenuException
56
     */
57
    public function testFailedLoad(): void
58
    {
59
        $lib = $this->getFailLib();
60
        $this->expectException(MenuException::class);
61
        $lib->load();
62
    }
63
64
    /**
65
     * @throws MenuException
66
     */
67
    public function testFailedSave(): void
68
    {
69
        $lib = $this->getFailLib();
70
        $this->expectException(MenuException::class);
71
        $lib->save(new Menu());
72
    }
73
74
    /**
75
     * @throws MenuException
76
     */
77
    public function testCannotWrite(): void
78
    {
79
        $lib = $this->getLib();
80
        chmod($this->getTargetPath() . 'unable.meta', 0444);
81
        $lib->setSource(['unable.meta']);
82
        $this->assertFalse($lib->save(new Menu()));
83
    }
84
85
    protected function getLib(): MetaSource\Storage
86
    {
87
        Key\DirKey::setDir($this->getTargetPath());
88
        return new MetaSource\Storage(new Storage(new Key\DirKey(), new Target\Volume()), new MetaSource\FileParser(), ['target.meta']);
89
    }
90
91
    protected function getFailLib(): MetaSource\Storage
92
    {
93
        return new MetaSource\Storage(new \XFailStorage(new Key\DefaultKey(), new Target\Memory()), new MetaSource\FileParser(), ['target.meta']);
94
    }
95
}
96