Completed
Push — 6.7 ( a85f61...1fb9e9 )
by Łukasz
46:47 queued 23:40
created

assertImageVariationIsCorrect()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 59
nc 1
nop 6
dl 0
loc 76
rs 8.9667
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
namespace eZ\Bundle\EzPublishCoreBundle\Tests\Imagine;
10
11
use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator;
12
use eZ\Bundle\EzPublishCoreBundle\Imagine\Variation\ImagineAwareAliasGenerator;
13
use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator;
14
use eZ\Publish\API\Repository\Values\Content\Field;
15
use eZ\Publish\Core\Base\Tests\PHPUnit5CompatTrait;
16
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
17
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
18
use eZ\Publish\SPI\FieldType\Value as FieldTypeValue;
19
use eZ\Publish\Core\IO\IOServiceInterface;
20
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
21
use eZ\Publish\SPI\Variation\Values\ImageVariation;
22
use Imagine\Image\BoxInterface;
23
use Imagine\Image\ImageInterface;
24
use Imagine\Image\ImagineInterface;
25
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
26
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
27
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
28
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
29
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
30
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
31
use PHPUnit\Framework\TestCase;
32
use Psr\Log\LoggerInterface;
33
34
class AliasGeneratorTest extends TestCase
35
{
36
    use PHPUnit5CompatTrait;
37
38
    /**
39
     * @var \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Binary\Loader\LoaderInterface
40
     */
41
    private $dataLoader;
42
43
    /**
44
     * @var \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Imagine\Filter\FilterManager
45
     */
46
    private $filterManager;
47
48
    /**
49
     * @var \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface
50
     */
51
    private $ioResolver;
52
53
    /**
54
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration
55
     */
56
    private $filterConfiguration;
57
58
    /**
59
     * @var \PHPUnit_Framework_MockObject_MockObject|\Psr\Log\LoggerInterface
60
     */
61
    private $logger;
62
63
    /**
64
     * @var \PHPUnit_Framework_MockObject_MockObject|\Imagine\Image\ImagineInterface
65
     */
66
    private $imagine;
67
68
    /**
69
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator
70
     */
71
    private $aliasGenerator;
72
73
    /**
74
     * @var \eZ\Publish\SPI\Variation\VariationHandler
75
     */
76
    private $decoratedAliasGenerator;
77
78
    /**
79
     * @var \PHPUnit_Framework_MockObject_MockObject|\Imagine\Image\BoxInterface
80
     */
81
    private $box;
82
83
    /**
84
     * @var \PHPUnit_Framework_MockObject_MockObject|\Imagine\Image\ImageInterface
85
     */
86
    private $image;
87
88
    /**
89
     * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\IO\IOServiceInterface
90
     */
91
    private $ioService;
92
93
    /**
94
     * @var \PHPUnit_Framework_MockObject_MockObject|\eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator
95
     */
96
    private $variationPathGenerator;
97
98
    protected function setUp()
99
    {
100
        parent::setUp();
101
        $this->dataLoader = $this->createMock(LoaderInterface::class);
102
        $this->filterManager = $this
103
            ->getMockBuilder(FilterManager::class)
104
            ->disableOriginalConstructor()
105
            ->getMock();
106
        $this->ioResolver = $this->createMock(ResolverInterface::class);
107
        $this->filterConfiguration = new FilterConfiguration();
108
        $this->imagine = $this->createMock(ImagineInterface::class);
109
        $this->logger = $this->createMock(LoggerInterface::class);
110
        $this->box = $this->createMock(BoxInterface::class);
111
        $this->image = $this->createMock(ImageInterface::class);
112
        $this->ioService = $this->createMock(IOServiceInterface::class);
113
        $this->variationPathGenerator = $this->createMock(VariationPathGenerator::class);
114
        $this->aliasGenerator = new AliasGenerator(
115
            $this->dataLoader,
116
            $this->filterManager,
117
            $this->ioResolver,
118
            $this->filterConfiguration,
119
            $this->logger
120
        );
121
        $this->decoratedAliasGenerator = new ImagineAwareAliasGenerator(
122
            $this->aliasGenerator,
123
            $this->variationPathGenerator,
124
            $this->ioService,
125
            $this->imagine
126
        );
127
    }
128
129
    /**
130
     * @dataProvider supportsValueProvider
131
     * @param \eZ\Publish\SPI\FieldType\Value $value
132
     * @param bool $isSupported
133
     */
134
    public function testSupportsValue($value, $isSupported)
135
    {
136
        $this->assertSame($isSupported, $this->aliasGenerator->supportsValue($value));
137
    }
138
139
    /**
140
     * Data provider for testSupportsValue.
141
     *
142
     * @see testSupportsValue
143
     *
144
     * @return array
145
     *
146
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
147
     */
148
    public function supportsValueProvider()
149
    {
150
        return [
151
            [$this->createMock(FieldTypeValue::class), false],
152
            [new TextLineValue(), false],
153
            [new ImageValue(), true],
154
            [$this->createMock(ImageValue::class), true],
155
        ];
156
    }
157
158
    /**
159
     * @expectedException \InvalidArgumentException
160
     */
161
    public function testGetVariationWrongValue()
162
    {
163
        $field = new Field(array('value' => $this->getMock('eZ\Publish\Core\FieldType\Value')));
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
164
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
165
    }
166
167
    /**
168
     * Test obtaining Image Variation that hasn't been stored yet.
169
     *
170
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
171
     */
172
    public function testGetVariationNotStored()
173
    {
174
        $originalPath = 'foo/bar/image.jpg';
175
        $variationName = 'my_variation';
176
        $this->filterConfiguration->set($variationName, array());
177
        $imageId = '123-45';
178
        $imageWidth = 300;
179
        $imageHeight = 300;
180
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
181
182
        $this->ioResolver
183
            ->expects($this->once())
184
            ->method('isStored')
185
            ->with($originalPath, $variationName)
186
            ->will($this->returnValue(false));
187
188
        $this->logger
189
            ->expects($this->once())
190
            ->method('debug');
191
192
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
193
        $this->dataLoader
194
            ->expects($this->once())
195
            ->method('find')
196
            ->with($originalPath)
197
            ->will($this->returnValue($binary));
198
        $this->filterManager
199
            ->expects($this->once())
200
            ->method('applyFilter')
201
            ->with($binary, $variationName)
202
            ->will($this->returnValue($binary));
203
        $this->ioResolver
204
            ->expects($this->once())
205
            ->method('store')
206
            ->with($binary, $originalPath, $variationName);
207
208
        $this->assertImageVariationIsCorrect(
209
            $expectedUrl,
210
            $variationName,
211
            $imageId,
212
            $originalPath,
213
            $imageWidth,
214
            $imageHeight
215
        );
216
    }
217
218
    public function testGetVariationOriginal()
219
    {
220
        $originalPath = 'foo/bar/image.jpg';
221
        $variationName = 'original';
222
        $imageId = '123-45';
223
        $imageWidth = 300;
224
        $imageHeight = 300;
225
        // original images already contain proper width and height
226
        $imageValue = new ImageValue(
227
            [
228
                'id' => $originalPath,
229
                'imageId' => $imageId,
230
                'width' => $imageWidth,
231
                'height' => $imageHeight,
232
            ]
233
        );
234
        $field = new Field(['value' => $imageValue]);
235
        $expectedUrl = 'http://localhost/foo/bar/image.jpg';
236
237
        $this->ioResolver
238
            ->expects($this->never())
239
            ->method('isStored')
240
            ->with($originalPath, $variationName)
241
            ->will($this->returnValue(false));
242
243
        $this->logger
244
            ->expects($this->once())
245
            ->method('debug');
246
247
        $this->ioResolver
248
            ->expects($this->once())
249
            ->method('resolve')
250
            ->with($originalPath, $variationName)
251
            ->will($this->returnValue($expectedUrl));
252
253
        $expected = new ImageVariation(
254
            array(
255
                'name' => $variationName,
256
                'fileName' => 'image.jpg',
257
                'dirPath' => 'http://localhost/foo/bar',
258
                'uri' => $expectedUrl,
259
                'imageId' => $imageId,
260
                'height' => $imageHeight,
261
                'width' => $imageWidth,
262
            )
263
        );
264
        $this->assertEquals($expected, $this->decoratedAliasGenerator->getVariation($field, new VersionInfo(), $variationName));
265
    }
266
267
    /**
268
     * Test obtaining Image Variation that hasn't been stored yet and has multiple references.
269
     *
270
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
271
     */
272
    public function testGetVariationNotStoredHavingReferences()
273
    {
274
        $originalPath = 'foo/bar/image.jpg';
275
        $variationName = 'my_variation';
276
        $reference1 = 'reference1';
277
        $reference2 = 'reference2';
278
        $configVariation = array('reference' => $reference1);
279
        $configReference1 = array('reference' => $reference2);
280
        $configReference2 = array();
281
        $this->filterConfiguration->set($variationName, $configVariation);
282
        $this->filterConfiguration->set($reference1, $configReference1);
283
        $this->filterConfiguration->set($reference2, $configReference2);
284
        $imageId = '123-45';
285
        $imageWidth = 300;
286
        $imageHeight = 300;
287
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
288
289
        $this->ioResolver
290
            ->expects($this->once())
291
            ->method('isStored')
292
            ->with($originalPath, $variationName)
293
            ->will($this->returnValue(false));
294
295
        $this->logger
296
            ->expects($this->once())
297
            ->method('debug');
298
299
        $binary = $this->getMock('\Liip\ImagineBundle\Binary\BinaryInterface');
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\Core\Base\Tes...5CompatTrait::getMock() has been deprecated with message: Since PHPUnit 5.4, marked as deprecated here to make it clear when working on 6.7/5.4 branches
{@inheritdoc}

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
300
        $this->dataLoader
301
            ->expects($this->once())
302
            ->method('find')
303
            ->with($originalPath)
304
            ->will($this->returnValue($binary));
305
306
        // Filter manager is supposed to be called 3 times to generate references, and then passed variation.
307
        $this->filterManager
308
            ->expects($this->at(0))
309
            ->method('applyFilter')
310
            ->with($binary, $reference2)
311
            ->will($this->returnValue($binary));
312
        $this->filterManager
313
            ->expects($this->at(1))
314
            ->method('applyFilter')
315
            ->with($binary, $reference1)
316
            ->will($this->returnValue($binary));
317
        $this->filterManager
318
            ->expects($this->at(2))
319
            ->method('applyFilter')
320
            ->with($binary, $variationName)
321
            ->will($this->returnValue($binary));
322
323
        $this->ioResolver
324
            ->expects($this->once())
325
            ->method('store')
326
            ->with($binary, $originalPath, $variationName);
327
328
        $this->assertImageVariationIsCorrect(
329
            $expectedUrl,
330
            $variationName,
331
            $imageId,
332
            $originalPath,
333
            $imageWidth,
334
            $imageHeight
335
        );
336
    }
337
338
    /**
339
     * Test obtaining Image Variation that has been stored already.
340
     *
341
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
342
     */
343
    public function testGetVariationAlreadyStored()
344
    {
345
        $originalPath = 'foo/bar/image.jpg';
346
        $variationName = 'my_variation';
347
        $imageId = '123-45';
348
        $imageWidth = 300;
349
        $imageHeight = 300;
350
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
351
352
        $this->ioResolver
353
            ->expects($this->once())
354
            ->method('isStored')
355
            ->with($originalPath, $variationName)
356
            ->will($this->returnValue(true));
357
358
        $this->logger
359
            ->expects($this->once())
360
            ->method('debug');
361
362
        $this->dataLoader
363
            ->expects($this->never())
364
            ->method('find');
365
        $this->filterManager
366
            ->expects($this->never())
367
            ->method('applyFilter');
368
        $this->ioResolver
369
            ->expects($this->never())
370
            ->method('store');
371
372
        $this->assertImageVariationIsCorrect(
373
            $expectedUrl,
374
            $variationName,
375
            $imageId,
376
            $originalPath,
377
            $imageWidth,
378
            $imageHeight
379
        );
380
    }
381
382
    /**
383
     * @expectedException \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
384
     */
385
    public function testGetVariationOriginalNotFound()
386
    {
387
        $this->dataLoader
388
            ->expects($this->once())
389
            ->method('find')
390
            ->will($this->throwException(new NotLoadableException()));
391
392
        $field = new Field(array('value' => new ImageValue()));
393
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
394
    }
395
396
    /**
397
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
398
     */
399
    public function testGetVariationInvalidVariation()
400
    {
401
        $originalPath = 'foo/bar/image.jpg';
402
        $variationName = 'my_variation';
403
        $imageId = '123-45';
404
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
405
        $field = new Field(array('value' => $imageValue));
406
407
        $this->ioResolver
408
            ->expects($this->once())
409
            ->method('isStored')
410
            ->with($originalPath, $variationName)
411
            ->will($this->returnValue(true));
412
413
        $this->logger
414
            ->expects($this->once())
415
            ->method('debug');
416
417
        $this->dataLoader
418
            ->expects($this->never())
419
            ->method('find');
420
        $this->filterManager
421
            ->expects($this->never())
422
            ->method('applyFilter');
423
        $this->ioResolver
424
            ->expects($this->never())
425
            ->method('store');
426
427
        $this->ioResolver
428
            ->expects($this->once())
429
            ->method('resolve')
430
            ->with($originalPath, $variationName)
431
            ->will($this->throwException(new NotResolvableException()));
432
433
        $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName);
434
    }
