Completed
Push — master ( e595c8...dedff5 )
by Aimeos
08:25
created

ImagickTest::testSavePng()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017
6
 */
7
8
9
namespace Aimeos\MW\Media\Image;
10
11
12
class ImagickTest extends \PHPUnit_Framework_TestCase
13
{
14
	public function testConstructGif()
15
	{
16
		$ds = DIRECTORY_SEPARATOR;
17
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
18
19
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', array() );
20
21
		$this->assertEquals( 'image/gif', $media->getMimetype() );
22
	}
23
24
25
	public function testConstructJpeg()
26
	{
27
		$ds = DIRECTORY_SEPARATOR;
28
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.jpg' );
29
30
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/jpeg', array() );
31
32
		$this->assertEquals( 'image/jpeg', $media->getMimetype() );
33
	}
34
35
36
	public function testConstructPng()
37
	{
38
		$ds = DIRECTORY_SEPARATOR;
39
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
40
41
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
42
43
		$this->assertEquals( 'image/png', $media->getMimetype() );
44
	}
45
46
47
	public function testConstructImageException()
48
	{
49
		$ds = DIRECTORY_SEPARATOR;
50
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'application.txt' );
51
52
		$this->setExpectedException( '\\Aimeos\\MW\\Media\\Exception' );
53
		new \Aimeos\MW\Media\Image\Imagick( $content, 'text/plain', array() );
54
	}
55
56
57
	public function testSaveGif()
58
	{
59
		$ds = DIRECTORY_SEPARATOR;
60
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
61
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.gif';
62
63
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
64
		$media->save( $dest, 'image/gif' );
65
66
		$this->assertEquals( true, file_exists( $dest ) );
67
		unlink( $dest );
68
	}
69
70
71
	public function testSaveGifInvalidDest()
72
	{
73
		$ds = DIRECTORY_SEPARATOR;
74
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
75
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.gif';
76
77
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
78
79
		$this->setExpectedException( '\\Aimeos\\MW\\Media\\Exception' );
80
		$media->save( $dest, 'image/gif' );
81
	}
82
83
84
	public function testSaveJpeg()
85
	{
86
		$ds = DIRECTORY_SEPARATOR;
87
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
88
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.jpg';
89
90
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', array() );
91
		$media->save( $dest, 'image/jpeg' );
92
93
		$this->assertEquals( true, file_exists( $dest ) );
94
		unlink( $dest );
95
	}
96
97
98
	public function testSaveJpegInvalidDest()
99
	{
100
		$ds = DIRECTORY_SEPARATOR;
101
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
102
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.jpg';
103
104
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
105
106
		$this->setExpectedException( '\\Aimeos\\MW\\Media\\Exception' );
107
		$media->save( $dest, 'image/jpeg' );
108
	}
109
110
111
	public function testSavePng()
112
	{
113
		$ds = DIRECTORY_SEPARATOR;
114
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
115
		$dest = dirname( dirname( dirname( __DIR__ ) ) ) . $ds . 'tmp' . $ds . 'media.png';
116
117
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', array() );
118
		$media->save( $dest, 'image/png' );
119
120
		$this->assertEquals( true, file_exists( $dest ) );
121
		unlink( $dest );
122
	}
123
124
125
	public function testSavePngInvalidDest()
126
	{
127
		$ds = DIRECTORY_SEPARATOR;
128
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.gif' );
129
		$dest = __DIR__ . $ds . 'notexisting' . $ds . 'media.png';
130
131
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/gif', array() );
132
133
		$this->setExpectedException( '\\Aimeos\\MW\\Media\\Exception' );
134
		$media->save( $dest, 'image/png' );
135
	}
136
137
138
	public function testScale()
139
	{
140
		$ds = DIRECTORY_SEPARATOR;
141
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
142
143
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
144
		$info = getimagesizefromstring( $media->scale( 100, 100, false )->save( null, 'image/png' ) );
145
146
		$this->assertEquals( 100, $info[0] );
147
		$this->assertEquals( 100, $info[1] );
148
	}
149
150
151
	public function testScaleFit()
152
	{
153
		$ds = DIRECTORY_SEPARATOR;
154
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
155
156
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
157
		$info = getimagesizefromstring( $media->scale( 5, 100 )->save( null, 'image/png' ) );
158
159
		$this->assertEquals( 5, $info[0] );
160
		$this->assertEquals( 5, $info[1] );
161
	}
162
163
164
	public function testScaleFitInside()
165
	{
166
		$ds = DIRECTORY_SEPARATOR;
167
		$content = file_get_contents( dirname( __DIR__ ) . $ds . '_testfiles' . $ds . 'image.png' );
168
169
		$media = new \Aimeos\MW\Media\Image\Imagick( $content, 'image/png', array() );
170
		$info = getimagesizefromstring( $media->scale( 100, 100 )->save( null, 'image/png' ) );
171
172
		$this->assertEquals( 10, $info[0] );
173
		$this->assertEquals( 10, $info[1] );
174
	}
175
}
176