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

StandardTest::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 Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
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 testConstructImageException()
49
	{
50
		$ds = DIRECTORY_SEPARATOR;
51
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'application.txt' );
52
53
		$this->expectException( \Aimeos\MW\Media\Exception::class );
54
		new \Aimeos\MW\Media\Image\Standard( $content, 'text/plain', [] );
55
	}
56
57
58
	public function testDestruct()
59
	{
60
		$ds = DIRECTORY_SEPARATOR;
61
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
62
63
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
64
65
		$this->assertInstanceOf( \Aimeos\MW\Media\Image\Iface::class, $media );
66
		unset( $media );
67
	}
68
69
70
	public function testGetHeight()
71
	{
72
		$ds = DIRECTORY_SEPARATOR;
73
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
74
75
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/jpeg', [] );
76
77
		$this->assertEquals( 10, $media->getHeight() );
78
	}
79
80
81
	public function testGetWidth()
82
	{
83
		$ds = DIRECTORY_SEPARATOR;
84
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
85
86
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/jpeg', [] );
87
88
		$this->assertEquals( 10, $media->getWidth() );
89
	}
90
91
92
	public function testSaveGif()
93
	{
94
		$ds = DIRECTORY_SEPARATOR;
95
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
96
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.gif';
97
98
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
99
		$media->save( $dest, 'image/gif' );
100
101
		$this->assertEquals( true, file_exists( $dest ) );
102
		unlink( $dest );
103
	}
104
105
106
	public function testSaveGifInvalidDest()
107
	{
108
		$ds = DIRECTORY_SEPARATOR;
109
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
110
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.gif';
111
112
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
113
114
		$this->expectException( \Aimeos\MW\Media\Exception::class );
115
		$media->save( $dest, 'image/gif' );
116
	}
117
118
119
	public function testSaveJpeg()
120
	{
121
		$ds = DIRECTORY_SEPARATOR;
122
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
123
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.jpg';
124
125
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
126
		$media->save( $dest, 'image/jpeg' );
127
128
		$this->assertEquals( true, file_exists( $dest ) );
129
		unlink( $dest );
130
	}
131
132
133
	public function testSaveJpegInvalidDest()
134
	{
135
		$ds = DIRECTORY_SEPARATOR;
136
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
137
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.jpg';
138
139
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
140
141
		$this->expectException( \Aimeos\MW\Media\Exception::class );
142
		$media->save( $dest, 'image/jpeg' );
143
	}
144
145
146
	public function testSavePng()
147
	{
148
		$ds = DIRECTORY_SEPARATOR;
149
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
150
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.png';
151
152
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
153
		$media->save( $dest, 'image/png' );
154
155
		$this->assertEquals( true, file_exists( $dest ) );
156
		unlink( $dest );
157
	}
158
159
160
	public function testSavePngInvalidDest()
161
	{
162
		$ds = DIRECTORY_SEPARATOR;
163
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
164
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.png';
165
166
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/gif', [] );
167
168
		$this->expectException( \Aimeos\MW\Media\Exception::class );
169
		$media->save( $dest, 'image/png' );
170
	}
171
172
173
	public function testSaveWatermarkNotfound()
174
	{
175
		$ds = DIRECTORY_SEPARATOR;
176
		$basedir = dirname( __DIR__ );
177
178
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
179
		$options = ['image' => ['watermark' => $basedir . $ds . 'notexisting' . $ds . 'image.png']];
180
181
		$this->expectException( \Aimeos\MW\Media\Exception::class );
182
		$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...
183
	}
184
185
186
	public function testSaveWatermarkInvalid()
187
	{
188
		$ds = DIRECTORY_SEPARATOR;
189
		$basedir = dirname( __DIR__ );
190
191
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
192
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'application.txt']];
193
194
		$this->expectException( \Aimeos\MW\Media\Exception::class );
195
		$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...
196
	}
197
198
199
	public function testSaveWatermark()
200
	{
201
		$ds = DIRECTORY_SEPARATOR;
202
		$basedir = dirname( __DIR__ );
203
204
		$content = file_get_contents( $basedir . $ds . '_testfiles' . $ds . 'image.jpg' );
205
		$options = ['image' => ['watermark' => $basedir . $ds . '_testfiles' . $ds . 'image.png']];
206
		$dest = dirname( dirname( $basedir ) ) . $ds . 'tmp' . $ds . 'media.jpg';
207
208
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', $options );
209
		$media->save( $dest, 'image/jpeg' );
210
211
		$this->assertEquals( true, file_exists( $dest ) );
212
		unlink( $dest );
213
	}
214
215
216
	public function testScaleFit()
217
	{
218
		$ds = DIRECTORY_SEPARATOR;
219
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
220
221
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
222
		$info = getimagesizefromstring( $media->scale( 100, 100, 1 )->save( null, 'image/png' ) );
223
224
		$this->assertEquals( 100, $info[0] );
225
		$this->assertEquals( 100, $info[1] );
226
	}
227
228
229
	public function testScaleDown()
230
	{
231
		$ds = DIRECTORY_SEPARATOR;
232
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
233
234
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
235
		$info = getimagesizefromstring( $media->scale( 5, 100 )->save( null, 'image/png' ) );
236
237
		$this->assertEquals( 5, $info[0] );
238
		$this->assertEquals( 5, $info[1] );
239
	}
240
241
242
	public function testScaleInside()
243
	{
244
		$ds = DIRECTORY_SEPARATOR;
245
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
246
247
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
248
		$info = getimagesizefromstring( $media->scale( 100, 100 )->save( null, 'image/png' ) );
249
250
		$this->assertEquals( 10, $info[0] );
251
		$this->assertEquals( 10, $info[1] );
252
	}
253
	
254
	
255
	public function testScaleFitCrop()
256
	{
257
		$ds = DIRECTORY_SEPARATOR;
258
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
259
260
		$media = new \Aimeos\MW\Media\Image\Standard( $content, 'image/png', [] );
261
		$info = getimagesizefromstring( $media->scale( 100, 50, 2 )->save( null, 'image/png' ) );
262
263
		$this->assertEquals( 100, $info[0] );
264
		$this->assertEquals( 50, $info[1] );
265
	}
266
}
267