Passed
Push — master ( 8e0f36...cdb6de )
by Aimeos
06:20
created

StandardTest   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 137
dl 0
loc 305
rs 10
c 0
b 0
f 0
wmc 26

24 Methods

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