Passed
Push — master ( 0b20e6...3a20e7 )
by Aimeos
05:22 queued 55s
created

StandardTest::setUpBeforeClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2023
7
 */
8
9
10
namespace Aimeos\MW\Media\Image;
11
12
13
class StandardTest extends \PHPUnit\Framework\TestCase
14
{
15
	public static function setUpBeforeClass() : void
16
	{
17
		$dir = dirname( dirname( dirname( __DIR__ ) ) ) . '/tmp';
18
19
		if( !is_dir( $dir ) ) {
20
			mkdir( $dir, 0755, true );
21
		}
22
	}
23
24
25
	public function testConstructGif()
26
	{
27
		$ds = DIRECTORY_SEPARATOR;
28
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
29
30
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
31
32
		$this->assertEquals( 'image/gif', $media->getMimetype() );
33
	}
34
35
36
	public function testConstructJpeg()
37
	{
38
		$ds = DIRECTORY_SEPARATOR;
39
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
40
41
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/jpeg', [] );
42
43
		$this->assertEquals( 'image/jpeg', $media->getMimetype() );
44
	}
45
46
47
	public function testConstructPng()
48
	{
49
		$ds = DIRECTORY_SEPARATOR;
50
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
51
52
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
53
54
		$this->assertEquals( 'image/png', $media->getMimetype() );
55
	}
56
57
58
	public function testConstructWebp()
59
	{
60
		if( \Aimeos\MW\Media\Image\Standard::supports( 'image/webp' ) ) {
61
			$this->markTestSkipped( 'WEBP image format not supported' );
62
		}
63
64
		$ds = DIRECTORY_SEPARATOR;
65
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.webp' );
66
67
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/webp', [] );
68
69
		$this->assertEquals( 'image/webp', $media->getMimetype() );
70
	}
71
72
73
	public function testConstructImageException()
74
	{
75
		$ds = DIRECTORY_SEPARATOR;
76
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'application.txt' );
77
78
		$this->expectException( \Aimeos\MW\Media\Exception::class );
79
		new \Aimeos\MW\Media\Image\Standard( $content, 'text/plain', [] );
80
	}
81
82
83
	public function testDestruct()
84
	{
85
		$ds = DIRECTORY_SEPARATOR;
86
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
87
88
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
89
90
		$this->assertInstanceOf( \Aimeos\MW\Media\Image\Iface::class, $media );
91
		unset( $media );
92
	}
93
94
95
	public function testGetHeight()
96
	{
97
		$ds = DIRECTORY_SEPARATOR;
98
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
99
100
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/jpeg', [] );
101
102
		$this->assertEquals( 10, $media->getHeight() );
103
	}
104
105
106
	public function testGetWidth()
107
	{
108
		$ds = DIRECTORY_SEPARATOR;
109
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
110
111
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/jpeg', [] );
112
113
		$this->assertEquals( 10, $media->getWidth() );
114
	}
115
116
117
	public function testSaveGif()
118
	{
119
		$ds = DIRECTORY_SEPARATOR;
120
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
121
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.gif';
122
123
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
124
		$media->save( $dest, 'image/gif' );
125
126
		$this->assertEquals( true, file_exists( $dest ) );
127
		unlink( $dest );
128
	}
129
130
131
	public function testSaveGifInvalidDest()
132
	{
133
		$ds = DIRECTORY_SEPARATOR;
134
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
135
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.gif';
136
137
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
138
139
		$this->expectException( \Aimeos\MW\Media\Exception::class );
140
		$media->save( $dest, 'image/gif' );
141
	}
142
143
144
	public function testSaveJpeg()
145
	{
146
		$ds = DIRECTORY_SEPARATOR;
147
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
148
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.jpg';
149
150
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
151
		$media->save( $dest, 'image/jpeg' );
152
153
		$this->assertEquals( true, file_exists( $dest ) );
154
		unlink( $dest );
155
	}
156
157
158
	public function testSaveJpegInvalidDest()
159
	{
160
		$ds = DIRECTORY_SEPARATOR;
161
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
162
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.jpg';
163
164
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
165
166
		$this->expectException( \Aimeos\MW\Media\Exception::class );
167
		$media->save( $dest, 'image/jpeg' );
168
	}
169
170
171
	public function testSavePng()
172
	{
173
		$ds = DIRECTORY_SEPARATOR;
174
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
175
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.png';
176
177
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
178
		$media->save( $dest, 'image/png' );
179
180
		$this->assertEquals( true, file_exists( $dest ) );
181
		unlink( $dest );
182
	}
183
184
185
	public function testSavePngInvalidDest()
