Completed
Push — 7.1 ( c93e68...5ccb92 )
by
unknown
29:26
created

AliasGeneratorTest::testGetVariationOriginal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
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\FieldType\Image\Value as ImageValue;
16
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
17
use eZ\Publish\SPI\FieldType\Value as FieldTypeValue;
18
use eZ\Publish\Core\IO\IOServiceInterface;
19
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
20
use eZ\Publish\SPI\Variation\Values\ImageVariation;
21
use Imagine\Image\BoxInterface;
22
use Imagine\Image\ImageInterface;
23
use Imagine\Image\ImagineInterface;
24
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
25
use Liip\ImagineBundle\Binary\BinaryInterface;
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
    /**
37
     * @var \PHPUnit\Framework\MockObject\MockObject|\Liip\ImagineBundle\Binary\Loader\LoaderInterface
38
     */
39
    private $dataLoader;
40
41
    /**
42
     * @var \PHPUnit\Framework\MockObject\MockObject|\Liip\ImagineBundle\Imagine\Filter\FilterManager
43
     */
44
    private $filterManager;
45
46
    /**
47
     * @var \PHPUnit\Framework\MockObject\MockObject|\Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface
48
     */
49
    private $ioResolver;
50
51
    /**
52
     * @var \Liip\ImagineBundle\Imagine\Filter\FilterConfiguration
53
     */
54
    private $filterConfiguration;
55
56
    /**
57
     * @var \PHPUnit\Framework\MockObject\MockObject|\Psr\Log\LoggerInterface
58
     */
59
    private $logger;
60
61
    /**
62
     * @var \PHPUnit\Framework\MockObject\MockObject|\Imagine\Image\ImagineInterface
63
     */
64
    private $imagine;
65
66
    /**
67
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator
68
     */
69
    private $aliasGenerator;
70
71
    /**
72
     * @var \eZ\Publish\SPI\Variation\VariationHandler
73
     */
74
    private $decoratedAliasGenerator;
75
76
    /**
77
     * @var \PHPUnit\Framework\MockObject\MockObject|\Imagine\Image\BoxInterface
78
     */
79
    private $box;
80
81
    /**
82
     * @var \PHPUnit\Framework\MockObject\MockObject|\Imagine\Image\ImageInterface
83
     */
84
    private $image;
85
86
    /**
87
     * @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\IO\IOServiceInterface
88
     */
89
    private $ioService;
90
91
    /**
92
     * @var \PHPUnit\Framework\MockObject\MockObject|\eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator
93
     */
94
    private $variationPathGenerator;
95
96
    protected function setUp()
97
    {
98
        parent::setUp();
99
        $this->dataLoader = $this->createMock(LoaderInterface::class);
100
        $this->filterManager = $this
101
            ->getMockBuilder(FilterManager::class)
102
            ->disableOriginalConstructor()
103
            ->getMock();
104
        $this->ioResolver = $this->createMock(ResolverInterface::class);
105
        $this->filterConfiguration = new FilterConfiguration();
106
        $this->logger = $this->createMock(LoggerInterface::class);
107
        $this->imagine = $this->createMock(ImagineInterface::class);
108
        $this->box = $this->createMock(BoxInterface::class);
109
        $this->image = $this->createMock(ImageInterface::class);
110
        $this->ioService = $this->createMock(IOServiceInterface::class);
111
        $this->variationPathGenerator = $this->createMock(VariationPathGenerator::class);
112
        $this->aliasGenerator = new AliasGenerator(
113
            $this->dataLoader,
114
            $this->filterManager,
115
            $this->ioResolver,
116
            $this->filterConfiguration,
117
            $this->logger
118
        );
119
        $this->decoratedAliasGenerator = new ImagineAwareAliasGenerator(
120
            $this->aliasGenerator,
121
            $this->variationPathGenerator,
122
            $this->ioService,
123
            $this->imagine
124
        );
125
    }
126
127
    /**
128
     * @dataProvider supportsValueProvider
129
     * @param \eZ\Publish\SPI\FieldType\Value $value
130
     * @param bool $isSupported
131
     */
