ThumbnailerTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
rs 9.4286
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace DmFilemanTest\Service\Thumbnailer;
4
5
use DmFileman\Service\Thumbnailer\Thumbnailer;
6
use org\bovigo\vfs;
7
8
class ThumbnailerTest extends \PHPUnit_Framework_TestCase
9
{
10
    /** @var Thumbnailer */
11
    private $sut;
12
13
    /** @var \PHPUnit_Framework_MockObject_MockObject */
14
    private $imagineMock;
15
16
    /** @var \PHPUnit_Framework_MockObject_MockObject */
17
    private $factoryMock;
18
19
    /**
20
     * @var vfs\vfsStreamDirectory
21
     */
22
    private $root;
23
24
    protected function setUp()
25
    {
26
        $this->imagineMock = $this->getMockBuilder('Imagine\Gd\Imagine')
27
            ->setMethods(['open', 'resize', 'crop', 'save'])
28
            ->disableAutoload()
29
            ->getMock();
30
31
        $this->imagineMock->expects($this->any())->method('resize')->will($this->returnSelf());
32
        $this->imagineMock->expects($this->any())->method('crop')->will($this->returnSelf());
33
34
        $this->factoryMock = $this->getMockBuilder('DmFileman\Service\Thumbnailer\Factory')
35
            ->setMethods(['getBox', 'getPoint'])
36
            ->getMock();
37
38
        $imageContent = file_get_contents(__DIR__ . '/fixtures/orig/white.gif');
39
        $structure    = ['orig' => ['white.gif' => $imageContent], 'thumb' => ['white.gif' => '']];
40
        $this->root   = vfs\vfsStream::setup('root', 0777, $structure);
41
42
        $this->sut = new Thumbnailer($this->imagineMock, $this->factoryMock);
43
    }
44
45
    /**
46
     * @covers DmFileman\Service\Thumbnailer\Thumbnailer
47
     */
48
    public function testResizeGetsOnlyCopiesWhenImageIsSmallerThanAllowedThumbnailSize()
49
    {
50
        $this->factoryMock
51
            ->expects($this->never())
52
            ->method('getBox');
53
54
        $this->factoryMock
55
            ->expects($this->never())
56
            ->method('getPoint');
57
58
        $this->imagineMock
59
            ->expects($this->never())
60
            ->method('open');
61
62
        $this->imagineMock
63
            ->expects($this->never())
64
            ->method('resize');
65
66
        $this->imagineMock
67
            ->expects($this->never())
68
            ->method('crop');
69
70
        $this->imagineMock
71
            ->expects($this->never())
72
            ->method('save');
73
74
        $this->sut->setThumbConfig([Thumbnailer::CONFIG_WIDTH => 100, Thumbnailer::CONFIG_HEIGHT => 100]);
75
76
        $origData = '12345678901';
77
78
        $origFile  = vfs\vfsStream::url('root/orig.txt');
79
        $thumbFile = vfs\vfsStream::url('root/thumb.txt');
80
81
        file_put_contents($origFile, $origData);
82
83
        $this->sut->resize($origFile, $thumbFile, [50, 50]);
84
85
        $this->assertEquals(file_get_contents($thumbFile), $origData);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getResizeProvider()
92
    {
93
        return [
94
            // dataSet #1
95
            [
96
                'orig1',
97
                'thumb1',
98
                [
99
                    Thumbnailer::CONFIG_WIDTH  => 100,
100
                    Thumbnailer::CONFIG_HEIGHT => 100
101
                ],
102
                [
103
                    0 => 200,
104
                    1 => 200
105
                ],
106
                [
107
                    100,
108
                    100
109
                ],
110
                [
111
                    0,
112
                    0
113
                ],
114
                [
115
                    100,
116
                    100
117
                ]
118
            ],
119
            // dataSet #2
120
            [
121
                'orig1',
122
                'thumb1',
123
                [
124
                    Thumbnailer::CONFIG_WIDTH  => 100,
125
                    Thumbnailer::CONFIG_HEIGHT => 100
126
                ],
127
                [
128
                    0 => 200,
129
                    1 => 300
130
                ],
131
                [
132
                    100,
133
                    150
134
                ],
135
                [
136
                    0,
137
                    25
138
                ]
139
            ],
140
            // dataSet #3
141
            [
142
                'orig1',
143
                'thumb1',
144
                [
145
                    Thumbnailer::CONFIG_WIDTH  => 100,
146
                    Thumbnailer::CONFIG_HEIGHT => 100
147
                ],
148
                [
149
                    0 => 300,
150
                    1 => 200
151
                ],
152
                [
153
                    150,
154
                    100
155
                ],
156
                [
157
                    25,
158
                    0
159
                ]
160
            ]
161
        ];
162
    }
163
164
    /**
165
     * @covers       DmFileman\Service\Thumbnailer\Thumbnailer
166
     * @dataProvider getResizeProvider
167
     *
168
     * @param string $origName
169
     * @param string $thumbName
170
     * @param array  $thumbConfig
171
     * @param array  $origInfo
172
     * @param array  $resizeParameters
173
     * @param array  $cropStartParameters
174
     */
175
    public function testResizeGetsSizesRight(
176
        $origName,
177
        $thumbName,
178
        array $thumbConfig,
179
        array $origInfo,
180
        array $resizeParameters,
181
        array $cropStartParameters
182
    ) {
183
        $this->factoryMock
184
            ->expects($this->at(0))
185
            ->method('getBox')
186
            ->with($this->equalTo($resizeParameters[0]), $this->equalTo($resizeParameters[1]))
187
            ->will($this->returnValue('foo-resizeSize'));
188
189
        $this->factoryMock
190
            ->expects($this->at(1))
191
            ->method('getPoint')
192
            ->with($this->equalTo($cropStartParameters[0]), $this->equalTo($cropStartParameters[1]))
193
            ->will($this->returnValue('bar-cropStart'));
194
195
        $this->factoryMock
196
            ->expects($this->at(2))
197
            ->method('getBox')
198
            ->with(
199
                $this->equalTo($thumbConfig[Thumbnailer::CONFIG_WIDTH]),
200
                $this->equalTo($thumbConfig[Thumbnailer::CONFIG_HEIGHT])
201
            )
202
            ->will($this->returnValue('bar-cropSize'));
203
204
        $this->imagineMock
205
            ->expects($this->any())
206
            ->method('open')
207
            ->with($this->equalTo($origName))
208
            ->will($this->returnSelf());
209
210
        $this->imagineMock
211
            ->expects($this->any())
212
            ->method('resize')
213
            ->with($this->equalTo('foo-resizeSize'))
214
            ->will($this->returnSelf());
215
216
        $this->imagineMock
217
            ->expects($this->any())
218
            ->method('crop')
219
            ->with($this->equalTo('bar-cropStart'), $this->equalTo('bar-cropSize'))
220
            ->will($this->returnSelf());
221
222
        $this->imagineMock
223
            ->expects($this->any())
224
            ->method('save')
225
            ->with($this->equalTo($thumbName))
226
            ->will($this->returnSelf());
227
228
        $this->sut->setThumbConfig($thumbConfig);
229
230
        $this->sut->resize($origName, $thumbName, $origInfo);
231
    }
232
233
    /**
234
     * @covers            DmFileman\Service\Thumbnailer\Thumbnailer
235
     * @expectedException \InvalidArgumentException
236
     */
237
    public function testConstructorThrowsExceptionOnUnknownImagineImplementation()
238
    {
239
        $this->imagineMock = $this->getMockBuilder('Imagine\Gd\ImagineInterface')
240
            ->setMethods([])
241
            ->disableAutoload()
242
            ->getMock();
243
244
        $this->factoryMock = $this->getMockBuilder('DmFileman\Service\Thumbnailer\Factory')
245
            ->setMethods([])
246
            ->getMock();
247
248
        $this->sut = new Thumbnailer($this->imagineMock, $this->factoryMock);
249
    }
250
251
    /**
252
     * @covers DmFileman\Service\Thumbnailer\Thumbnailer
253
     */
254
    public function testResizeOrigImageReturnsFalseIfFileDoesNotExist()
255
    {
256
        $origName = 'asd';
257
        $origDir  = vfs\vfsStream::url('root/orig');
258
        $thumbDir = vfs\vfsStream::url('root/thumb');
259
260
        $actualResult = $this->sut->resizeOrigImage($origName, $origDir, $thumbDir);
261
262
        $this->assertFalse($actualResult);
263
    }
264
265
    /**
266
     * @covers DmFileman\Service\Thumbnailer\Thumbnailer
267
     */
268
    public function testResizeOrigImageReturnsFalseIfGetimagesizeFails()
269
    {
270
        $origName = vfs\vfsStream::url('root/thumb/white.gif');
271
        $origDir  = vfs\vfsStream::url('root/orig');
272
        $thumbDir = vfs\vfsStream::url('root/thumb');
273
274
        $actualResult = $this->sut->resizeOrigImage($origName, $origDir, $thumbDir);
275
276
        $this->assertFalse($actualResult);
277
    }
278
279
    /**
280
     * @covers DmFileman\Service\Thumbnailer\Thumbnailer
281
     */
282
    public function testResizeOrigImageReturnsTrueIfGetimagesizeIsSuccessful()
283
    {
284
        $origName = vfs\vfsStream::url('root/orig/white.gif');
285
        $origDir  = vfs\vfsStream::url('root/orig');
286
        $thumbDir = vfs\vfsStream::url('root/thumb');
287
288
        $this->sut->setThumbConfig([Thumbnailer::CONFIG_WIDTH => 10000, Thumbnailer::CONFIG_HEIGHT => 10000]);
289
290
        $actualResult = $this->sut->resizeOrigImage($origName, $origDir, $thumbDir);
291
292
        $this->assertTrue($actualResult);
293
    }
294
}
295