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\AdminBundle\Tests\Command; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
19
|
|
|
use Sonata\AdminBundle\Admin\FieldDescriptionInterface; |
20
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
21
|
|
|
use Sonata\AdminBundle\Builder\DatagridBuilderInterface; |
22
|
|
|
use Sonata\AdminBundle\Builder\ListBuilderInterface; |
23
|
|
|
use Sonata\AdminBundle\Command\ExplainAdminCommand; |
24
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
25
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
26
|
|
|
use Sonata\AdminBundle\Route\RouteCollection; |
27
|
|
|
use Symfony\Component\Console\Application; |
28
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
29
|
|
|
use Symfony\Component\DependencyInjection\Container; |
30
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
31
|
|
|
use Symfony\Component\Validator\Constraints\Email; |
32
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
33
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
34
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata; |
35
|
|
|
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface; |
36
|
|
|
use Symfony\Component\Validator\Mapping\GenericMetadata; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @author Andrej Hudec <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
class ExplainAdminCommandTest extends TestCase |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var Application |
45
|
|
|
*/ |
46
|
|
|
private $application; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var AdminInterface |
50
|
|
|
*/ |
51
|
|
|
private $admin; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var MetadataFactoryInterface |
55
|
|
|
*/ |
56
|
|
|
private $validatorFactory; |
57
|
|
|
|
58
|
|
|
protected function setUp(): void |
59
|
|
|
{ |
60
|
|
|
$this->application = new Application(); |
61
|
|
|
|
62
|
|
|
$container = new Container(); |
63
|
|
|
|
64
|
|
|
$this->admin = $this->createMock(AdminInterface::class); |
65
|
|
|
|
66
|
|
|
$this->admin |
67
|
|
|
->method('getCode') |
68
|
|
|
->willReturn('foo'); |
69
|
|
|
|
70
|
|
|
$this->admin |
71
|
|
|
->method('getClass') |
72
|
|
|
->willReturn('Acme\Entity\Foo'); |
73
|
|
|
|
74
|
|
|
$this->admin |
75
|
|
|
->method('getBaseControllerName') |
76
|
|
|
->willReturn(CRUDController::class); |
77
|
|
|
|
78
|
|
|
$routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', CRUDController::class); |
79
|
|
|
$routeCollection->add('list'); |
80
|
|
|
$routeCollection->add('edit'); |
81
|
|
|
|
82
|
|
|
$this->admin |
83
|
|
|
->method('getRoutes') |
84
|
|
|
->willReturn($routeCollection); |
85
|
|
|
|
86
|
|
|
$fieldDescription1 = $this->createMock(FieldDescriptionInterface::class); |
87
|
|
|
|
88
|
|
|
$fieldDescription1 |
89
|
|
|
->method('getType') |
90
|
|
|
->willReturn('text'); |
91
|
|
|
|
92
|
|
|
$fieldDescription1 |
93
|
|
|
->method('getTemplate') |
94
|
|
|
->willReturn('@SonataAdmin/CRUD/foo_text.html.twig'); |
95
|
|
|
|
96
|
|
|
$fieldDescription2 = $this->createMock(FieldDescriptionInterface::class); |
97
|
|
|
|
98
|
|
|
$fieldDescription2 |
99
|
|
|
->method('getType') |
100
|
|
|
->willReturn('datetime'); |
101
|
|
|
|
102
|
|
|
$fieldDescription2 |
103
|
|
|
->method('getTemplate') |
104
|
|
|
->willReturn('@SonataAdmin/CRUD/bar_datetime.html.twig'); |
105
|
|
|
|
106
|
|
|
$this->admin |
107
|
|
|
->method('getListFieldDescriptions') |
108
|
|
|
->willReturn([ |
109
|
|
|
'fooTextField' => $fieldDescription1, |
110
|
|
|
'barDateTimeField' => $fieldDescription2, |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$this->admin |
114
|
|
|
->method('getFilterFieldDescriptions') |
115
|
|
|
->willReturn([ |
116
|
|
|
'fooTextField' => $fieldDescription1, |
117
|
|
|
'barDateTimeField' => $fieldDescription2, |
118
|
|
|
]); |
119
|
|
|
|
120
|
|
|
$this->admin |
121
|
|
|
->method('getFormTheme') |
122
|
|
|
->willReturn(['@Foo/bar.html.twig']); |
123
|
|
|
|
124
|
|
|
$this->admin |
125
|
|
|
->method('getFormFieldDescriptions') |
126
|
|
|
->willReturn([ |
127
|
|
|
'fooTextField' => $fieldDescription1, |
128
|
|
|
'barDateTimeField' => $fieldDescription2, |
129
|
|
|
]); |
130
|
|
|
|
131
|
|
|
$this->admin |
132
|
|
|
->method('isChild') |
133
|
|
|
->willReturn(true); |
134
|
|
|
|
135
|
|
|
$this->admin |
136
|
|
|
->method('getParent') |
137
|
|
|
->willReturnCallback(function () { |
138
|
|
|
$adminParent = $this->createMock(AdminInterface::class); |
139
|
|
|
|
140
|
|
|
$adminParent |
141
|
|
|
->method('getCode') |
142
|
|
|
->willReturn('foo_child'); |
143
|
|
|
|
144
|
|
|
return $adminParent; |
145
|
|
|
}); |
146
|
|
|
|
147
|
|
|
$container->set('acme.admin.foo', $this->admin); |
148
|
|
|
|
149
|
|
|
$pool = new Pool($container, '', ''); |
150
|
|
|
$pool->setAdminServiceIds(['acme.admin.foo', 'acme.admin.bar']); |
151
|
|
|
|
152
|
|
|
$this->validatorFactory = $this->createMock(MetadataFactoryInterface::class); |
153
|
|
|
|
154
|
|
|
$command = new ExplainAdminCommand($pool, $this->validatorFactory); |
155
|
|
|
|
156
|
|
|
$this->application->add($command); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testExecute(): void |
160
|
|
|
{ |
161
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
162
|
|
|
|
163
|
|
|
$this->validatorFactory->expects($this->once()) |
|
|
|
|
164
|
|
|
->method('getMetadataFor') |
165
|
|
|
->with($this->equalTo('Acme\Entity\Foo')) |
166
|
|
|
->willReturn($metadata); |
167
|
|
|
|
168
|
|
|
$propertyMetadata = $this->getMockForAbstractClass(GenericMetadata::class); |
169
|
|
|
$propertyMetadata->constraints = [ |
170
|
|
|
new NotNull(), |
171
|
|
|
new Length(['min' => 2, 'max' => 50, 'groups' => ['create', 'edit']]), |
172
|
|
|
]; |
173
|
|
|
|
174
|
|
|
$metadata->properties = ['firstName' => $propertyMetadata]; |
175
|
|
|
|
176
|
|
|
$getterMetadata = $this->getMockForAbstractClass(GenericMetadata::class); |
177
|
|
|
$getterMetadata->constraints = [ |
178
|
|
|
new NotNull(), |
179
|
|
|
new Email(['groups' => ['registration', 'edit']]), |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
$metadata->getters = ['email' => $getterMetadata]; |
183
|
|
|
|
184
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
185
|
|
|
|
186
|
|
|
$this->admin |
|
|
|
|
187
|
|
|
->method('getModelManager') |
188
|
|
|
->willReturn($modelManager); |
189
|
|
|
|
190
|
|
|
$formBuilder = $this->createMock(FormBuilderInterface::class); |
191
|
|
|
|
192
|
|
|
$this->admin |
|
|
|
|
193
|
|
|
->method('getFormBuilder') |
194
|
|
|
->willReturn($formBuilder); |
195
|
|
|
|
196
|
|
|
$datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
197
|
|
|
|
198
|
|
|
$this->admin |
|
|
|
|
199
|
|
|
->method('getDatagridBuilder') |
200
|
|
|
->willReturn($datagridBuilder); |
201
|
|
|
|
202
|
|
|
$listBuilder = $this->createMock(ListBuilderInterface::class); |
203
|
|
|
|
204
|
|
|
$this->admin |
|
|
|
|
205
|
|
|
->method('getListBuilder') |
206
|
|
|
->willReturn($listBuilder); |
207
|
|
|
|
208
|
|
|
$command = $this->application->find('sonata:admin:explain'); |
209
|
|
|
$commandTester = new CommandTester($command); |
210
|
|
|
$commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']); |
211
|
|
|
|
212
|
|
|
$this->assertSame(sprintf( |
213
|
|
|
str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')), |
214
|
|
|
\get_class($this->admin), |
215
|
|
|
\get_class($modelManager), |
216
|
|
|
\get_class($formBuilder), |
217
|
|
|
\get_class($datagridBuilder), |
218
|
|
|
\get_class($listBuilder) |
219
|
|
|
), $commandTester->getDisplay()); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function testExecuteEmptyValidator(): void |
223
|
|
|
{ |
224
|
|
|
$metadata = $this->createMock(ClassMetadata::class); |
225
|
|
|
|
226
|
|
|
$this->validatorFactory->expects($this->once()) |
|
|
|
|
227
|
|
|
->method('getMetadataFor') |
228
|
|
|
->with($this->equalTo('Acme\Entity\Foo')) |
229
|
|
|
->willReturn($metadata); |
230
|
|
|
|
231
|
|
|
$metadata->properties = []; |
232
|
|
|
$metadata->getters = []; |
233
|
|
|
|
234
|
|
|
$modelManager = $this->createMock(ModelManagerInterface::class); |
235
|
|
|
|
236
|
|
|
$this->admin |
|
|
|
|
237
|
|
|
->method('getModelManager') |
238
|
|
|
->willReturn($modelManager); |
239
|
|
|
|
240
|
|
|
$formBuilder = $this->createMock(FormBuilderInterface::class); |
241
|
|
|
|
242
|
|
|
$this->admin |
|
|
|
|
243
|
|
|
->method('getFormBuilder') |
244
|
|
|
->willReturn($formBuilder); |
245
|
|
|
|
246
|
|
|
$datagridBuilder = $this->createMock(DatagridBuilderInterface::class); |
247
|
|
|
|
248
|
|
|
$this->admin |
|
|
|
|
249
|
|
|
->method('getDatagridBuilder') |
250
|
|
|
->willReturn($datagridBuilder); |
251
|
|
|
|
252
|
|
|
$listBuilder = $this->createMock(ListBuilderInterface::class); |
253
|
|
|
|
254
|
|
|
$this->admin |
|
|
|
|
255
|
|
|
->method('getListBuilder') |
256
|
|
|
->willReturn($listBuilder); |
257
|
|
|
|
258
|
|
|
$command = $this->application->find('sonata:admin:explain'); |
259
|
|
|
$commandTester = new CommandTester($command); |
260
|
|
|
$commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']); |
261
|
|
|
|
262
|
|
|
$this->assertSame(sprintf( |
263
|
|
|
str_replace( |
264
|
|
|
"\n", |
265
|
|
|
PHP_EOL, |
266
|
|
|
file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt') |
267
|
|
|
), |
268
|
|
|
\get_class($this->admin), |
269
|
|
|
\get_class($modelManager), |
270
|
|
|
\get_class($formBuilder), |
271
|
|
|
\get_class($datagridBuilder), |
272
|
|
|
\get_class($listBuilder) |
273
|
|
|
), $commandTester->getDisplay()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function testExecuteNonAdminService(): void |
277
|
|
|
{ |
278
|
|
|
$command = $this->application->find('sonata:admin:explain'); |
279
|
|
|
$commandTester = new CommandTester($command); |
280
|
|
|
|
281
|
|
|
$this->expectException(InvalidArgumentException::class); |
282
|
|
|
$this->expectExceptionMessage('Admin service "nonexistent.service" not found in admin pool. Did you mean "acme.admin.bar" or one of those: []'); |
283
|
|
|
|
284
|
|
|
$commandTester->execute(['command' => $command->getName(), 'admin' => 'nonexistent.service']); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
public function testExecuteWithNonClassMetadata(): void |
288
|
|
|
{ |
289
|
|
|
$metadata = $this->createStub(GenericMetadata::class); |
290
|
|
|
|
291
|
|
|
$this->validatorFactory->expects($this->once()) |
|
|
|
|
292
|
|
|
->method('getMetadataFor') |
293
|
|
|
->with($this->equalTo('Acme\Entity\Foo')) |
294
|
|
|
->willReturn($metadata); |
295
|
|
|
|
296
|
|
|
$metadata->properties = []; |
297
|
|
|
$metadata->getters = []; |
298
|
|
|
|
299
|
|
|
$modelManager = $this->createStub(ModelManagerInterface::class); |
300
|
|
|
|
301
|
|
|
$this->admin |
|
|
|
|
302
|
|
|
->method('getModelManager') |
303
|
|
|
->willReturn($modelManager); |
304
|
|
|
|
305
|
|
|
$formBuilder = $this->createStub(FormBuilderInterface::class); |
306
|
|
|
|
307
|
|
|
$this->admin |
|
|
|
|
308
|
|
|
->method('getFormBuilder') |
309
|
|
|
->willReturn($formBuilder); |
310
|
|
|
|
311
|
|
|
$datagridBuilder = $this->createStub(DatagridBuilderInterface::class); |
312
|
|
|
|
313
|
|
|
$this->admin |
|
|
|
|
314
|
|
|
->method('getDatagridBuilder') |
315
|
|
|
->willReturn($datagridBuilder); |
316
|
|
|
|
317
|
|
|
$listBuilder = $this->createStub(ListBuilderInterface::class); |
318
|
|
|
|
319
|
|
|
$this->admin |
|
|
|
|
320
|
|
|
->method('getListBuilder') |
321
|
|
|
->willReturn($listBuilder); |
322
|
|
|
|
323
|
|
|
$command = $this->application->find('sonata:admin:explain'); |
324
|
|
|
$commandTester = new CommandTester($command); |
325
|
|
|
|
326
|
|
|
$this->expectException(\RuntimeException::class); |
327
|
|
|
$this->expectExceptionMessage( |
328
|
|
|
sprintf( |
329
|
|
|
'Cannot read metadata properties of Acme\Entity\Foo because its metadata is an instance of %s instead of Symfony\Component\Validator\Mapping\ClassMetadata', |
330
|
|
|
\get_class($metadata) |
331
|
|
|
) |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
$commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.