132
    public function testSupportsValue($value, $isSupported)
133
    {
134
        $this->assertSame($isSupported, $this->aliasGenerator->supportsValue($value));
135
    }
136
137
    /**
138
     * Data provider for testSupportsValue.
139
     *
140
     * @see testSupportsValue
141
     *
142
     * @return array
143
     *
144
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
145
     */
146
    public function supportsValueProvider()
147
    {
148
        return [
149
            [$this->createMock(FieldTypeValue::class), false],
150
            [new TextLineValue(), false],
151
            [new ImageValue(), true],
152
            [$this->createMock(ImageValue::class), true],
153
        ];
154
    }
155
156
    /**
157
     * @expectedException \InvalidArgumentException
158
     */
159
    public function testGetVariationWrongValue()
160
    {
161
        $field = new Field(array('value' => $this->createMock(FieldTypeValue::class)));
162
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
163
    }
164
165
    /**
166
     * Test obtaining Image Variation that hasn't been stored yet.
167
     *
168
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
169
     */
170
    public function testGetVariationNotStored()
171
    {
172
        $originalPath = 'foo/bar/image.jpg';
173
        $variationName = 'my_variation';
174
        $this->filterConfiguration->set($variationName, array());
175
        $imageId = '123-45';
176
        $imageWidth = 300;
177
        $imageHeight = 300;
178
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
179
180
        $this->ioResolver
181
            ->expects($this->once())
182
            ->method('isStored')
183
            ->with($originalPath, $variationName)
184
            ->will($this->returnValue(false));
185
186
        $this->logger
187
            ->expects($this->once())
188
            ->method('debug');
189
190
        $binary = $this->createMock(BinaryInterface::class);
191
        $this->dataLoader
192
            ->expects($this->once())
193
            ->method('find')
194
            ->with($originalPath)
195
            ->will($this->returnValue($binary));
196
        $this->filterManager
197
            ->expects($this->once())
198
            ->method('applyFilter')
199
            ->with($binary, $variationName)
200
            ->will($this->returnValue($binary));
201
        $this->ioResolver
202
            ->expects($this->once())
203
            ->method('store')
204
            ->with($binary, $originalPath, $variationName);
205
206
        $this->assertImageVariationIsCorrect(
207
            $expectedUrl,
208
            $variationName,
209
            $imageId,
210
            $originalPath,
211
            $imageWidth,
212
            $imageHeight
213
        );
214
    }
215
216
    public function testGetVariationOriginal()
217
    {
218
        $originalPath = 'foo/bar/image.jpg';
219
        $variationName = 'original';
220
        $imageId = '123-45';
221
        $imageWidth = 300;
222
        $imageHeight = 300;
223
        // original images already contain proper width and height
224
        $imageValue = new ImageValue(
225
            [
226
                'id' => $originalPath,
227
                'imageId' => $imageId,
228
                'width' => $imageWidth,
229
                'height' => $imageHeight,
230
            ]
231
        );
232
        $field = new Field(['value' => $imageValue]);
233
        $expectedUrl = 'http://localhost/foo/bar/image.jpg';
234
235
        $this->ioResolver
236
            ->expects($this->never())
237
            ->method('isStored')
238
            ->with($originalPath, $variationName)
239
            ->will($this->returnValue(false));
240
241
        $this->logger
242
            ->expects($this->once())
243
            ->method('debug');
244
245
        $this->ioResolver
246
            ->expects($this->once())
247
            ->method('resolve')
248
            ->with($originalPath, $variationName)
249
            ->will($this->returnValue($expectedUrl));
250
251
        $expected = new ImageVariation(
252
            array(
253
                'name' => $variationName,
254
                'fileName' => 'image.jpg',
255
                'dirPath' => 'http://localhost/foo/bar',
256
                'uri' => $expectedUrl,
257
                'imageId' => $imageId,
258
                'height' => $imageHeight,
259
                'width' => $imageWidth,
260
            )
261
        );
262
        $this->assertEquals($expected, $this->decoratedAliasGenerator->getVariation($field, new VersionInfo(), $variationName));
263
    }
