Passed
Push — master ( 3c6756...8b3b37 )
by Michael
14:24 queued 07:02
created

WideImageTest   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 132
c 2
b 0
f 0
dl 0
loc 268
rs 10
wmc 29

24 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoadFromHandle() 0 11 1
A testLoadMagicalFromHandle() 0 4 1
A testEmptyString() 0 3 1
A testMapperLoad() 0 8 1
A testMapperSaveToFile() 0 5 1
A testLoadFromStringWithCustomMapper() 0 4 1
A testLoadMagicalFromBinaryString() 0 4 1
A testLoadFromUpload() 0 15 1
A testInvalidImageFile() 0 3 1
A testLoadMagicalFromFile() 0 16 1
A testLoadFromFileFallbackToLoadFromString() 0 10 1
A testInvalidImageUploadField() 0 3 1
A teardown() 0 18 5
A testLoadFromFileWithInvalidExtension() 0 4 1
A testLoadBMPMagicalFromUpload() 0 14 1
A testLoadFromFileWithInvalidExtensionWithCustomMapper() 0 7 2
A testLoadFromMultipleUploads() 0 27 1
A testInvalidImageStringData() 0 3 1
A setup() 0 4 1
A testLoadFromStringEmpty() 0 3 1
A testMapperAsString() 0 6 1
A testLoadFromString() 0 8 1
A testInvalidImageHandle() 0 3 1
A testLoadFromFile() 0 15 1
1
<?php
2
	/**
3
    This file is part of WideImage.
4
		
5
    WideImage is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU Lesser General Public License as published by
7
    the Free Software Foundation; either version 2.1 of the License, or
8
    (at your option) any later version.
9
		
10
    WideImage is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU Lesser General Public License for more details.
14
		
15
    You should have received a copy of the GNU Lesser General Public License
16
    along with WideImage; if not, write to the Free Software
17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
    
19
    * @package Tests
20
  **/
21
22
namespace Test\WideImage;
23
24
include_once __DIR__ . '/Mapper/FOO.php';
25
include_once __DIR__ . '/Mapper/FOO2.php';
26
27
use WideImage\WideImage;
28
use WideImage\PaletteImage;
29
use WideImage\TrueColorImage;
30
use WideImage\Mapper\FOO;
31
use WideImage\Mapper\FOO2;
32
use Test\WideImage_TestCase;
33
34
/**
35
 * @package Tests
36
 */
