Completed
Push — master ( 1f03c4...ae54df )
by André
47:22 queued 24:43
created

AliasGeneratorTest::testGetVariationOriginal()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 36
rs 8.8571
1
<?php
2
3
/**
4
 * File containing the AliasGeneratorTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishCoreBundle\Tests\Imagine;
12
13
use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator;
14
use eZ\Publish\API\Repository\Values\Content\Field;
15
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
16
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
17
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
18
use eZ\Publish\SPI\Variation\Values\ImageVariation;
19
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
20
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
21
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
22
use PHPUnit_Framework_TestCase;
23
24
class AliasGeneratorTest extends PHPUnit_Framework_TestCase
25
{
26
    /**
27
     * @var \PHPUnit_Framework_MockObject_MockObject
28
     */
29
    private $dataLoader;
30
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject
33
     */
34
    private $filterManager;
35
36
    /**
37
     * @var \PHPUnit_Framework_MockObject_MockObject
38
     */
39
    private $ioResolver;
40
41
    /**
42
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration
43
     */
44
    private $filterConfiguration;
45
46
    /**
47
     * @var \PHPUnit_Framework_MockObject_MockObject
48
     */
49
    private $logger;
50
51
    /**
52
     * @var AliasGenerator
53
     */
54
    private $aliasGenerator;
55
56
    protected function setUp()
57
    {
58
        parent::setUp();
59
        $this->dataLoader = $this->getMock('\Liip\ImagineBundle\Binary\Loader\LoaderInterface');
60
        $this->filterManager = $this
61
            ->getMockBuilder('\Liip\ImagineBundle\Imagine\Filter\FilterManager')
62
            ->disableOriginalConstructor()
63
            ->getMock();
64
        $this->ioResolver = $this->getMock('\Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface');
65
        $this->filterConfiguration = new FilterConfiguration();
66
        $this->logger = $this->getMock('\Psr\Log\LoggerInterface');
67
        $this->aliasGenerator = new AliasGenerator(
68
            $this->dataLoader,
69
            $this->filterManager,
70
            $this->ioResolver,
71
            $this->filterConfiguration,
72
            $this->logger
73
        );
74
    }
75
76
    /**
77
     * @dataProvider supportsValueProvider
78
     */
79
    public function testSupportsValue($value, $isSupported)
80
    {
81
        $this->assertSame($isSupported, $this->aliasGenerator->supportsValue($value));
82
    }
83
84
    public function supportsValueProvider()
85
    {
86
        return array(
87
            array($this->getMock('\eZ\Publish\Core\FieldType\Value'), false),
88
            array(new TextLineValue(), false),
89
            array(new ImageValue(), true),
90
            array($this->getMock('\eZ\Publish\Core\FieldType\Image\Value'), true),
91
        );
92
    }
93
94
    /**
95
     * @expectedException \InvalidArgumentException
96
     */
97
    public function testGetVariationWrongValue()
98
    {
99
        $field = new Field(array('value' => $this->getMock('eZ\Publish\Core\FieldType\Value')));
100
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
101
    }
102
103
    public function testGetVariationNotStored()
104
    {
105
        $originalPath = 'foo/bar/image.jpg';
106
        $variationName = 'my_variation';
107
        $this->filterConfiguration->set($variationName, array());
108
        $imageId = '123-45';
109
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
110
        $field = new Field(array('value' => $imageValue));
111
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
112
113
        $this->ioResolver
114
            ->expects($this->once())
115
            ->method('isStored')
116
            ->with($originalPath, $variationName)
117
            ->will($this->returnValue(false));
118
119
        $this->logger
120
            ->expects($this->once())
121
            ->method('debug');
122
123
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
124
        $this->dataLoader
125
            ->expects($this->once())
126
            ->method('find')
127
            ->with($originalPath)
128
            ->will($this->returnValue($binary));
129
        $this->filterManager
130
            ->expects($this->once())
131
            ->method('applyFilter')
132
            ->with($binary, $variationName)
133
            ->will($this->returnValue($binary));
134
        $this->ioResolver
135
            ->expects($this->once())
136
            ->method('store')
137
            ->with($binary, $originalPath, $variationName);
138
        $this->ioResolver
139
            ->expects($this->once())
140
            ->method('resolve')
141
            ->with($originalPath, $variationName)
142
            ->will($this->returnValue($expectedUrl));
143
144
        $expected = new ImageVariation(
145
            array(
146
                'name' => $variationName,
147
                'fileName' => "image_$variationName.jpg",
148
                'dirPath' => 'http://localhost/foo/bar',
149
                'uri' => $expectedUrl,
150
                'imageId' => $imageId,
151
            )
152
        );
153
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
154
    }
155
156
    public function testGetVariationOriginal()
157
    {
158
        $originalPath = 'foo/bar/image.jpg';
159
        $variationName = 'original';
160
        $imageId = '123-45';
161
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
162
        $field = new Field(array('value' => $imageValue));
163
        $expectedUrl = 'http://localhost/foo/bar/image.jpg';
164
165
        $this->ioResolver
166
            ->expects($this->never())
167
            ->method('isStored')
168
            ->with($originalPath, $variationName)
169
            ->will($this->returnValue(false));
170
171
        $this->logger
172
            ->expects($this->once())
173
            ->method('debug');
174
175
        $this->ioResolver
176
            ->expects($this->once())
177
            ->method('resolve')
178
            ->with($originalPath, $variationName)
179
            ->will($this->returnValue($expectedUrl));
180
181
        $expected = new ImageVariation(
182
            array(
183
                'name' => $variationName,
184
                'fileName' => 'image.jpg',
185
                'dirPath' => 'http://localhost/foo/bar',
186
                'uri' => $expectedUrl,
187
                'imageId' => $imageId,
188
            )
189
        );
190
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
191
    }
