Passed
Push — master ( 9e220a...448f9b )
by Petr
07:43
created

ManageTest::testSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BasicTests;
4
5
use CommonTestClass;
6
use kalanis\kw_afterload\AfterloadException;
7
use kalanis\kw_afterload\Manage\Files;
8
9
10
class ManageTest extends CommonTestClass
11
{
12
    public function tearDown(): void
13
    {
14
        $files = new FilesMock();
15
        foreach ($files->getFiles() as $item) {
16
            $files->removeInput($item->getName());
17
        }
18
        parent::tearDown();
19
    }
20
21
    /**
22
     * @throws AfterloadException
23
     */
24
    public function testManageSimple(): void
25
    {
26
        $files = new FilesMock();
27
        $this->assertEmpty($files->getFiles(), 'Something left from previous run');
28
29
        $files->addInput('pre', 'foo-bar-baz');
30
        $this->assertEquals(1, count($files->getFiles()), 'No too many files');
31
32
        $files->enable('pre');
33
        $files->enable('pre'); // do nothing
34
        $this->assertEquals('foo-bar-baz', $files->getInput('pre'));
35
36
        $files->updateInput('pre', 'abc-def-ghi');
37
        $files->disable('pre');
38
        $files->disable('pre'); // do nothing
39
        $this->assertEquals('abc-def-ghi', $files->getInput('pre'));
40
41
        $files->removeInput('pre');
42
        $this->assertEmpty($files->getFiles(), 'No too many files left');
43
    }
44
45
    /**
46
     * @throws AfterloadException
47
     */
48
    public function testManageMulti(): void
49
    {
50
        $files1 = new FilesMock();
51
        $this->assertEmpty($files1->getFiles(), 'Something left from previous run');
52
53
        $files1->addInput('pre', 'foo-bar-baz');
54
        $files1->addInput('duo', 'foo-baz-baz');
55
        $this->assertEquals(2, count($files1->getFiles()), 'No too many files');
56
        $this->assertEquals('foo-baz-baz', $files1->getInput('duo'));
57
58
        $files1->enable('pre');
59
        $files1->enable('pre'); // do nothing
60
        $this->assertEquals('foo-bar-baz', $files1->getInput('pre'));
61
62
        $files2 = new FilesMock();
63
        $files2->updateInput('pre', 'abc-def-ghi');
64
        $files2->disable('pre');
65
        $files2->disable('pre'); // do nothing
66
        $this->assertEquals('abc-def-ghi', $files2->getInput('pre'));
67
68
        $files2->removeInput('duo');
69
        $files2->removeInput('pre');
70
        $this->assertEmpty($files2->getFiles(), 'No too many files left');
71
    }
72
73
    /**
74
     * @throws AfterloadException
75
     */
76
    public function testFailRead(): void
77
    {
78
        $files = new FilesMock();
79
        $this->assertEmpty($files->getFiles(), 'Something left from previous run');
80
        $files->addInput('erteddf', 'poiuztr');
81
        $in = $files->getFiles();
82
        $in = reset($in);
83
        chmod($in->getFullName(), 0222);
84
        $this->expectException(AfterloadException::class);
85
        $files->getInput('erteddf'); // fail
86
    }
87
88
    /**
89
     * @throws AfterloadException
90
     */
91
    public function testDoubleAdd(): void
92
    {
93
        $files = new FilesMock();
94
        $this->assertEmpty($files->getFiles(), 'Something left from previous run');
95
        $files->addInput('bnuuu', 'poiuztr');
96
        $in = $files->getFiles();
97
        $in = reset($in);
98
        chmod($in->getFullName(), 0444);
99
        $this->expectException(AfterloadException::class);
100
        $files->addInput('bnuuu', 'lkjhgfd'); // fail
101
    }
102
103
    /**
104
     * @throws AfterloadException
105
     */
106
    public function testDoubleUpdate(): void
107
    {
108
        $files = new FilesMock();
109
        $this->assertEmpty($files->getFiles(), 'Something left from previous run');
110
        $files->addInput('kldsg', 'poiuztr');
111
        $in = $files->getFiles();
112
        $in = reset($in);
113
        chmod($in->getFullName(), 0444);
114
        $this->expectException(AfterloadException::class);
115
        $files->updateInput('kldsg', 'lkjhgfd', false); // fail
116
    }
117
118
    /**
119
     * @throws AfterloadException
120
     */
121
    public function testSearch(): void
122
    {
123
        $files = new FilesMock();
124
        $this->assertEmpty($files->getFiles(), 'Something left from previous run');
125
        $this->expectException(AfterloadException::class);
126
        $files->getInput('unknown'); // fail
127
    }
128
}
129
130
131
class FilesMock extends Files
132
{
133
    protected function generatePaths(): void
134
    {
135
        parent::generatePaths();
136
        $this->enabledPath = strval(realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'test_conf_enabled'])));
137
        $this->disabledPath = strval(realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'test_conf_disabled'])));
138
    }
139
}
140