1 | <?php |
||
14 | class AutoRotateFilterLoaderTest extends AbstractTest |
||
15 | { |
||
16 | private $orientationKey = 'exif.Orientation'; |
||
17 | |||
18 | /** |
||
19 | * Starts a test with expected results. |
||
20 | * |
||
21 | * @param $exifValue {String} The exif value to be returned by the metadata mock. |
||
22 | * @param $expectCallRotateValue {null|number} The expected rotation value, null if no rotation is expected. |
||
23 | * @param $expectCallFlip {Boolean} True if a horizontal flip is expected, false otherwise. |
||
24 | */ |
||
25 | private function loadExif($exifValue, $expectCallRotateValue, $expectCallFlip) |
||
26 | { |
||
27 | $loader = new AutoRotateFilterLoader(); |
||
28 | |||
29 | // Mocks the image and makes it use the fake meta data. |
||
30 | $image = $this->getMockImage(); |
||
31 | |||
32 | if (method_exists('Imagine\Image\ImageInterface', 'metadata')) { |
||
33 | // Mocks the metadata and makes it return the expected exif value for the rotation. |
||
34 | // If $exifValue is null, it means the image doesn't contain any metadata. |
||
35 | $metaData = $this->getMockMetaData(); |
||
36 | |||
37 | $metaData |
||
38 | ->expects($this->atLeastOnce()) |
||
39 | ->method('offsetGet') |
||
40 | ->willReturn($exifValue); |
||
41 | |||
42 | if ($exifValue && $exifValue > '1' && $exifValue <= 8) { |
||
43 | $metaData |
||
44 | ->expects($this->once()) |
||
45 | ->method('offsetSet') |
||
46 | ->with($this->orientationKey, '1'); |
||
47 | } |
||
48 | |||
49 | $image |
||
50 | ->expects($this->atLeastOnce()) |
||
51 | ->method('metadata') |
||
52 | ->willReturn($metaData); |
||
53 | } else { |
||
54 | $jpg = file_get_contents(__DIR__.'/../../../Fixtures/assets/pixel_1x1_orientation_at_0x30.jpg'); |
||
55 | // The byte with orientation is at offset 0x30 for this image |
||
56 | $jpg[0x30] = chr((int) $exifValue); |
||
57 | |||
58 | $image |
||
59 | ->expects($this->once()) |
||
60 | ->method('get') |
||
61 | ->with('jpg') |
||
62 | ->will($this->returnValue($jpg)); |
||
63 | } |
||
64 | |||
65 | // Checks that rotate is called with $expectCallRotateValue, or not called at all if $expectCallRotateValue is null. |
||
66 | $image |
||
67 | ->expects($expectCallRotateValue !== null ? $this->once() : $this->never()) |
||
68 | ->method('rotate') |
||
69 | ->with($expectCallRotateValue); |
||
70 | |||
71 | // Checks that rotate is called if $expectCallFlip is true, not called if $expectCallFlip is false. |
||
72 | $image |
||
73 | ->expects($expectCallFlip ? $this->once() : $this->never()) |
||
74 | ->method('flipHorizontally'); |
||
75 | |||
76 | $loader->load($image); |
||
77 | } |
||
78 | |||
79 | /* |
||
80 | * Possible rotation values |
||
81 | * 1: 0° |
||
82 | * 2: 0° flipped horizontally |
||
83 | * 3: 180° |
||
84 | * 4: 180° flipped horizontally |
||
85 | * 5: 90° flipped horizontally |
||
86 | * 6: 90° |
||
87 | * 7: -90° flipped horizontally |
||
88 | * 8: -90° |
||
89 | * No metadata means no rotation nor flip. |
||
90 | */ |
||
91 | |||
92 | /** |
||
93 | * 1: no rotation. |
||
94 | */ |
||
95 | public function testLoadExif1() |
||
99 | |||
100 | /** |
||
101 | * 2: no rotation flipped horizontally. |
||
102 | */ |
||
103 | public function testLoadExif2() |
||
107 | |||
108 | /** |
||
109 | * 3: 180°. |
||
110 | */ |
||
111 | public function testLoadExif3() |
||
115 | |||
116 | /** |
||
117 | * 4: 180° flipped horizontally. |
||
118 | */ |
||
119 | public function testLoadExif4() |
||
123 | |||
124 | /** |
||
125 | * 5: 90° flipped horizontally. |
||
126 | */ |
||
127 | public function testLoadExif5() |
||
131 | |||
132 | /** |
||
133 | * 6: 90°. |
||
134 | */ |
||
135 | public function testLoadExif6() |
||
139 | |||
140 | /** |
||
141 | * 7: -90° flipped horizontally. |
||
142 | */ |
||
143 | public function testLoadExif7() |
||
147 | |||
148 | /** |
||
149 | * 8: -90°. |
||
150 | */ |
||
151 | public function testLoadExif8() |
||
155 | |||
156 | /** |
||
157 | * Theoretically Orientation is `short` (uint16), so it could be anything |
||
158 | * from [0; 65535]. |
||
159 | * |
||
160 | * @expectedException \Imagine\Exception\InvalidArgumentException |
||
161 | */ |
||
162 | public function testLoadExifInvalid() |
||
166 | |||
167 | /** |
||
168 | * No rotation info: no rotation nor flip. |
||
169 | */ |
||
170 | public function testLoadExifNull() |
||
174 | } |
||
175 |