435
436
    /**
437
     * Prepare required Imagine-related mocks and assert that the Image Variation is as expected.
438
     *
439
     * @param string $expectedUrl
440
     * @param string $variationName
441
     * @param string $imageId
442
     * @param string $originalPath
443
     * @param int $imageWidth
444
     * @param int $imageHeight
445
     *
446
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
447
     */
448
    protected function assertImageVariationIsCorrect(
449
        $expectedUrl,
450
        $variationName,
451
        $imageId,
452
        $originalPath,
453
        $imageWidth,
454
        $imageHeight
455
    ) {
456
        $imageValue = new ImageValue(['id' => $originalPath, 'imageId' => $imageId]);
457
        $field = new Field(['value' => $imageValue]);
458
459
        $binaryFile = new \eZ\Publish\Core\IO\Values\BinaryFile(
460
            [
461
                'uri' => "_aliases/{$variationName}/foo/bar/image.jpg",
462
            ]
463
        );
464
465
        $this->ioResolver
466
            ->expects($this->once())
467
            ->method('resolve')
468
            ->with($originalPath, $variationName)
469
            ->will($this->returnValue($expectedUrl));
470
471
        $this->variationPathGenerator
472
            ->expects($this->once())
473
            ->method('getVariationPath')
474
            ->with($originalPath, $variationName)
475
            ->willReturn($binaryFile->uri);
476
477
        $this->ioService
478
            ->expects($this->once())
479
            ->method('loadBinaryFile')
480
            ->withAnyParameters()
481
            ->willReturn($binaryFile);
482
483
        $this->ioService
484
            ->expects($this->once())
485
            ->method('getFileContents')
486
            ->with($binaryFile)
487
            ->willReturn('file contents mock');
488
489
        $this->imagine
490
            ->expects($this->once())
491
            ->method('load')
492
            ->with('file contents mock')
493
            ->will($this->returnValue($this->image));
494
        $this->image
495
            ->expects($this->once())
496
            ->method('getSize')
497
            ->will($this->returnValue($this->box));
498
499
        $this->box
500
            ->expects($this->once())
501
            ->method('getWidth')
502
            ->will($this->returnValue($imageWidth));
503
        $this->box
504
            ->expects($this->once())
505
            ->method('getHeight')
506
            ->will($this->returnValue($imageHeight));
507
508
        $expected = new ImageVariation(
509
            [
510
                'name' => $variationName,
511
                'fileName' => "image_$variationName.jpg",
512
                'dirPath' => 'http://localhost/foo/bar',
513
                'uri' => $expectedUrl,
514
                'imageId' => $imageId,
515
                'height' => $imageHeight,
516
                'width' => $imageWidth,
517
            ]
518
        );
519
        $this->assertEquals(
520
            $expected,
521
            $this->decoratedAliasGenerator->getVariation($field, new VersionInfo(), $variationName)
522
        );
523
    }
524
}
525