Passed
Push — master ( bfbe95...c29b26 )
by Aimeos
06:06
created

ImagickTest::testScaleInside()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2021
6
 */
7
8
9
namespace Aimeos\MW\Media\Image;
10
11
12
class ImagickTest extends \PHPUnit\Framework\TestCase
13
{
14
	protected function setUp() : void
15
	{
16
		if( !class_exists( \Imagick::class ) ) {
17
			$this->markTestSkipped( 'Imagick not available' );
18
		}
19
	}
20
21
22
	public function testConstructGif()
23
	{
24
		$ds = DIRECTORY_SEPARATOR;
25
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
26
27
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] );
28
29
		$this->assertEquals( 'image/gif', $media->getMimetype() );
30
	}
31
32
33
	public function testConstructJpeg()
34
	{
35
		$ds = DIRECTORY_SEPARATOR;
36
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
37
38
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] );
39
40
		$this->assertEquals( 'image/jpeg', $media->getMimetype() );
41
	}
42
43
44
	public function testConstructPng()
45
	{
46
		$ds = DIRECTORY_SEPARATOR;
47
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
48
49
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
50
51
		$this->assertEquals( 'image/png', $media->getMimetype() );
52
	}
53
54
55
	public function testConstructImageException()
56
	{
57
		$ds = DIRECTORY_SEPARATOR;
58
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'application.txt' );
59
60
		$this->expectException( \Aimeos\MW\Media\Exception::class );
61
		new \Aimeos\MW\Media\Image\Imagick( $content, 'text/plain', [] );
62
	}
63
64
65
	public function testGetHeight()
66
	{
67
		$ds = DIRECTORY_SEPARATOR;
68
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
69
70
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] );
71
72
		$this->assertEquals( 10, $media->getHeight() );
73
	}
74
75
76
	public function testGetWidth()
77
	{
78
		$ds = DIRECTORY_SEPARATOR;
79
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
80
81
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', [] );
82
83
		$this->assertEquals( 10, $media->getWidth() );
84
	}
85
86
87
	public function testSaveGif()
88
	{
89
		$ds = DIRECTORY_SEPARATOR;
90
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
91
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.gif';
92
93
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
94
		$media->save( $dest, 'image/gif' );
95
96
		$this->assertEquals( true, file_exists( $dest ) );
97
		unlink( $dest );
98
	}
99
100
101
	public function testSaveGifInvalidDest()
102
	{
103
		$ds = DIRECTORY_SEPARATOR;
104
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
105
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.gif';
106
107
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
108
109
		$this->expectException( \Aimeos\MW\Media\Exception::class );
110
		$media->save( $dest, 'image/gif' );
111
	}
112
113
114
	public function testSaveJpeg()
115
	{
116
		$ds = DIRECTORY_SEPARATOR;
117
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
118
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.jpg';
119
120
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] );
121
		$media->save( $dest, 'image/jpeg' );
122
123
		$this->assertEquals( true, file_exists( $dest ) );
124
		unlink( $dest );
125
	}
126
127
128
	public function testSaveJpegInvalidDest()
129
	{
130
		$ds = DIRECTORY_SEPARATOR;
131
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
132
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.jpg';
133
134
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
135
136
		$this->expectException( \Aimeos\MW\Media\Exception::class );
137
		$media->save( $dest, 'image/jpeg' );
138
	}
139
140
141
	public function testSavePng()
142
	{
143
		$ds = DIRECTORY_SEPARATOR;
144
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
145
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.png';
146
147
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] );
148
		$media->save( $dest, 'image/png' );
149
150
		$this->assertEquals( true, file_exists( $dest ) );
151
		unlink( $dest );
152
	}
153
154
155
	public function testSavePngInvalidDest()
156
	{
157
		$ds = DIRECTORY_SEPARATOR;
158
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
159
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.png';
160
161
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', [] );
162
163
		$this->expectException( \Aimeos\MW\Media\Exception::class );
164
		$media->save( $dest, 'image/png' );
165
	}
166
167
168
	public function testSaveWatermarkNotfound()