192
193
    public function testGetVariationNotStoredHavingReferences()
194
    {
195
        $originalPath = 'foo/bar/image.jpg';
196
        $variationName = 'my_variation';
197
        $reference1 = 'reference1';
198
        $reference2 = 'reference2';
199
        $configVariation = array('reference' => $reference1);
200
        $configReference1 = array('reference' => $reference2);
201
        $configReference2 = array();
202
        $this->filterConfiguration->set($variationName, $configVariation);
203
        $this->filterConfiguration->set($reference1, $configReference1);
204
        $this->filterConfiguration->set($reference2, $configReference2);
205
        $imageId = '123-45';
206
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
207
        $field = new Field(array('value' => $imageValue));
208
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
209
210
        $this->ioResolver
211
            ->expects($this->once())
212
            ->method('isStored')
213
            ->with($originalPath, $variationName)
214
            ->will($this->returnValue(false));
215
216
        $this->logger
217
            ->expects($this->once())
218
            ->method('debug');
219
220
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
221
        $this->dataLoader
222
            ->expects($this->once())
223
            ->method('find')
224
            ->with($originalPath)
225
            ->will($this->returnValue($binary));
226
227
        // Filter manager is supposed to be called 3 times to generate references, and then passed variation.
228
        $this->filterManager
229
            ->expects($this->at(0))
230
            ->method('applyFilter')
231
            ->with($binary, $reference2)
232
            ->will($this->returnValue($binary));
233
        $this->filterManager
234
            ->expects($this->at(1))
235
            ->method('applyFilter')
236
            ->with($binary, $reference1)
237
            ->will($this->returnValue($binary));
238
        $this->filterManager
239
            ->expects($this->at(2))
240
            ->method('applyFilter')
241
            ->with($binary, $variationName)
242
            ->will($this->returnValue($binary));
243
244
        $this->ioResolver
245
            ->expects($this->once())
246
            ->method('store')
247
            ->with($binary, $originalPath, $variationName);
248
        $this->ioResolver
249
            ->expects($this->once())
250
            ->method('resolve')
251
            ->with($originalPath, $variationName)
252
            ->will($this->returnValue($expectedUrl));
253
254
        $expected = new ImageVariation(
255
            array(
256
                'name' => $variationName,
257
                'fileName' => "image_$variationName.jpg",
258
                'dirPath' => 'http://localhost/foo/bar',
259
                'uri' => $expectedUrl,
260
                'imageId' => $imageId,
261
            )
262
        );
263
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
264
    }
265
266
    public function testGetVariationAlreadyStored()
267
    {
268
        $originalPath = 'foo/bar/image.jpg';
269
        $variationName = 'my_variation';
270
        $imageId = '123-45';
271
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
272
        $field = new Field(array('value' => $imageValue));
273
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
274
275
        $this->ioResolver
276
            ->expects($this->once())
277
            ->method('isStored')
278
            ->with($originalPath, $variationName)
279
            ->will($this->returnValue(true));
280
281
        $this->logger
282
            ->expects($this->once())
283
            ->method('debug');
284
285
        $this->dataLoader
286
            ->expects($this->never())
287
            ->method('find');
288
        $this->filterManager
289
            ->expects($this->never())
290
            ->method('applyFilter');
291
        $this->ioResolver
292
            ->expects($this->never())
293
            ->method('store');
294
295
        $this->ioResolver
296
            ->expects($this->once())
297
            ->method('resolve')
298
            ->with($originalPath, $variationName)
299
            ->will($this->returnValue($expectedUrl));
300
301
        $expected = new ImageVariation(
302
            array(
303
                'name' => $variationName,
304
                'fileName' => "image_$variationName.jpg",
305
                'dirPath' => 'http://localhost/foo/bar',
306
                'uri' => $expectedUrl,
307
                'imageId' => $imageId,
308
            )
309
        );
310
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
311
    }
312
313
    /**
314
     * @expectedException \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
315
     */
316
    public function testGetVariationOriginalNotFound()
317
    {
318
        $this->dataLoader
319
            ->expects($this->once())
320
            ->method('find')
321
            ->will($this->throwException(new NotLoadableException()));
322
323
        $field = new Field(array('value' => new ImageValue()));
324
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
325
    }
326
327
    /**
328
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
329
     */
330
    public function testGetVariationInvalidVariation()
331
    {
332
        $originalPath = 'foo/bar/image.jpg';
333
        $variationName = 'my_variation';
334
        $imageId = '123-45';
335
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
336
        $field = new Field(array('value' => $imageValue));
337
338
        $this->ioResolver
339
            ->expects($this->once())
340
            ->method('isStored')
341
            ->with($originalPath, $variationName)
342
            ->will($this->returnValue(true));
343
344
        $this->logger
345
            ->expects($this->once())
346
            ->method('debug');
347
348
        $this->dataLoader
349
            ->expects($this->never())
350
            ->method('find');
351
        $this->filterManager
352
            ->expects($this->never())
353
            ->method('applyFilter');
354
        $this->ioResolver
355
            ->expects($this->never())
356
            ->method('store');
357
358
        $this->ioResolver
359
            ->expects($this->once())
360
            ->method('resolve')
361
            ->with($originalPath, $variationName)
362
            ->will($this->throwException(new NotResolvableException()));
363
364
        $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName);
365
    }
366
}
367