264
265
    /**
266
     * Test obtaining Image Variation that hasn't been stored yet and has multiple references.
267
     *
268
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
269
     */
270
    public function testGetVariationNotStoredHavingReferences()
271
    {
272
        $originalPath = 'foo/bar/image.jpg';
273
        $variationName = 'my_variation';
274
        $reference1 = 'reference1';
275
        $reference2 = 'reference2';
276
        $configVariation = array('reference' => $reference1);
277
        $configReference1 = array('reference' => $reference2);
278
        $configReference2 = array();
279
        $this->filterConfiguration->set($variationName, $configVariation);
280
        $this->filterConfiguration->set($reference1, $configReference1);
281
        $this->filterConfiguration->set($reference2, $configReference2);
282
        $imageId = '123-45';
283
        $imageWidth = 300;
284
        $imageHeight = 300;
285
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
286
287
        $this->ioResolver
288
            ->expects($this->once())
289
            ->method('isStored')
290
            ->with($originalPath, $variationName)
291
            ->will($this->returnValue(false));
292
293
        $this->logger
294
            ->expects($this->once())
295
            ->method('debug');
296
297
        $binary = $this->createMock(BinaryInterface::class);
298
        $this->dataLoader
299
            ->expects($this->once())
300
            ->method('find')
301
            ->with($originalPath)
302
            ->will($this->returnValue($binary));
303
304
        // Filter manager is supposed to be called 3 times to generate references, and then passed variation.
305
        $this->filterManager
306
            ->expects($this->at(0))
307
            ->method('applyFilter')
308
            ->with($binary, $reference2)
309
            ->will($this->returnValue($binary));
310
        $this->filterManager
311
            ->expects($this->at(1))
312
            ->method('applyFilter')
313
            ->with($binary, $reference1)
314
            ->will($this->returnValue($binary));
315
        $this->filterManager
316
            ->expects($this->at(2))
317
            ->method('applyFilter')
318
            ->with($binary, $variationName)
319
            ->will($this->returnValue($binary));
320
321
        $this->ioResolver
322
            ->expects($this->once())
323
            ->method('store')
324
            ->with($binary, $originalPath, $variationName);
325
326
        $this->assertImageVariationIsCorrect(
327
            $expectedUrl,
328
            $variationName,
329
            $imageId,
330
            $originalPath,
331
            $imageWidth,
332
            $imageHeight
333
        );
334
    }
335
336
    /**
337
     * Test obtaining Image Variation that has been stored already.
338
     *
339
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
340
     */
341
    public function testGetVariationAlreadyStored()
342
    {
343
        $originalPath = 'foo/bar/image.jpg';
344
        $variationName = 'my_variation';
345
        $imageId = '123-45';
346
        $imageWidth = 300;
347
        $imageHeight = 300;
348
        $expectedUrl = "http://localhost/foo/bar/image_$variationName.jpg";
349
350
        $this->ioResolver
351
            ->expects($this->once())
352
            ->method('isStored')
353
            ->with($originalPath, $variationName)
354
            ->will($this->returnValue(true));
355
356
        $this->logger
357
            ->expects($this->once())
358
            ->method('debug');
359
360
        $this->dataLoader
361
            ->expects($this->never())
362
            ->method('find');
363
        $this->filterManager
364
            ->expects($this->never())
365
            ->method('applyFilter');
366
        $this->ioResolver
367
            ->expects($this->never())
368
            ->method('store');
369
370
        $this->assertImageVariationIsCorrect(
371
            $expectedUrl,
372
            $variationName,
373
            $imageId,
374
            $originalPath,
375
            $imageWidth,
376
            $imageHeight
377
        );
378
    }
379
380
    /**
381
     * @expectedException \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
382
     */
383
    public function testGetVariationOriginalNotFound()
384
    {
385
        $this->dataLoader
386
            ->expects($this->once())
387
            ->method('find')
388
            ->will($this->throwException(new NotLoadableException()));
389
390
        $field = new Field(array('value' => new ImageValue()));
391
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
392
    }
393
394
    /**
395
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
396
     */
397
    public function testGetVariationInvalidVariation()
