Completed
Push — ezp25003-trash_subitems_count_... ( cf1e00...cc3aa3 )
by André
34:07
created

AliasGeneratorTest::testGetVariationOriginal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
176
        $this->dataLoader
177
            ->expects($this->once())
178
            ->method('find')
179
            ->with($originalPath)
180
            ->will($this->returnValue($binary));
181
        $this->filterManager
182
            ->expects($this->never())
183
            ->method('applyFilter')
184
            ->with($binary, $variationName)
185
            ->will($this->returnValue($binary));
186
        $this->ioResolver
187
            ->expects($this->never())
188
            ->method('store')
189
            ->with($binary, $originalPath, $variationName);
190
        $this->ioResolver
191
            ->expects($this->once())
192
            ->method('resolve')
193
            ->with($originalPath, $variationName)
194
            ->will($this->returnValue($expectedUrl));
195
196
        $expected = new ImageVariation(
197
            array(
198
                'name' => $variationName,
199
                'fileName' => 'image.jpg',
200
                'dirPath' => 'http://localhost/foo/bar',
201
                'uri' => $expectedUrl,
202
                'imageId' => $imageId,
203
            )
204
        );
205
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
206
    }
207
208
    public function testGetVariationNotStoredHavingReferences()
209
    {
210
        $originalPath = 'foo/bar/image.jpg';
211
        $variationName = 'my_variation';
212
        $reference1 = 'reference1';
213
        $reference2 = 'reference2';
214
        $configVariation = array('reference' => $reference1);
215
        $configReference1 = array('reference' => $reference2);
216
        $configReference2 = array();
217
        $this->filterConfiguration->set($variationName, $configVariation);
218
        $this->filterConfiguration->set($reference1, $configReference1);
219
        $this->filterConfiguration->set($reference2, $configReference2);
220
        $imageId = '123-45';
221
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
222
        $field = new Field(array('value' => $imageValue));
223
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
224
225
        $this->ioResolver
226
            ->expects($this->once())
227
            ->method('isStored')
228
            ->with($originalPath, $variationName)
229
            ->will($this->returnValue(false));
230
231
        $this->logger
232
            ->expects($this->once())
233
            ->method('debug');
234
235
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
236
        $this->dataLoader
237
            ->expects($this->once())
238
            ->method('find')
239
            ->with($originalPath)
240
            ->will($this->returnValue($binary));
241
242
        // Filter manager is supposed to be called 3 times to generate references, and then passed variation.
243
        $this->filterManager
244
            ->expects($this->at(0))
245
            ->method('applyFilter')
246
            ->with($binary, $reference2)
247
            ->will($this->returnValue($binary));
248
        $this->filterManager
249
            ->expects($this->at(1))
250
            ->method('applyFilter')
251
            ->with($binary, $reference1)
252
            ->will($this->returnValue($binary));
253
        $this->filterManager
254
            ->expects($this->at(2))
255
            ->method('applyFilter')
256
            ->with($binary, $variationName)
257
            ->will($this->returnValue($binary));
258
259
        $this->ioResolver
260
            ->expects($this->once())
261
            ->method('store')
262
            ->with($binary, $originalPath, $variationName);
263
        $this->ioResolver
264
            ->expects($this->once())
265
            ->method('resolve')
266
            ->with($originalPath, $variationName)
267
            ->will($this->returnValue($expectedUrl));
268
269
        $expected = new ImageVariation(
270
            array(
271
                'name' => $variationName,
272
                'fileName' => "image_$variationName.jpg",
273
                'dirPath' => 'http://localhost/foo/bar',
274
                'uri' => $expectedUrl,
275
                'imageId' => $imageId,
276
            )
277
        );
278
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
279
    }
280
281
    public function testGetVariationAlreadyStored()
282
    {
283
        $originalPath = 'foo/bar/image.jpg';
284
        $variationName = 'my_variation';
285
        $imageId = '123-45';
286
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
287
        $field = new Field(array('value' => $imageValue));
288
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
289
290
        $this->ioResolver
291
            ->expects($this->once())
292
            ->method('isStored')
293
            ->with($originalPath, $variationName)
294
            ->will($this->returnValue(true));
295
296
        $this->logger
297
            ->expects($this->once())
298
            ->method('debug');
299
300
        $this->dataLoader
301
            ->expects($this->once())
302
            ->method('find');
303
        $this->filterManager
304
            ->expects($this->never())
305
            ->method('applyFilter');
306
        $this->ioResolver
307
            ->expects($this->never())
308
            ->method('store');
309
310
        $this->ioResolver
311
            ->expects($this->once())
312
            ->method('resolve')
313
            ->with($originalPath, $variationName)
314
            ->will($this->returnValue($expectedUrl));
315
316
        $expected = new ImageVariation(
317
            array(
318
                'name' => $variationName,
319
                'fileName' => "image_$variationName.jpg",
320
                'dirPath' => 'http://localhost/foo/bar',
321
                'uri' => $expectedUrl,
322
                'imageId' => $imageId,
323
            )
324
        );
325
        $this->assertEquals($expected, $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName));
326
    }
327
328
    /**
329
     * @expectedException \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
330
     */
331
    public function testGetVariationOriginalNotFound()
332
    {
333
        $this->dataLoader
334
            ->expects($this->once())
335
            ->method('find')
336
            ->will($this->throwException(new NotLoadableException()));
337
338
        $field = new Field(array('value' => new ImageValue()));
339
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
340
    }
341
342
    /**
343
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
344
     */
345
    public function testGetVariationInvalidVariation()
346
    {
347
        $originalPath = 'foo/bar/image.jpg';
348
        $variationName = 'my_variation';
349
        $imageId = '123-45';
350
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
351
        $field = new Field(array('value' => $imageValue));
352
353
        $this->ioResolver
354
            ->expects($this->once())
355
            ->method('isStored')
356
            ->with($originalPath, $variationName)
357
            ->will($this->returnValue(true));
358
359
        $this->logger
360
            ->expects($this->once())
361
            ->method('debug');
362
363
        $this->dataLoader
364
            ->expects($this->once())
365
            ->method('find');
366
        $this->filterManager
367
            ->expects($this->never())
368
            ->method('applyFilter');
369
        $this->ioResolver
370
            ->expects($this->never())
371
            ->method('store');
372
373
        $this->ioResolver
374
            ->expects($this->once())
375
            ->method('resolve')
376
            ->with($originalPath, $variationName)
377
            ->will($this->throwException(new NotResolvableException()));
378
379
        $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName);
380
    }
381
}
382