Completed
Pull Request — master (#732)
by 12345
03:41
created

AutoRotateFilterLoaderTest::testLoadExif8()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 4
c 2
b 1
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Liip\ImagineBundle\Tests\Filter;
4
5
use Liip\ImagineBundle\Imagine\Filter\Loader\AutoRotateFilterLoader;
6
use Liip\ImagineBundle\Tests\AbstractTest;
7
8
/**
9
 * Test cases for RotateFilterLoader class.
10
 * Depending on the EXIF value checks whether rotate and flip are called.
11
 *
12
 * @covers Liip\ImagineBundle\Imagine\Filter\Loader\AutoRotateFilterLoader
13
 */
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()
96
    {
97
        $this->loadExif('1', null, false);
98
    }
99
100
    /**
101
     * 2: no rotation flipped horizontally.
102
     */
103
    public function testLoadExif2()
104
    {
105
        $this->loadExif('2', null, true);
106
    }
107
108
    /**
109
     * 3: 180°.
110
     */
111
    public function testLoadExif3()
112
    {
113
        $this->loadExif('3', 180, false);
114
    }
115
116
    /**
117
     * 4: 180° flipped horizontally.
118
     */
119
    public function testLoadExif4()
120
    {
121
        $this->loadExif('4', 180, true);
122
    }
123
124
    /**
125
     * 5: 90° flipped horizontally.
126
     */
127
    public function testLoadExif5()
128
    {
129
        $this->loadExif('5', 90, true);
130
    }
131
132
    /**
133
     * 6: 90°.
134
     */
135
    public function testLoadExif6()
136
    {
137
        $this->loadExif('6', 90, false);
138
    }
139
140
    /**
141
     * 7: -90° flipped horizontally.
142
     */
143
    public function testLoadExif7()
144
    {
145
        $this->loadExif('7', -90, true);
146
    }
147
148
    /**
149
     * 8: -90°.
150
     */
151
    public function testLoadExif8()
152
    {
153
        $this->loadExif('8', -90, false);
154
    }
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()
163
    {
164
        $this->loadExif('10', null, false);
165
    }
166
167
    /**
168
     * No rotation info: no rotation nor flip.
169
     */
170
    public function testLoadExifNull()
171
    {
172
        $this->loadExif(null, null, false);
173
    }
174
}
175