169
	{
170
		$ds = DIRECTORY_SEPARATOR;
171
		$basedir = dirname( __DIR__ );
172
173
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
174
		$options = ['image' => ['watermark' => $basedir . $ds . 'notexisting' . $ds . 'image.png']];
175
176
		$this->expectException( \Aimeos\MW\Media\Exception::class );
177
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options );
0 ignored issues
show
Unused Code introduced by
The assignment to $media is dead and can be removed.
Loading history...
178
	}
179
180
181
	public function testSaveWatermarkInvalid()
182
	{
183
		$ds = DIRECTORY_SEPARATOR;
184
		$basedir = dirname( __DIR__ );
185
186
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
187
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'application.txt']];
188
189
		$this->expectException( \Aimeos\MW\Media\Exception::class );
190
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options );
0 ignored issues
show
Unused Code introduced by
The assignment to $media is dead and can be removed.
Loading history...
191
	}
192
193
194
	public function testSaveWatermark()
195
	{
196
		$ds = DIRECTORY_SEPARATOR;
197
		$basedir = dirname( __DIR__ );
198
199
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
200
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'image.png']];
201
		$dest = dirname( dirname( $basedir ) ) . $ds . 'tmp' . $ds . 'media.jpg';
202
203
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', $options );
204
		$media->save( $dest, 'image/jpeg' );
205
206
		$this->assertEquals( true, file_exists( $dest ) );
207
		unlink( $dest );
208
	}
209
210
211
	public function testScaleFit()
212
	{
213
		$ds = DIRECTORY_SEPARATOR;
214
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
215
216
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
217
		$info = getimagesizefromstring( $media->scale( 100, 100, 1 )->save( null, 'image/png' ) );
0 ignored issues
show
Bug introduced by
It seems like $media->scale(100, 100, ...save(null, 'image/png') can also be of type null; however, parameter $string of getimagesizefromstring() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

217
		$info = getimagesizefromstring( /** @scrutinizer ignore-type */ $media->scale( 100, 100, 1 )->save( null, 'image/png' ) );
Loading history...
218
219
		$this->assertEquals( 100, $info[0] );
220
		$this->assertEquals( 100, $info[1] );
221
	}
222
223
224
	public function testScaleDown()
225
	{
226
		$ds = DIRECTORY_SEPARATOR;
227
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
228
229
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
230
		$info = getimagesizefromstring( $media->scale( 5, 100 )->save( null, 'image/png' ) );
0 ignored issues
show
Bug introduced by
It seems like $media->scale(5, 100)->save(null, 'image/png') can also be of type null; however, parameter $string of getimagesizefromstring() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
		$info = getimagesizefromstring( /** @scrutinizer ignore-type */ $media->scale( 5, 100 )->save( null, 'image/png' ) );
Loading history...
231
232
		$this->assertEquals( 5, $info[0] );
233
		$this->assertEquals( 5, $info[1] );
234
	}
235
236
237
	public function testScaleInside()
238
	{
239
		$ds = DIRECTORY_SEPARATOR;
240
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
241
242
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
243
		$info = getimagesizefromstring( $media->scale( 100, 100 )->save( null, 'image/png' ) );
0 ignored issues
show
Bug introduced by
It seems like $media->scale(100, 100)->save(null, 'image/png') can also be of type null; however, parameter $string of getimagesizefromstring() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
		$info = getimagesizefromstring( /** @scrutinizer ignore-type */ $media->scale( 100, 100 )->save( null, 'image/png' ) );
Loading history...
244
245
		$this->assertEquals( 10, $info[0] );
246
		$this->assertEquals( 10, $info[1] );
247
	}
248
249
250
	public function testScaleFitCrop()
251
	{
252
		$ds = DIRECTORY_SEPARATOR;
253
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
254
255
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', [] );
256
		$info = getimagesizefromstring( $media->scale( 100, 50, 2 )->save( null, 'image/png' ) );
0 ignored issues
show
Bug introduced by
It seems like $media->scale(100, 50, 2...save(null, 'image/png') can also be of type null; however, parameter $string of getimagesizefromstring() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

256
		$info = getimagesizefromstring( /** @scrutinizer ignore-type */ $media->scale( 100, 50, 2 )->save( null, 'image/png' ) );
Loading history...
257
258
		$this->assertEquals( 100, $info[0] );
259
		$this->assertEquals( 50, $info[1] );
260
	}
261
}
262