186
	{
187
		$ds = DIRECTORY_SEPARATOR;
188
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
189
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.png';
190
191
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
192
193
		$this->expectException( \Aimeos\MW\Media\Exception::class );
194
		$media->save( $dest, 'image/png' );
195
	}
196
197
198
	public function testSaveWebp()
199
	{
200
		if( \Aimeos\MW\Media\Image\Standard::supports( 'image/webp' ) ) {
201
			$this->markTestSkipped( 'WEBP image format not supported' );
202
		}
203
204
		$ds = DIRECTORY_SEPARATOR;
205
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
206
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.webp';
207
208
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
209
		$media->save( $dest, 'image/webp' );
210
211
		$this->assertEquals( true, file_exists( $dest ) );
212
		unlink( $dest );
213
	}
214
215
216
	public function testSaveWebpInvalidDest()
217
	{
218
		$ds = DIRECTORY_SEPARATOR;
219
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
220
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.webp';
221
222
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
223
224
		$this->expectException( \Aimeos\MW\Media\Exception::class );
225
		$media->save( $dest, 'image/webp' );
226
	}
227
228
229
	public function testSaveWatermarkNotfound()
230
	{
231
		$ds = DIRECTORY_SEPARATOR;
232
		$basedir = dirname( __DIR__ );
233
234
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
235
		$options = ['image' => ['watermark' => $basedir . $ds . 'notexisting' . $ds . 'image.png']];
236
237
		$this->expectException( \Aimeos\MW\Media\Exception::class );
238
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', $options );
0 ignored issues
show
Unused Code introduced by
The assignment to $media is dead and can be removed.
Loading history...
239
	}
240
241
242
	public function testSaveWatermarkInvalid()
243
	{
244
		$ds = DIRECTORY_SEPARATOR;
245
		$basedir = dirname( __DIR__ );
246
247
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
248
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'application.txt']];
249
250
		$this->expectException( \Aimeos\MW\Media\Exception::class );
251
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', $options );
0 ignored issues
show
Unused Code introduced by
The assignment to $media is dead and can be removed.
Loading history...
252
	}
253
254
255
	public function testSaveWatermark()
256
	{
257
		$ds = DIRECTORY_SEPARATOR;
258
		$basedir = dirname( __DIR__ );
259
260
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
261
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'image.png']];
262
		$dest = dirname( dirname( $basedir ) ) . $ds . 'tmp' . $ds . 'media.jpg';
263
264
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', $options );
265
		$media->save( $dest, 'image/jpeg' );
266
267
		$this->assertEquals( true, file_exists( $dest ) );
268
		unlink( $dest );
269
	}
270
271
272
	public function testScaleFit()
273
	{
274
		$ds = DIRECTORY_SEPARATOR;
275
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
276
277
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
278
		$info = getimagesizefromstring( $media->scale( 100, 100, 1 )->save( null, 'image/png' ) );
279
280
		$this->assertEquals( 100, $info[0] );
281
		$this->assertEquals( 100, $info[1] );
282
	}
283
284
285
	public function testScaleDown()
286
	{
287
		$ds = DIRECTORY_SEPARATOR;
288
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
289
290
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
291
		$info = getimagesizefromstring( $media->scale( 5, 100 )->save( null, 'image/png' ) );
292
293
		$this->assertEquals( 5, $info[0] );
294
		$this->assertEquals( 5, $info[1] );
295
	}
296
297
298
	public function testScaleInside()
299
	{
300
		$ds = DIRECTORY_SEPARATOR;
301
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
302
303
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
304
		$info = getimagesizefromstring( $media->scale( 100, 100 )->save( null, 'image/png' ) );
305
306
		$this->assertEquals( 10, $info[0] );
307
		$this->assertEquals( 10, $info[1] );
308
	}
309
310
311
	public function testScaleFitCrop()
312
	{
313
		$ds = DIRECTORY_SEPARATOR;
314
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
315
316
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
317
		$info = getimagesizefromstring( $media->scale( 100, 50, 2 )->save( null, 'image/png' ) );
318
319
		$this->assertEquals( 100, $info[0] );
320
		$this->assertEquals( 50, $info[1] );
321
	}
322
323
324
	public function testSupports()
325
	{
326
		$this->assertGreaterThan( 0, count( \Aimeos\MW\Media\Image\Standard::supports() ) );
327
		$this->assertEquals( 1, count( \Aimeos\MW\Media\Image\Standard::supports( 'image/jpeg' ) ) );
328
329
		$result = \Aimeos\MW\Media\Image\Standard::supports( ['image/gif', 'image/png', 'image/jpeg'] );
330
		$this->assertEquals( ['image/gif', 'image/png', 'image/jpeg'], $result );
331
	}
332
}
333