Passed
Push — master ( 3ab830...8feef3 )
by Petr
08:05
created

FormatTest::testContentXbm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GraphicsTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_images\Graphics\Format;
8
use kalanis\kw_images\ImagesException;
9
use kalanis\kw_images\Translations;
10
11
12
class FormatTest extends CommonTestClass
13
{
14
    protected function tearDown(): void
15
    {
16
        $tgt0 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.png';
17
        $tgt1 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.bmp';
18
        $tgt2 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.gif';
19
        $tgt3 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.jpg';
20
        $tgt4 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.wbmp';
21
        $tgt5 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.webp';
22
        $tgt6 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.avif';
23
        $tgt7 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.xbm';
24
        if (is_file($tgt0)) {
25
            unlink($tgt0);
26
        }
27
        if (is_file($tgt1)) {
28
            unlink($tgt1);
29
        }
30
        if (is_file($tgt2)) {
31
            unlink($tgt2);
32
        }
33
        if (is_file($tgt3)) {
34
            unlink($tgt3);
35
        }
36
        if (is_file($tgt4)) {
37
            unlink($tgt4);
38
        }
39
        if (is_file($tgt5)) {
40
            unlink($tgt5);
41
        }
42
        if (is_file($tgt6)) {
43
            unlink($tgt6);
44
        }
45
        if (is_file($tgt7)) {
46
            unlink($tgt7);
47
        }
48
    }
49
50
    /**
51
     * @param string $type
52
     * @throws ImagesException
53
     * @dataProvider factoryProvider
54
     * @requires function imagecreatefrompng
55
     * @requires function imagepng
56
     * @requires function imagecreatefrombmp
57
     * @requires function imagebmp
58
     * @requires function imagecreatefromjpeg
59
     * @requires function imagejpeg
60
     */
61
    public function testFactoryPass(string $type): void
62
    {
63
        $lib = new Format\Factory();
64
        $this->assertInstanceOf(Format\AFormat::class, $lib->getByType($type, new Translations()));
65
    }
66
67
    public function factoryProvider(): array
68
    {
69
        return [
70
            ['png'],
71
            ['bmp'],
72
            ['jpeg'],
73
        ];
74
    }
75
76
    public function testFactoryTypeFail(): void
77
    {
78
        $lib = new Format\Factory();
79
        $this->expectExceptionMessage('Unknown type *txt*');
80
        $this->expectException(ImagesException::class);
81
        $lib->getByType('txt', new Translations());
82
    }
83
84
    public function testFactoryClassFail(): void
85
    {
86
        $lib = new XFactory();
87
        $this->expectExceptionMessage('Wrong instance of *stdClass*, must be instance of \kalanis\kw_images\Graphics\Format\AFormat');
88
        $this->expectException(ImagesException::class);
89
        $lib->getByType('xxx', new Translations());
90
    }
91
92
    /**
93
     * @throws ImagesException
94
     * @requires function imagecreatefrompng
95
     * @requires function imagepng
96
     */
97
    public function testContentPng(): void
98
    {
99
        $tgt0 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.png';
100
        $this->contentTesting(new Format\Png(), $tgt0);
101
    }
102
103
    /**
104
     * @throws ImagesException
105
     * Slow test, I know about it
106
     * @requires function imagecreatefrombmp
107
     * @requires function imagebmp
108
     */
109
    public function testContentBmp(): void
110
    {
111
        $tgt1 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.bmp';
112
        $this->contentTesting(new Format\Bmp(), $tgt1);
113
    }
114
115
    /**
116
     * @throws ImagesException
117
     * @requires function imagecreatefromgif
118
     * @requires function imagegif
119
     */
120
    public function testContentGif(): void
121
    {
122
        $tgt2 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.gif';
123
        $this->contentTesting(new Format\Gif(), $tgt2);
124
    }
125
126
    /**
127
     * @throws ImagesException
128
     * @requires function imagecreatefromjpeg
129
     * @requires function imagejpeg
130
     */
131
    public function testContentJpg(): void
132
    {
133
        $tgt3 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.jpg';
134
        $this->contentTesting(new Format\Jpeg(), $tgt3);
135
    }
136
137
    /**
138
     * @throws ImagesException
139
     * @requires function imagecreatefromwbmp
140
     * @requires function imagewbmp
141
     */
142
    public function testContentWbmp(): void
143
    {
144
        $tgt4 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.wbmp';
145
        $this->contentTesting(new Format\Wbmp(), $tgt4);
146
    }
147
148
    /**
149
     * @throws ImagesException
150
     * @requires function imagecreatefromwebp
151
     * @requires function imagewebp
152
     */
153
    public function testContentWebp(): void
154
    {
155
        $tgt5 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.webp';
156
        $this->contentTesting(new Format\Webp(), $tgt5);
157
    }
158
159
//    /**
160
//     * @throws ImagesException
161
//     * @requires function imagecreatefromavif
162
//     * @requires function imageavif
163
//     */
164
//    public function testContentAvif(): void
165
//    {
166
//        $tgt6 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.avif';
167
//        $this->contentTesting(new Format\Avif(), $tgt6);
168
//    }
169
170
    /**
171
     * @throws ImagesException
172
     * @requires function imagecreatefromxbm
173
     * @requires function imagexbm
174
     */
175
    public function testContentXbm(): void
176
    {
177
        $tgt7 = $this->targetPath() . DIRECTORY_SEPARATOR . 'testtree' . DIRECTORY_SEPARATOR . 'tstimg.xbm';
178
        $this->contentTesting(new Format\Xbm(), $tgt7);
179
    }
180
181
    /**
182
     * @param Format\AFormat $lib
183
     * @param string $target
184
     * @throws ImagesException
185
     */
186
    protected function contentTesting(Format\AFormat $lib, string $target): void
187
    {
188
        $src = $this->targetPath() . DIRECTORY_SEPARATOR . 'testimage.png';
189
        $libSrc = new Format\Png();
190
191
        $resSrc = $libSrc->load($src);
192
        $this->assertNotEmpty($resSrc);
193
194
        $lib->save($target, $resSrc);
195
        $this->assertTrue(file_exists($target));
196
197
        $res = $lib->load($target);
198
        $this->assertNotEmpty($res);
199
    }
200
}
201
202
203
class XFactory extends Format\Factory
204
{
205
    protected $types = [
206
        'bmp' => Format\Bmp::class,
207
        'xxx' => \stdClass::class,
208
    ];
209
}
210