|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\DependencyInjection; |
|
15
|
|
|
|
|
16
|
|
|
use Imagine\Gd\Imagine as GdImagine; |
|
17
|
|
|
use Imagine\Gmagick\Imagine as GmagicImagine; |
|
18
|
|
|
use Imagine\Imagick\Imagine as ImagicImagine; |
|
19
|
|
|
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; |
|
20
|
|
|
use Sonata\MediaBundle\DependencyInjection\SonataMediaExtension; |
|
21
|
|
|
use Sonata\MediaBundle\Model\CategoryManager; |
|
22
|
|
|
use Sonata\MediaBundle\Resizer\SimpleResizer; |
|
23
|
|
|
use Sonata\MediaBundle\Resizer\SquareResizer; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
25
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
26
|
|
|
use Twig\Extra\String\StringExtension; |
|
27
|
|
|
|
|
28
|
|
|
class SonataMediaExtensionTest extends AbstractExtensionTestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function setUp(): void |
|
34
|
|
|
{ |
|
35
|
|
|
parent::setUp(); |
|
36
|
|
|
|
|
37
|
|
|
$this->container->setParameter('kernel.bundles', ['SonataAdminBundle' => true]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testLoadWithDefaultAndCustomCategoryManager(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$this->load([ |
|
43
|
|
|
'class' => [ |
|
44
|
|
|
'category' => \stdClass::class, |
|
45
|
|
|
], |
|
46
|
|
|
'category_manager' => 'dummy.service.name', |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertContainerBuilderHasAlias('sonata.media.manager.category', 'dummy.service.name'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testLoadWithForceDisableTrueAndWithCategoryManager(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->load([ |
|
55
|
|
|
'class' => [ |
|
56
|
|
|
'category' => \stdClass::class, |
|
57
|
|
|
], |
|
58
|
|
|
'category_manager' => 'dummy.service.name', |
|
59
|
|
|
'force_disable_category' => true, |
|
60
|
|
|
]); |
|
61
|
|
|
|
|
62
|
|
|
$this->assertContainerBuilderNotHasService('sonata.media.manager.category'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testLoadWithDefaultAndClassificationBundleEnable(): void |
|
66
|
|
|
{ |
|
67
|
|
|
$this->load(); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertContainerBuilderHasAlias('sonata.media.manager.category'); |
|
70
|
|
|
$this->assertContainerBuilderHasService( |
|
71
|
|
|
'sonata.media.manager.category.default', |
|
72
|
|
|
CategoryManager::class |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testLoadWithDefaultAndClassificationBundleEnableAndForceDisableCategory(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->load([ |
|
79
|
|
|
'force_disable_category' => true, |
|
80
|
|
|
]); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertContainerBuilderNotHasService('sonata.media.manager.category'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testLoadWithDefaultAndClassificationBundleEnableAndCustomCategoryManager(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->load([ |
|
88
|
|
|
'class' => [ |
|
89
|
|
|
'category' => \stdClass::class, |
|
90
|
|
|
], |
|
91
|
|
|
'category_manager' => 'dummy.service.name', |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertContainerBuilderHasAlias('sonata.media.manager.category', 'dummy.service.name'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testDefaultAdapter(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->load(); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertContainerBuilderHasAlias('sonata.media.adapter.image.default', 'sonata.media.adapter.image.gd'); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @dataProvider dataAdapter |
|
106
|
|
|
*/ |
|
107
|
|
|
public function testAdapter(string $serviceId, string $extension, string $type): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->load(); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertContainerBuilderHasService($serviceId); |
|
112
|
|
|
if (\extension_loaded($extension)) { |
|
113
|
|
|
$this->assertInstanceOf($type, $this->container->get($serviceId)); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function dataAdapter(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return [ |
|
120
|
|
|
['sonata.media.adapter.image.gd', 'gd', GdImagine::class], |
|
121
|
|
|
['sonata.media.adapter.image.gmagick', 'gmagick', GmagicImagine::class], |
|
122
|
|
|
['sonata.media.adapter.image.imagick', 'imagick', ImagicImagine::class], |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testDefaultResizer(): void |
|
127
|
|
|
{ |
|
128
|
|
|
$this->load(); |
|
129
|
|
|
|
|
130
|
|
|
$this->assertContainerBuilderHasAlias('sonata.media.resizer.default', 'sonata.media.resizer.simple'); |
|
131
|
|
|
if (\extension_loaded('gd')) { |
|
132
|
|
|
$this->assertContainerBuilderHasService( |
|
133
|
|
|
'sonata.media.resizer.default', |
|
134
|
|
|
SimpleResizer::class |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @dataProvider dataResizer |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testResizer(string $serviceId, string $type): void |
|
143
|
|
|
{ |
|
144
|
|
|
$this->load(); |
|
145
|
|
|
|
|
146
|
|
|
$this->assertContainerBuilderHasService($serviceId); |
|
147
|
|
|
if (\extension_loaded('gd')) { |
|
148
|
|
|
$this->assertInstanceOf($type, $this->container->get($serviceId)); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function dataResizer(): array |
|
153
|
|
|
{ |
|
154
|
|
|
return [ |
|
155
|
|
|
['sonata.media.resizer.simple', SimpleResizer::class], |
|
156
|
|
|
['sonata.media.resizer.square', SquareResizer::class], |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testLoadWithSonataAdminDefaults(): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->load(); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertSame( |
|
165
|
|
|
$this->container->getDefinition('sonata.media.security.superadmin_strategy')->getArgument(2), |
|
166
|
|
|
['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'] |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testLoadWithSonataAdminCustomConfiguration(): void |
|
171
|
|
|
{ |
|
172
|
|
|
$fakeContainer = $this->createMock(ContainerBuilder::class); |
|
173
|
|
|
|
|
174
|
|
|
$fakeContainer->expects($this->once()) |
|
175
|
|
|
->method('getParameter') |
|
176
|
|
|
->with($this->equalTo('kernel.bundles')) |
|
177
|
|
|
->willReturn($this->container->getParameter('kernel.bundles')); |
|
178
|
|
|
|
|
179
|
|
|
$fakeContainer->expects($this->once()) |
|
180
|
|
|
->method('getExtensionConfig') |
|
181
|
|
|
->with($this->equalTo('sonata_admin')) |
|
182
|
|
|
->willReturn([[ |
|
183
|
|
|
'security' => [ |
|
184
|
|
|
'role_admin' => 'ROLE_FOO', |
|
185
|
|
|
'role_super_admin' => 'ROLE_BAR', |
|
186
|
|
|
], |
|
187
|
|
|
]]); |
|
188
|
|
|
|
|
189
|
|
|
$configs = [$this->getMinimalConfiguration()]; |
|
190
|
|
|
foreach ($this->getContainerExtensions() as $extension) { |
|
191
|
|
|
if ($extension instanceof PrependExtensionInterface) { |
|
192
|
|
|
$extension->prepend($fakeContainer); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$extension->load($configs, $this->container); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->assertSame( |
|
199
|
|
|
$this->container->getDefinition('sonata.media.security.superadmin_strategy')->getArgument(2), |
|
200
|
|
|
['ROLE_FOO', 'ROLE_BAR'] |
|
201
|
|
|
); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @dataProvider dataFilesystemConfiguration |
|
206
|
|
|
*/ |
|
207
|
|
|
public function testLoadWithFilesystemConfiguration(array $configs, array $args): void |
|
208
|
|
|
{ |
|
209
|
|
|
$this->load($configs); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertSame( |
|
212
|
|
|
$this->container->getDefinition('sonata.media.adapter.service.s3')->getArgument(0), |
|
213
|
|
|
$args |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function dataFilesystemConfiguration(): array |
|
218
|
|
|
{ |
|
219
|
|
|
return [ |
|
220
|
|
|
[ |
|
221
|
|
|
[ |
|
222
|
|
|
'filesystem' => [ |
|
223
|
|
|
's3' => [ |
|
224
|
|
|
'bucket' => 'bucket_name', |
|
225
|
|
|
'sdk_version' => 3, |
|
226
|
|
|
'region' => 'region', |
|
227
|
|
|
'version' => 'version', |
|
228
|
|
|
'secretKey' => null, |
|
229
|
|
|
'accessKey' => null, |
|
230
|
|
|
], |
|
231
|
|
|
], |
|
232
|
|
|
], |
|
233
|
|
|
[ |
|
234
|
|
|
'region' => 'region', |
|
235
|
|
|
'version' => 'version', |
|
236
|
|
|
], |
|
237
|
|
|
], |
|
238
|
|
|
[ |
|
239
|
|
|
[ |
|
240
|
|
|
'filesystem' => [ |
|
241
|
|
|
's3' => [ |
|
242
|
|
|
'bucket' => 'bucket_name', |
|
243
|
|
|
'sdk_version' => 3, |
|
244
|
|
|
'region' => 'region', |
|
245
|
|
|
'version' => 'version', |
|
246
|
|
|
'secretKey' => 'secret', |
|
247
|
|
|
'accessKey' => 'access', |
|
248
|
|
|
], |
|
249
|
|
|
], |
|
250
|
|
|
], |
|
251
|
|
|
[ |
|
252
|
|
|
'region' => 'region', |
|
253
|
|
|
'version' => 'version', |
|
254
|
|
|
'credentials' => [ |
|
255
|
|
|
'secret' => 'secret', |
|
256
|
|
|
'key' => 'access', |
|
257
|
|
|
], |
|
258
|
|
|
], |
|
259
|
|
|
], |
|
260
|
|
|
]; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
public function testMediaPool(): void |
|
264
|
|
|
{ |
|
265
|
|
|
$this->load(); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertContainerBuilderHasService('sonata.media.pool'); |
|
268
|
|
|
$this->assertContainerBuilderHasAlias('%sonata.media.pool.class%', 'sonata.media.pool'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function testLoadTwigStringExtension(): void |
|
272
|
|
|
{ |
|
273
|
|
|
$this->load(); |
|
274
|
|
|
|
|
275
|
|
|
$this->assertContainerBuilderHasServiceDefinitionWithTag(StringExtension::class, 'twig.extension'); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
protected function getMinimalConfiguration(): array |
|
279
|
|
|
{ |
|
280
|
|
|
return [ |
|
281
|
|
|
'default_context' => 'default', |
|
282
|
|
|
'db_driver' => 'doctrine_orm', |
|
283
|
|
|
'contexts' => [ |
|
284
|
|
|
'default' => [ |
|
285
|
|
|
'formats' => [ |
|
286
|
|
|
'small' => [ |
|
287
|
|
|
'width' => 100, |
|
288
|
|
|
'quality' => 50, |
|
289
|
|
|
], |
|
290
|
|
|
], |
|
291
|
|
|
], |
|
292
|
|
|
], |
|
293
|
|
|
'filesystem' => [ |
|
294
|
|
|
'local' => [ |
|
295
|
|
|
'directory' => '/tmp/', |
|
296
|
|
|
], |
|
297
|
|
|
], |
|
298
|
|
|
]; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* {@inheritdoc} |
|
303
|
|
|
*/ |
|
304
|
|
|
protected function getContainerExtensions(): array |
|
305
|
|
|
{ |
|
306
|
|
|
return [ |
|
307
|
|
|
new SonataMediaExtension(), |
|
308
|
|
|
]; |
|
309
|
|
|
} |
|
310
|
|
|
} |
|
311
|
|
|
|