Passed
Push — master ( b263ca...3dfec7 )
by Petr
02:26
created

XDirs::getLibThumb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ContentTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\Extended\Config;
8
use kalanis\kw_files\Extended\Processor;
9
use kalanis\kw_files\FilesException;
10
use kalanis\kw_files\Processing\Storage;
11
use kalanis\kw_images\Content\Dirs;
12
use kalanis\kw_images\Content\ImageSize;
13
use kalanis\kw_images\Graphics;
14
use kalanis\kw_images\Graphics\Format;
15
use kalanis\kw_images\ImagesException;
16
use kalanis\kw_images\Sources;
17
use kalanis\kw_mime\MimeType;
18
use kalanis\kw_storage\Storage\Key;
19
use kalanis\kw_storage\Storage\Target;
20
21
22
class DirsTest extends CommonTestClass
23
{
24
    /**
25
     * @throws FilesException
26
     * @throws ImagesException
27
     */
28
    public function testDescription(): void
29
    {
30
        $tgt = ['testtree'];
31
        $lib = $this->getLib();
32
33
        $this->assertEmpty($lib->getDescription($tgt));
34
        $this->assertTrue($lib->updateDescription($tgt, static::TEST_STRING));
35
        $this->assertEquals(static::TEST_STRING, $lib->getDescription($tgt));
36
        $this->assertTrue($lib->updateDescription($tgt));
37
        $this->assertEmpty($lib->getDescription($tgt));
38
    }
39
40
    /**
41
     * @throws FilesException
42
     * @throws ImagesException
43
     */
44
    public function testExtend1(): void
45
    {
46
        $tgt = ['testtree'];
47
        $lib = $this->getLib();
48
49
        $this->assertFalse($lib->canUse($tgt));
50
        $this->assertTrue($lib->create($tgt));
51
        $this->assertTrue($lib->canUse($tgt));
52
    }
53
54
    /**
55
     * @throws FilesException
56
     * @throws ImagesException
57
     */
58
    public function testExtend2(): void
59
    {
60
        $tgt = ['testtree'];
61
        $lib = $this->getLib();
62
63
        $this->assertFalse($lib->canUse($tgt));
64
        $this->assertTrue($lib->createSimple($tgt));
65
        $this->assertFalse($lib->canUse($tgt));
66
        $this->assertTrue($lib->createExtra($tgt));
67
        $this->assertTrue($lib->canUse($tgt));
68
    }
69
70
    /**
71
     * @throws FilesException
72
     * @throws ImagesException
73
     */
74
    public function testUpdatePass(): void
75
    {
76
        $src = ['testtree', '.tmb', 'testimage.png'];
77
        $tgt = ['testtree'];
78
        $lib = $this->getLib();
79
80
        $this->assertTrue($lib->getLibSizes()->getImage()->set($src, strval(@file_get_contents($this->targetPath() . DIRECTORY_SEPARATOR . 'testimage.png'))));
81
        $this->assertFalse($lib->getLibThumb()->isHere($tgt));
82
        $this->assertEmpty($lib->getThumb($tgt));
83
        $this->assertTrue($lib->updateThumb($tgt, 'testimage.png'));
84
        $this->assertTrue($lib->getLibThumb()->isHere($tgt));
85
        $this->assertNotEmpty($lib->getThumb($tgt));
86
        $this->assertTrue($lib->removeThumb($tgt));
87
        $this->assertFalse($lib->getLibThumb()->isHere($tgt));
88
        $this->assertEmpty($lib->getThumb($tgt));
89
    }
90
91
    /**
92
     * @throws FilesException
93
     * @throws ImagesException
94
     */
95
    public function testRemoveFail(): void
96
    {
97
        $src = ['testtree', '.tmb', 'testimage.png'];
98
        $tgt = ['testtree'];
99
        $lib = $this->getLibThumbFail();
100
101
        $this->assertTrue($lib->getLibSizes()->getImage()->set($src, strval(@file_get_contents($this->targetPath() . DIRECTORY_SEPARATOR . 'testimage.png'))));
102
        $this->assertFalse($lib->getLibThumb()->isHere($tgt));
103
        $this->assertTrue($lib->updateThumb($tgt, 'testimage.png'));
104
        $this->assertTrue($lib->getLibThumb()->isHere($tgt));
105
106
        $this->expectExceptionMessage('Cannot remove current thumb!');
107
        $this->expectException(FilesException::class);
108
        $lib->removeThumb($tgt);
109
    }
110
111
    /**
112
     * @param array<string, string|int> $params
113
     * @throws ImagesException
114
     * @return XDirs
115
     */
116
    protected function getLib(array $params = []): XDirs
117
    {
118
        $storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), new Target\Memory());
119
        $nodes = new Storage\ProcessNode($storage);
120
        $files = new Storage\ProcessFile($storage);
121
        $dirs = new Storage\ProcessDir($storage);
122
        $config = (new Config())->setData($params);
123
124
        return new XDirs(
125
            new ImageSize(
126
                new Graphics($this->getGraphicsProcessor(), new MimeType(true)),
127
                (new Graphics\ImageConfig())->setData($params),
128
                new Sources\Image($nodes, $files, $config)
129
            ),
130
            new Sources\Thumb($nodes, $files, $config),
131
            new Sources\DirDesc($nodes, $files, $config),
132
            new Sources\DirThumb($nodes, $files, $config),
133
            new Processor($dirs, $nodes, $config)
134
        );
135
    }
136
137
    /**
138
     * @param array<string, string|int> $params
139
     * @throws ImagesException
140
     * @return XDirs
141
     */
142
    protected function getLibThumbFail(array $params = []): XDirs
143
    {
144
        $storage = new \kalanis\kw_storage\Storage\Storage(new Key\DefaultKey(), new Target\Memory());
145
        $nodes = new Storage\ProcessNode($storage);
146
        $files = new Storage\ProcessFile($storage);
147
        $dirs = new Storage\ProcessDir($storage);
148
        $config = (new Config())->setData($params);
149
150
        return new XDirs(
151
            new ImageSize(
152
                new Graphics($this->getGraphicsProcessor(), new MimeType(true)),
153
                (new Graphics\ImageConfig())->setData($params),
154
                new Sources\Image($nodes, $files, $config)
155
            ),
156
            new Sources\Thumb($nodes, $files, $config),
157
            new Sources\DirDesc($nodes, $files, $config),
158
            new XSourceDirThumbFail($nodes, $files, $config),
159
            new Processor($dirs, $nodes, $config)
160
        );
161
    }
162
163
    /**
164
     * @throws ImagesException
165
     * @return Graphics\Processor
166
     */
167
    protected function getGraphicsProcessor(): Graphics\Processor
168
    {
169
        return new Graphics\Processor(new Format\Factory());
170
    }
171
}
172
173
174
class XDirs extends Dirs
175
{
176
    public function getLibSizes(): ImageSize
177
    {
178
        return $this->libSizes;
179
    }
180
181
    public function getLibThumb(): Sources\DirThumb
182
    {
183
        return $this->libDirThumb;
184
    }
185
186
    /**
187
     * @param string[] $path
188
     * @throws FilesException
189
     * @return bool
190
     */
191
    public function createSimple(array $path): bool
192
    {
193
        return $this->libExt->createDir($path, false);
194
    }
195
}
196
197
198
class XSourceDirThumbFail extends Sources\DirThumb
199
{
200
    public function delete(array $whichDir): bool
201
    {
202
        return false;
203
    }
204
}
205