398
    {
399
        $originalPath = 'foo/bar/image.jpg';
400
        $variationName = 'my_variation';
401
        $imageId = '123-45';
402
        $imageValue = new ImageValue(array('id' => $originalPath, 'imageId' => $imageId));
403
        $field = new Field(array('value' => $imageValue));
404
405
        $this->ioResolver
406
            ->expects($this->once())
407
            ->method('isStored')
408
            ->with($originalPath, $variationName)
409
            ->will($this->returnValue(true));
410
411
        $this->logger
412
            ->expects($this->once())
413
            ->method('debug');
414
415
        $this->dataLoader
416
            ->expects($this->never())
417
            ->method('find');
418
        $this->filterManager
419
            ->expects($this->never())
420
            ->method('applyFilter');
421
        $this->ioResolver
422
            ->expects($this->never())
423
            ->method('store');
424
425
        $this->ioResolver
426
            ->expects($this->once())
427
            ->method('resolve')
428
            ->with($originalPath, $variationName)
429
            ->will($this->throwException(new NotResolvableException()));
430
431
        $this->aliasGenerator->getVariation($field, new VersionInfo(), $variationName);
432
    }
433
434
    /**
435
     * Prepare required Imagine-related mocks and assert that the Image Variation is as expected.
436
     *
437
     * @param string $expectedUrl
438
     * @param string $variationName
439
     * @param string $imageId
440
     * @param string $originalPath
441
     * @param int $imageWidth
442
     * @param int $imageHeight
443
     *
444
     * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
445
     */
446
    protected function assertImageVariationIsCorrect(
447
        $expectedUrl,
448
        $variationName,
449
        $imageId,
450
        $originalPath,
451
        $imageWidth,
452
        $imageHeight
453
    ) {
454
        $imageValue = new ImageValue(['id' => $originalPath, 'imageId' => $imageId]);
455
        $field = new Field(['value' => $imageValue]);
456
457
        $binaryFile = new \eZ\Publish\Core\IO\Values\BinaryFile(
458
            [
459
                'uri' => "_aliases/{$variationName}/foo/bar/image.jpg",
460
            ]
461
        );
462
463
        $this->ioResolver
464
            ->expects($this->once())
465
            ->method('resolve')
466
            ->with($originalPath, $variationName)
467
            ->will($this->returnValue($expectedUrl));
468
469
        $this->variationPathGenerator
470
            ->expects($this->once())
471
            ->method('getVariationPath')
472
            ->with($originalPath, $variationName)
473
            ->willReturn($binaryFile->uri);
474
475
        $this->ioService
476
            ->expects($this->once())
477
            ->method('loadBinaryFile')
478
            ->withAnyParameters()
479
            ->willReturn($binaryFile);
480
481
        $this->ioService
482
            ->expects($this->once())
483
            ->method('getFileContents')
484
            ->with($binaryFile)
485
            ->willReturn('file contents mock');
486
487
        $this->imagine
488
            ->expects($this->once())
489
            ->method('load')
490
            ->with('file contents mock')
491
            ->will($this->returnValue($this->image));
492
        $this->image
493
            ->expects($this->once())
494
            ->method('getSize')
495
            ->will($this->returnValue($this->box));
496
497
        $this->box
498
            ->expects($this->once())
499
            ->method('getWidth')
500
            ->will($this->returnValue($imageWidth));
501
        $this->box
502
            ->expects($this->once())
503
            ->method('getHeight')
504
            ->will($this->returnValue($imageHeight));
505
506
        $expected = new ImageVariation(
507
            [
508
                'name' => $variationName,
509
                'fileName' => "image_$variationName.jpg",
510
                'dirPath' => 'http://localhost/foo/bar',
511
                'uri' => $expectedUrl,
512
                'imageId' => $imageId,
513
                'height' => $imageHeight,
514
                'width' => $imageWidth,
515
            ]
516
        );
517
        $this->assertEquals(
518
            $expected,
519
            $this->decoratedAliasGenerator->getVariation($field, new VersionInfo(), $variationName)
520
        );
521
    }
522
}
523