37
class WideImageTest extends WideImage_TestCase
38
{
39
	protected $_FILES;
40
	
41
	public function setup()
42
	{
43
		$this->_FILES = $_FILES;
44
		$_FILES = array();
45
	}
46
	
47
	public function teardown()
48
	{
49
		$_FILES = $this->_FILES;
50
		
51
		if (PHP_OS == 'WINNT') {
52
			chdir(IMG_PATH . "temp");
53
			
54
			foreach (new \DirectoryIterator(IMG_PATH . "temp") as $file) {
55
				if (!$file->isDot()) {
56
					if ($file->isDir()) {
57
						exec("rd /S /Q {$file->getFilename()}\n");
58
					} else {
59
						unlink($file->getFilename());
60
					}
61
				}
62
			}
63
		} else {
64
			exec("rm -rf " . IMG_PATH . 'temp/*');
65
		}
66
	}
67
	
68
	public function testLoadFromFile()
69
	{
70
		$img = WideImage::load(IMG_PATH . '100x100-red-transparent.gif');
71
		$this->assertTrue($img instanceof PaletteImage);
72
		$this->assertValidImage($img);
73
		$this->assertFalse($img->isTrueColor());
74
		$this->assertEquals(100, $img->getWidth());
75
		$this->assertEquals(100, $img->getHeight());
76
		
77
		$img = WideImage::load(IMG_PATH . '100x100-rainbow.png');
78
		$this->assertTrue($img instanceof TrueColorImage);
79
		$this->assertValidImage($img);
80
		$this->assertTrue($img->isTrueColor());
81
		$this->assertEquals(100, $img->getWidth());
82
		$this->assertEquals(100, $img->getHeight());
83
	}
84
	
85
	public function testLoadFromString()
86
	{
87
		$img = WideImage::load(file_get_contents(IMG_PATH . '100x100-rainbow.png'));
88
		$this->assertTrue($img instanceof TrueColorImage);
89
		$this->assertValidImage($img);
90
		$this->assertTrue($img->isTrueColor());
91
		$this->assertEquals(100, $img->getWidth());
92
		$this->assertEquals(100, $img->getHeight());
93
	}
94
	
95
	public function testLoadFromHandle()
96
	{
97
		$handle = imagecreatefrompng(IMG_PATH . '100x100-rainbow.png');
98
		$img = WideImage::loadFromHandle($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type GdImage; however, parameter $handle of WideImage\WideImage::loadFromHandle() does only seem to accept resource, 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

98
		$img = WideImage::loadFromHandle(/** @scrutinizer ignore-type */ $handle);
Loading history...
99
		$this->assertValidImage($img);
100
		$this->assertTrue($img->isTrueColor());
101
		$this->assertSame($handle, $img->getHandle());
102
		$this->assertEquals(100, $img->getWidth());
103
		$this->assertEquals(100, $img->getHeight());
104
		unset($img);
105
		$this->assertFalse(WideImage::isValidImageHandle($handle));
106
	}
107
	
108
	public function testLoadFromUpload()
109
	{
110
		copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg');
111
		$_FILES = array(
112
			'testupl' => array(
113
				'name' => '100x100-rainbow.png',
114
				'type' => 'image/png',
115
				'size' => strlen(file_get_contents(IMG_PATH . '100x100-rainbow.png')),
116
				'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg',
117
				'error' => false,
118
			)
119
		);
120
		
121
		$img = WideImage::loadFromUpload('testupl');
122
		$this->assertValidImage($img);
123
	}
124
	
125
	public function testLoadFromMultipleUploads()
126
	{
127
		copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg1');
128
		copy(IMG_PATH . 'splat.tga', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg2');
129
		$_FILES = array(
130
			'testupl' => array(
131
				'name' => array('100x100-rainbow.png', 'splat.tga'),
132
				'type' => array('image/png', 'image/tga'),
133
				'size' => array(
134
						strlen(file_get_contents(IMG_PATH . '100x100-rainbow.png')), 
135
						strlen(file_get_contents(IMG_PATH . 'splat.tga'))
136
					),
137
				'tmp_name' => array(
138
						IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg1',
139
						IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg2'
140
					),
141
				'error' => array(false, false),
142
			)
143
		);
144
		
145
		$images = WideImage::loadFromUpload('testupl');
146
		$this->assertInternalType("array", $images);
147
		$this->assertValidImage($images[0]);
148
		$this->assertValidImage($images[1]);
149
		
150
		$img = WideImage::loadFromUpload('testupl', 1);
151
		$this->assertValidImage($img);
152
	}
153
	
154
	public function testLoadMagicalFromHandle()
155
	{
156
		$img = WideImage::load(imagecreatefrompng(IMG_PATH . '100x100-rainbow.png'));
157
		$this->assertValidImage($img);
158
	}
159
	
160
	
161
	public function testLoadMagicalFromBinaryString()
162
	{
163
		$img = WideImage::load(file_get_contents(IMG_PATH . '100x100-rainbow.png'));
164
		$this->assertValidImage($img);
165
	}
166
	
167
	public function testLoadMagicalFromFile()
168
	{
169
		$img = WideImage::load(IMG_PATH . '100x100-rainbow.png');
170
		$this->assertValidImage($img);
171
		copy(IMG_PATH . '100x100-rainbow.png', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg');
172
		$_FILES = array(
173
			'testupl' => array(
174
				'name' => 'fgnl.bmp',
175
				'type' => 'image/bmp',
176
				'size' => strlen(file_get_contents(IMG_PATH . 'fgnl.bmp')),
177
				'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg',
178
				'error' => false,
179
			)
180
		);
181
		$img = WideImage::load('testupl');
182
		$this->assertValidImage($img);
183
	}
184
	
185
	public function testLoadFromStringWithCustomMapper()
186
	{
187
		$img = WideImage::loadFromString(file_get_contents(IMG_PATH . 'splat.tga'));
188
		$this->assertValidImage($img);
189
	}
190
	
191
	public function testLoadFromFileWithInvalidExtension()
192
	{
193
		$img = WideImage::load(IMG_PATH . 'actually-a-png.jpg');
194
		$this->assertValidImage($img);
195
	}
196
	
197
	public function testLoadFromFileWithInvalidExtensionWithCustomMapper()
198
	{
199
		if (PHP_OS == 'WINNT')
200
			$this->markTestSkipped("For some reason, this test kills PHP my 32-bit Vista + PHP 5.3.1.");
201
		
202
		$img = WideImage::loadFromFile(IMG_PATH . 'fgnl-bmp.jpg');
203
		$this->assertValidImage($img);
204
	}
205
	
206
	/**
207
	 * @expectedException WideImage\Exception\InvalidImageSourceException
208
	 */
209
	public function testLoadFromStringEmpty()
210
	{
211
		WideImage::loadFromString('');
212
	}
213
	
214
	public function testLoadBMPMagicalFromUpload()
215
	{
216
		copy(IMG_PATH . 'fgnl.bmp', IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg');
217
		$_FILES = array(
218
			'testupl' => array(
219
				'name' => 'fgnl.bmp',
220
				'type' => 'image/bmp',
221
				'size' => strlen(file_get_contents(IMG_PATH . 'fgnl.bmp')),
222
				'tmp_name' => IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'upltmpimg',
223
				'error' => false,
224
			)
225
		);
226
		$img = WideImage::load('testupl');
227
		$this->assertValidImage($img);
228
	}
229
	
230
	public function testMapperLoad()
231
	{
232
		FOO::$handle = imagecreate(10, 10);
233
		$filename = IMG_PATH . 'image.foo';
234
		WideImage::registerCustomMapper(__NAMESPACE__ . '\\FOO', 'image/foo', 'foo');
235
		$img = WideImage::load($filename);
0 ignored issues
show
Unused Code introduced by
The assignment to $img is dead and can be removed.
Loading history...
236
		$this->assertEquals(FOO::$calls['load'], array($filename));
237
		imagedestroy(FOO::$handle);
238
	}
239
	
240
	public function testLoadFromFileFallbackToLoadFromString()
241
	{
242
		FOO::$handle = imagecreate(10, 10);
243
		$filename = IMG_PATH . 'image-actually-foo.foo2';
244
		WideImage::registerCustomMapper('FOO', 'image/foo', 'foo');
245
		WideImage::registerCustomMapper('FOO2', 'image/foo2', 'foo2');
246
		$img = WideImage::load($filename);
0 ignored issues
show
Unused Code introduced by
The assignment to $img is dead and can be removed.
Loading history...
247
		$this->assertEquals(FOO2::$calls['load'], array($filename));
248
		$this->assertEquals(FOO::$calls['loadFromString'], array(file_get_contents($filename)));
249
		imagedestroy(FOO::$handle);
250
	}
251
	
252
	public function testMapperSaveToFile()
253
	{
254
		$img = WideImage::load(IMG_PATH . 'fgnl.jpg');
255
		$img->saveToFile('test.foo', '123', 789);
256
		$this->assertEquals(FOO::$calls['save'], array($img->getHandle(), 'test.foo', '123', 789));
257
	}
258
	
259
	public function testMapperAsString()
260
	{
261
		$img = WideImage::load(IMG_PATH . 'fgnl.jpg');
262
		$str = $img->asString('foo', '123', 789);
263
		$this->assertEquals(FOO::$calls['save'], array($img->getHandle(), null, '123', 789));
264
		$this->assertEquals('out', $str);
265
	}
266
	
267
	/**
268
	 * @expectedException WideImage\Exception\InvalidImageSourceException
269
	 */
270
	public function testInvalidImageFile()
271
	{
272
		WideImage::loadFromFile(IMG_PATH . 'fakeimage.png');
273
	}
274
	
275
	/**
276
	 * @expectedException WideImage\Exception\InvalidImageSourceException
277
	 */
278
	public function testEmptyString()
279
	{
280
		WideImage::load('');
281
	}
282
	
283
	/**
284
	 * @expectedException WideImage\Exception\InvalidImageSourceException
285
	 */
286
	public function testInvalidImageStringData()
287
	{
288
		WideImage::loadFromString('asdf');
289
	}
290
	
291
	/**
292
	 * @expectedException WideImage\Exception\InvalidImageSourceException
293
	 */
294
	public function testInvalidImageHandle()
295
	{
296
		WideImage::loadFromHandle(0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type resource expected by parameter $handle of WideImage\WideImage::loadFromHandle(). ( Ignorable by Annotation )

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

296
		WideImage::loadFromHandle(/** @scrutinizer ignore-type */ 0);
Loading history...
297
	}
298
	
299
	/**
300
	 * @expectedException WideImage\Exception\InvalidImageSourceException
301
	 */
302
	public function testInvalidImageUploadField()
303
	{
304
		WideImage::loadFromUpload('xyz');
305
	}
306
}
307