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