Completed
Push — master ( 513898...595f7b )
by Łukasz
29:17
created

getVariationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\IORepositoryResolver;
12
use eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderAliasGenerator;
13
use eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider;
14
use eZ\Publish\API\Repository\Values\Content\Field;
15
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
16
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
17
use eZ\Publish\Core\FieldType\Null\Value as NullValue;
18
use eZ\Publish\Core\FieldType\Value as FieldTypeValue;
19
use eZ\Publish\Core\FieldType\Value;
20
use eZ\Publish\Core\IO\IOServiceInterface;
21
use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct;
22
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
23
use eZ\Publish\SPI\Variation\Values\ImageVariation;
24
use eZ\Publish\SPI\Variation\VariationHandler;
25
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
26
use PHPUnit\Framework\TestCase;
27
28
class PlaceholderAliasGeneratorTest extends TestCase
29
{
30
    /**
31
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderAliasGenerator
32
     */
33
    private $aliasGenerator;
34
35
    /**
36
     * @var \eZ\Publish\SPI\Variation\VariationHandler|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $innerAliasGenerator;
39
40
    /**
41
     * @var \eZ\Publish\Core\IO\IOServiceInterface|\PHPUnit_Framework_MockObject_MockObject
42
     */
43
    private $ioService;
44
45
    /**
46
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\IORepositoryResolver|\PHPUnit_Framework_MockObject_MockObject
47
     */
48
    private $ioResolver;
49
50
    /**
51
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider|\PHPUnit_Framework_MockObject_MockObject
52
     */
53
    private $placeholderProvider;
54
55
    /**
56
     * @var array
57
     */
58
    private $placeholderOptions;
59
60
    protected function setUp()
61
    {
62
        $this->innerAliasGenerator = $this->createMock(VariationHandler::class);
63
        $this->ioService = $this->createMock(IOServiceInterface::class);
64
        $this->ioResolver = $this->createMock(IORepositoryResolver::class);
65
        $this->placeholderProvider = $this->createMock(PlaceholderProvider::class);
66
        $this->placeholderOptions = [
67
            'foo' => 'foo',
68
            'bar' => 'bar',
69
        ];
70
71
        $this->aliasGenerator = new PlaceholderAliasGenerator(
72
            $this->innerAliasGenerator,
73
            $this->ioResolver,
74
            $this->ioService
75
        );
76
    }
77
78
    /**
79
     * @expectedException \InvalidArgumentException
80
     */
81
    public function testGetVariationWrongValue()
82
    {
83
        $field = new Field([
84
            'value' => $this->createMock(FieldTypeValue::class),
85
        ]);
86
87
        $this->aliasGenerator->setPlaceholderProvider(
88
            $this->placeholderProvider,
89
            $this->placeholderOptions
90
        );
91
        $this->aliasGenerator->getVariation($field, new VersionInfo(), 'foo');
92
    }
93
94
    /**
95
     * @dataProvider getVariationProvider
96
     */
97
    public function testGetVariationSkipsPlaceholderGeneration(Field $field, APIVersionInfo $versionInfo, string $variationName, array $parameters)
98
    {
99
        $expectedVariation = $this->createMock(ImageVariation::class);
100
101
        $this->ioResolver
102
            ->expects($this->never())
103
            ->method('resolve')
104
            ->with($field->value->id, IORepositoryResolver::VARIATION_ORIGINAL);
105
106
        $this->placeholderProvider
107
            ->expects($this->never())
108
            ->method('getPlaceholder')
109
            ->with($field->value, $this->placeholderOptions);
110
111
        $this->innerAliasGenerator
112
            ->expects($this->once())
113
            ->method('getVariation')
114
            ->with($field, $versionInfo, $variationName, $parameters)
115
            ->willReturn($expectedVariation);
116
117
        $actualVariation = $this->aliasGenerator->getVariation(
118
            $field, $versionInfo, $variationName, $parameters
119
        );
120
121
        $this->assertEquals($expectedVariation, $actualVariation);
122
    }
123
124
    /**
125
     * @dataProvider getVariationProvider
126
     */
127
    public function testGetVariationOriginalFound(Field $field, APIVersionInfo $versionInfo, string $variationName, array $parameters)
128
    {
129
        $expectedVariation = $this->createMock(ImageVariation::class);
130
131
        $this->ioResolver
132
            ->expects($this->once())
133
            ->method('resolve')
134
            ->with($field->value->id, IORepositoryResolver::VARIATION_ORIGINAL);
135
136
        $this->innerAliasGenerator
137
            ->expects($this->once())
138
            ->method('getVariation')
139
            ->with($field, $versionInfo, $variationName, $parameters)
140
            ->willReturn($expectedVariation);
141
142
        $this->aliasGenerator->setPlaceholderProvider(
143
            $this->placeholderProvider,
144
            $this->placeholderOptions
145
        );
146
147
        $actualVariation = $this->aliasGenerator->getVariation(
148
            $field, $versionInfo, $variationName, $parameters
149
        );
150
151
        $this->assertEquals($expectedVariation, $actualVariation);
152
    }
153
154
    /**
155
     * @dataProvider getVariationProvider
156
     */
157
    public function testGetVariationOriginalNotFound(Field $field, APIVersionInfo $versionInfo, string $variationName, array $parameters)
158
    {
159
        $placeholderPath = '/tmp/placeholder.jpg';
160
        $binaryCreateStruct = new BinaryFileCreateStruct();
161
        $expectedVariation = $this->createMock(ImageVariation::class);
162
163
        $this->ioResolver
164
            ->expects($this->once())
165
            ->method('resolve')
166
            ->with($field->value->id, IORepositoryResolver::VARIATION_ORIGINAL)
167
            ->willThrowException($this->createMock(NotResolvableException::class));
168
169
        $this->placeholderProvider
170
            ->expects($this->once())
171
            ->method('getPlaceholder')
172
            ->with($field->value, $this->placeholderOptions)
173
            ->willReturn($placeholderPath);
174
175
        $this->ioService
176
            ->expects($this->once())
177
            ->method('newBinaryCreateStructFromLocalFile')
178
            ->with($placeholderPath)
179
            ->willReturn($binaryCreateStruct);
180
181
        $this->ioService
182
            ->expects($this->once())
183
            ->method('createBinaryFile')
184
            ->with($binaryCreateStruct);
185
186
        $this->aliasGenerator->setPlaceholderProvider(
187
            $this->placeholderProvider,
188
            $this->placeholderOptions
189
        );
190
191
        $this->innerAliasGenerator
192
            ->expects($this->once())
193
            ->method('getVariation')
194
            ->with($field, $versionInfo, $variationName, $parameters)
195
            ->willReturn($expectedVariation);
196
197
        $actualVariation = $this->aliasGenerator->getVariation(
198
            $field, $versionInfo, $variationName, $parameters
199
        );
200
201
        $this->assertEquals($field->value->id, $binaryCreateStruct->id);
202
        $this->assertEquals($expectedVariation, $actualVariation);
203
    }
204
205
    /**
206
     * @dataProvider supportsValueProvider
207
     */
208
    public function testSupportsValue(Value $value, bool $isSupported)
209
    {
210
        $this->assertSame($isSupported, $this->aliasGenerator->supportsValue($value));
211
    }
212
213
    public function supportsValueProvider(): array
214
    {
215
        return [
216
            [new NullValue(), false],
217
            [new ImageValue(), true],
218
        ];
219
    }
220
221
    public function getVariationProvider(): array
222
    {
223
        $field = new Field([
224
            'value' => new ImageValue([
225
                'id' => 'images/6/8/4/0/486-10-eng-GB/photo.jpg',
226
            ]),
227
        ]);
228
229
        return [
230
            [$field, new VersionInfo(), 'thumbnail', []],
231
        ];
232
    }
233
}
234