Passed
Push — master ( 3c6756...8b3b37 )
by Michael
20:43 queued 12:59
created

PNGTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 35
c 2
b 0
f 0
dl 0
loc 66
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testLoad() 0 7 1
A testSaveToString() 0 13 1
A testSaveCompression() 0 11 1
A teardown() 0 6 2
A testSaveToFile() 0 11 1
A setup() 0 3 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
use WideImage\WideImage;
25
use WideImage\MapperFactory;
26
use Test\WideImage_TestCase;
27
28
/**
29
 * @package Tests
30
 */
31
class PNGTest extends WideImage_TestCase
32
{
33
	protected $mapper;
34
	
35
	public function setup()
36
	{
37
		$this->mapper = MapperFactory::selectMapper(null, 'png');
38
	}
39
	
40
	public function teardown()
41
	{
42
		$this->mapper = null;
43
		
44
		if (file_exists(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.png')) {
45
			unlink(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.png');
46
		}
47
	}
48
	
49
	public function testLoad()
50
	{
51
		$handle = $this->mapper->load(IMG_PATH . '100x100-color-hole.png');
52
		$this->assertTrue(WideImage::isValidImageHandle($handle));
53
		$this->assertEquals(100, imagesx($handle));
54
		$this->assertEquals(100, imagesy($handle));
55
		imagedestroy($handle);
56
	}
57
	
58
	public function testSaveToString()
59
	{
60
		$handle = imagecreatefrompng(IMG_PATH . '100x100-color-hole.png');
61
		ob_start();
62
		$this->mapper->save($handle);
63
		$string = ob_get_clean();
64
		$this->assertTrue(strlen($string) > 0);
65
		imagedestroy($handle);
66
		
67
		// string contains valid image data
68
		$handle = imagecreatefromstring($string);
69
		$this->assertTrue(WideImage::isValidImageHandle($handle));
70
		imagedestroy($handle);
71
	}
72
	
73
	public function testSaveToFile()
74
	{
75
		$handle = imagecreatefrompng(IMG_PATH . '100x100-color-hole.png');
76
		$this->mapper->save($handle, IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.png');
77
		$this->assertTrue(filesize(IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test.png') > 0);
78
		imagedestroy($handle);
79
		
80
		// file is a valid image
81
		$handle = imagecreatefrompng(IMG_PATH . 'temp/test.png');
82
		$this->assertTrue(WideImage::isValidImageHandle($handle));
83
		imagedestroy($handle);
84
	}
85
	
86
	public function testSaveCompression()
87
	{
88
		$handle = $this->mapper->load(IMG_PATH . '100x100-rainbow.png');
89
		$file1 = IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test-comp-0.png';
90
		$file2 = IMG_PATH . 'temp' . DIRECTORY_SEPARATOR . 'test-comp-9.png';
91
		$this->mapper->save($handle, $file1, 0);
92
		$this->mapper->save($handle, $file2, 9);
93
		$this->assertTrue(filesize($file1) > filesize($file2));
94
		
95
		unlink($file1);
96
		unlink($file2);
97
	}
98
}
99