1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Tests\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory; |
6
|
|
|
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory; |
7
|
|
|
use Liip\ImagineBundle\Tests\AbstractTest; |
8
|
|
|
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
use Symfony\Component\Yaml\Parser; |
13
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @covers Liip\ImagineBundle\DependencyInjection\Configuration |
17
|
|
|
* @covers Liip\ImagineBundle\DependencyInjection\LiipImagineExtension |
18
|
|
|
*/ |
19
|
|
|
class LiipImagineExtensionTest extends AbstractTest |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerBuilder |
23
|
|
|
*/ |
24
|
|
|
protected $containerBuilder; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
28
|
|
|
*/ |
29
|
|
|
public function testUserLoadThrowsExceptionUnlessDriverIsValid() |
30
|
|
|
{ |
31
|
|
|
$loader = new LiipImagineExtension(); |
32
|
|
|
$config = array('driver' => 'foo'); |
33
|
|
|
$loader->load(array($config), new ContainerBuilder()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testLoadWithDefaults() |
37
|
|
|
{ |
38
|
|
|
$this->createEmptyConfiguration(); |
39
|
|
|
|
40
|
|
|
$this->assertParameter('default', 'liip_imagine.cache.resolver.default'); |
41
|
|
|
$this->assertAlias('liip_imagine.gd', 'liip_imagine'); |
42
|
|
|
$this->assertHasDefinition('liip_imagine.controller'); |
43
|
|
|
$this->assertDICConstructorArguments( |
44
|
|
|
$this->containerBuilder->getDefinition('liip_imagine.controller'), |
45
|
|
|
array( |
46
|
|
|
new Reference('liip_imagine.data.manager'), |
47
|
|
|
new Reference('liip_imagine.filter.manager'), |
48
|
|
|
new Reference('liip_imagine.cache.manager'), |
49
|
|
|
new Reference('liip_imagine.cache.signer'), |
50
|
|
|
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE), |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testCustomRouteRequirements() |
56
|
|
|
{ |
57
|
|
|
$this->createFullConfiguration(); |
58
|
|
|
$param = $this->containerBuilder->getParameter('liip_imagine.filter_sets'); |
59
|
|
|
|
60
|
|
|
$this->assertTrue(isset($param['small']['filters']['route']['requirements'])); |
61
|
|
|
|
62
|
|
|
$variable1 = $param['small']['filters']['route']['requirements']['variable1']; |
63
|
|
|
$this->assertEquals('value1', $variable1, sprintf('%s parameter is correct', $variable1)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @dataProvider factoriesProvider |
68
|
|
|
*/ |
69
|
|
|
public function testFactoriesConfiguration($service, $factory) |
70
|
|
|
{ |
71
|
|
|
if (version_compare(Kernel::VERSION_ID, '20600') < 0) { |
72
|
|
|
$this->markTestSkipped('No need to test on symfony < 2.6'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->createEmptyConfiguration(); |
76
|
|
|
$definition = $this->containerBuilder->getDefinition($service); |
77
|
|
|
|
78
|
|
|
$this->assertEquals($factory, $definition->getFactory()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider factoriesProvider |
83
|
|
|
*/ |
84
|
|
|
public function testLegacyFactoriesConfiguration($service, $factory) |
85
|
|
|
{ |
86
|
|
|
if (version_compare(Kernel::VERSION_ID, '20600') >= 0) { |
87
|
|
|
$this->markTestSkipped('No need to test on symfony >= 2.6'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->createEmptyConfiguration(); |
91
|
|
|
$definition = $this->containerBuilder->getDefinition($service); |
92
|
|
|
|
93
|
|
|
$this->assertEquals($factory[0], $definition->getFactoryClass()); |
94
|
|
|
$this->assertEquals($factory[1], $definition->getFactoryMethod()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function factoriesProvider() |
98
|
|
|
{ |
99
|
|
|
return array( |
100
|
|
|
array( |
101
|
|
|
'liip_imagine.mime_type_guesser', |
102
|
|
|
array('Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser', 'getInstance'), |
103
|
|
|
), |
104
|
|
|
array( |
105
|
|
|
'liip_imagine.extension_guesser', |
106
|
|
|
array('Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser', 'getInstance'), |
107
|
|
|
), |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return ContainerBuilder |
113
|
|
|
*/ |
114
|
|
|
protected function createEmptyConfiguration() |
115
|
|
|
{ |
116
|
|
|
$this->containerBuilder = new ContainerBuilder(); |
117
|
|
|
$loader = new LiipImagineExtension(); |
118
|
|
|
$loader->addLoaderFactory(new FileSystemLoaderFactory()); |
119
|
|
|
$loader->addResolverFactory(new WebPathResolverFactory()); |
120
|
|
|
$loader->load(array(array()), $this->containerBuilder); |
121
|
|
|
$this->assertTrue($this->containerBuilder instanceof ContainerBuilder); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ContainerBuilder |
126
|
|
|
*/ |
127
|
|
|
protected function createFullConfiguration() |
128
|
|
|
{ |
129
|
|
|
$this->containerBuilder = new ContainerBuilder(); |
130
|
|
|
$loader = new LiipImagineExtension(); |
131
|
|
|
$loader->addLoaderFactory(new FileSystemLoaderFactory()); |
132
|
|
|
$loader->addResolverFactory(new WebPathResolverFactory()); |
133
|
|
|
$loader->load(array($this->getFullConfig()), $this->containerBuilder); |
134
|
|
|
$this->assertTrue($this->containerBuilder instanceof ContainerBuilder); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function getFullConfig() |
138
|
|
|
{ |
139
|
|
|
$yaml = <<<'EOF' |
140
|
|
|
driver: imagick |
141
|
|
|
cache: false |
142
|
|
|
filter_sets: |
143
|
|
|
small: |
144
|
|
|
filters: |
145
|
|
|
thumbnail: { size: [100, ~], mode: inset } |
146
|
|
|
route: |
147
|
|
|
requirements: { variable1: 'value1' } |
148
|
|
|
quality: 80 |
149
|
|
|
medium_small_cropped: |
150
|
|
|
filters: |
151
|
|
|
thumbnail: { size: [223, 173], mode: outbound } |
152
|
|
|
medium_cropped: |
153
|
|
|
filters: |
154
|
|
|
thumbnail: { size: [232, 180], mode: outbound } |
155
|
|
|
medium: |
156
|
|
|
filters: |
157
|
|
|
thumbnail: { size: [232, 180], mode: inset } |
158
|
|
|
large_cropped: |
159
|
|
|
filters: |
160
|
|
|
thumbnail: { size: [483, 350], mode: outbound } |
161
|
|
|
large: |
162
|
|
|
filters: |
163
|
|
|
thumbnail: { size: [483, ~], mode: inset } |
164
|
|
|
xxl: |
165
|
|
|
filters: |
166
|
|
|
thumbnail: { size: [660, ~], mode: inset } |
167
|
|
|
quality: 100 |
168
|
|
|
'': |
169
|
|
|
quality: 100 |
170
|
|
|
data_loader: my_loader |
171
|
|
|
EOF; |
172
|
|
|
$parser = new Parser(); |
173
|
|
|
|
174
|
|
|
return $parser->parse($yaml); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function assertAlias($value, $key) |
178
|
|
|
{ |
179
|
|
|
$this->assertEquals($value, (string) $this->containerBuilder->getAlias($key), sprintf('%s alias is correct', $key)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function assertParameter($value, $key) |
183
|
|
|
{ |
184
|
|
|
$this->assertEquals($value, $this->containerBuilder->getParameter($key), sprintf('%s parameter is correct', $key)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function assertHasDefinition($id) |
188
|
|
|
{ |
189
|
|
|
$this->assertTrue(($this->containerBuilder->hasDefinition($id) ?: $this->containerBuilder->hasAlias($id))); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function assertNotHasDefinition($id) |
193
|
|
|
{ |
194
|
|
|
$this->assertFalse(($this->containerBuilder->hasDefinition($id) ?: $this->containerBuilder->hasAlias($id))); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function assertDICConstructorArguments($definition, $args) |
198
|
|
|
{ |
199
|
|
|
$this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match."); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
protected function tearDown() |
203
|
|
|
{ |
204
|
|
|
unset($this->containerBuilder); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|