Completed
Push — 3.x ( b75183...b1c847 )
by Oskar
04:45
created

tests/Command/ExplainAdminCommandTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 PHPUnit\Framework\TestCase;
17
use Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
21
use Sonata\AdminBundle\Builder\ListBuilderInterface;
22
use Sonata\AdminBundle\Command\ExplainAdminCommand;
23
use Sonata\AdminBundle\Controller\CRUDController;
24
use Sonata\AdminBundle\Model\ModelManagerInterface;
25
use Sonata\AdminBundle\Route\RouteCollection;
26
use Symfony\Component\Console\Application;
27
use Symfony\Component\Console\Tester\CommandTester;
28
use Symfony\Component\DependencyInjection\ContainerInterface;
29
use Symfony\Component\Form\FormBuilderInterface;
30
use Symfony\Component\Validator\Constraints\Email;
31
use Symfony\Component\Validator\Constraints\Length;
32
use Symfony\Component\Validator\Constraints\NotNull;
33
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
34
use Symfony\Component\Validator\Mapping\GenericMetadata;
35
use Symfony\Component\Validator\Mapping\MetadataInterface;
36
37
/**
38
 * @author Andrej Hudec <[email protected]>
39
 */
40
class ExplainAdminCommandTest extends TestCase
41
{
42
    /**
43
     * @var Application
44
     */
45
    private $application;
46
47
    /**
48
     * @var AdminInterface
49
     */
50
    private $admin;
51
52
    /**
53
     * @var MetadataFactoryInterface
54
     */
55
    private $validatorFactory;
56
57
    protected function setUp(): void
58
    {
59
        $this->application = new Application();
60
        $command = new ExplainAdminCommand();
61
62
        $container = $this->createMock(ContainerInterface::class);
63
64
        $this->admin = $this->createMock(AdminInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Sonat...\AdminInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Sonata\AdminBundle\Admin\AdminInterface> of property $admin.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
65
66
        $this->admin->expects($this->any())
67
            ->method('getCode')
68
            ->will($this->returnValue('foo'));
69
70
        $this->admin->expects($this->any())
71
            ->method('getClass')
72
            ->will($this->returnValue('Acme\Entity\Foo'));
73
74
        $this->admin->expects($this->any())
75
            ->method('getBaseControllerName')
76
            ->will($this->returnValue(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->expects($this->any())
83
            ->method('getRoutes')
84
            ->will($this->returnValue($routeCollection));
85
86
        $fieldDescription1 = $this->createMock(FieldDescriptionInterface::class);
87
88
        $fieldDescription1->expects($this->any())
89
            ->method('getType')
90
            ->will($this->returnValue('text'));
91
92
        $fieldDescription1->expects($this->any())
93
            ->method('getTemplate')
94
            ->will($this->returnValue('@SonataAdmin/CRUD/foo_text.html.twig'));
95
96
        $fieldDescription2 = $this->createMock(FieldDescriptionInterface::class);
97
98
        $fieldDescription2->expects($this->any())
99
            ->method('getType')
100
            ->will($this->returnValue('datetime'));
101
102
        $fieldDescription2->expects($this->any())
103
            ->method('getTemplate')
104
            ->will($this->returnValue('@SonataAdmin/CRUD/bar_datetime.html.twig'));
105
106
        $this->admin->expects($this->any())
107
            ->method('getListFieldDescriptions')
108
            ->will($this->returnValue([
109
                'fooTextField' => $fieldDescription1,
110
                'barDateTimeField' => $fieldDescription2,
111
            ]));
112
113
        $this->admin->expects($this->any())
114
            ->method('getFilterFieldDescriptions')
115
            ->will($this->returnValue([
116
                'fooTextField' => $fieldDescription1,
117
                'barDateTimeField' => $fieldDescription2,
118
            ]));
119
120
        $this->admin->expects($this->any())
121
            ->method('getFormTheme')
122
            ->will($this->returnValue(['@Foo/bar.html.twig']));
123
124
        $this->admin->expects($this->any())
125
            ->method('getFormFieldDescriptions')
126
            ->will($this->returnValue([
127
                'fooTextField' => $fieldDescription1,
128
                'barDateTimeField' => $fieldDescription2,
129
            ]));
130
131
        $this->admin->expects($this->any())
132
            ->method('isChild')
133
            ->will($this->returnValue(true));
134
135
        $this->admin->expects($this->any())
136
            ->method('getParent')
137
            ->will($this->returnCallback(function () {
138
                $adminParent = $this->createMock(AdminInterface::class);
139
140
                $adminParent->expects($this->any())
141
                    ->method('getCode')
142
                    ->will($this->returnValue('foo_child'));
143
144
                return $adminParent;
145
            }));
146
147
        $this->validatorFactory = $this->createMock(MetadataFactoryInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...actoryInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...tadataFactoryInterface> of property $validatorFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
148
149
        $container->expects($this->any())
150
            ->method('get')
151
            ->will($this->returnCallback(function ($id) use ($container) {
152
                switch ($id) {
153
                    case 'sonata.admin.pool':
154
                        $pool = new Pool($container, '', '');
155
                        $pool->setAdminServiceIds(['acme.admin.foo', 'acme.admin.bar']);
156
157
                        return $pool;
158
159
                    case 'validator':
160
                        return $this->validatorFactory;
161
162
                    case 'acme.admin.foo':
163
                        return $this->admin;
164
                }
165
            }));
166
167
        $container->expects($this->any())->method('has')->will($this->returnValue(true));
168
169
        $command->setContainer($container);
170
171
        $this->application->add($command);
172
    }
173
174
    public function testExecute(): void
175
    {
176
        $metadata = $this->createMock(MetadataInterface::class);
177
178
        $this->validatorFactory->expects($this->once())
179
            ->method('getMetadataFor')
180
            ->with($this->equalTo('Acme\Entity\Foo'))
181
            ->will($this->returnValue($metadata));
182
183
        $propertyMetadata = $this->getMockForAbstractClass(GenericMetadata::class);
184
        $propertyMetadata->constraints = [
185
            new NotNull(),
186
            new Length(['min' => 2, 'max' => 50, 'groups' => ['create', 'edit']]),
187
        ];
188
189
        $metadata->properties = ['firstName' => $propertyMetadata];
190
191
        $getterMetadata = $this->getMockForAbstractClass(GenericMetadata::class);
192
        $getterMetadata->constraints = [
193
            new NotNull(),
194
            new Email(['groups' => ['registration', 'edit']]),
195
        ];
196
197
        $metadata->getters = ['email' => $getterMetadata];
198
199
        $modelManager = $this->createMock(ModelManagerInterface::class);
200
201
        $this->admin->expects($this->any())
202
            ->method('getModelManager')
203
            ->will($this->returnValue($modelManager));
204
205
        $formBuilder = $this->createMock(FormBuilderInterface::class);
206
207
        $this->admin->expects($this->any())
208
             ->method('getFormBuilder')
209
             ->will($this->returnValue($formBuilder));
210
211
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
212
213
        $this->admin->expects($this->any())
214
            ->method('getDatagridBuilder')
215
            ->will($this->returnValue($datagridBuilder));
216
217
        $listBuilder = $this->createMock(ListBuilderInterface::class);
218
219
        $this->admin->expects($this->any())
220
            ->method('getListBuilder')
221
            ->will($this->returnValue($listBuilder));
222
223
        $command = $this->application->find('sonata:admin:explain');
224
        $commandTester = new CommandTester($command);
225
        $commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']);
226
227
        $this->assertSame(sprintf(
228
            str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
229
            \get_class($this->admin),
230
            \get_class($modelManager),
231
            \get_class($formBuilder),
232
            \get_class($datagridBuilder),
233
            \get_class($listBuilder)
234
        ), $commandTester->getDisplay());
235
    }
236
237
    public function testExecuteEmptyValidator(): void
238
    {
239
        $metadata = $this->createMock(MetadataInterface::class);
240
241
        $this->validatorFactory->expects($this->once())
242
            ->method('getMetadataFor')
243
            ->with($this->equalTo('Acme\Entity\Foo'))
244
            ->will($this->returnValue($metadata));
245
246
        $metadata->properties = [];
247
        $metadata->getters = [];
248
249
        $modelManager = $this->createMock(ModelManagerInterface::class);
250
251
        $this->admin->expects($this->any())
252
            ->method('getModelManager')
253
            ->will($this->returnValue($modelManager));
254
255
        $formBuilder = $this->createMock(FormBuilderInterface::class);
256
257
        $this->admin->expects($this->any())
258
             ->method('getFormBuilder')
259
             ->will($this->returnValue($formBuilder));
260
261
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
262
263
        $this->admin->expects($this->any())
264
            ->method('getDatagridBuilder')
265
            ->will($this->returnValue($datagridBuilder));
266
267
        $listBuilder = $this->createMock(ListBuilderInterface::class);
268
269
        $this->admin->expects($this->any())
270
            ->method('getListBuilder')
271
            ->will($this->returnValue($listBuilder));
272
273
        $command = $this->application->find('sonata:admin:explain');
274
        $commandTester = new CommandTester($command);
275
        $commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']);
276
277
        $this->assertSame(sprintf(
278
            str_replace(
279
                "\n",
280
                PHP_EOL,
281
                file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
282
            ),
283
            \get_class($this->admin),
284
            \get_class($modelManager),
285
            \get_class($formBuilder),
286
            \get_class($datagridBuilder),
287
            \get_class($listBuilder)
288
        ), $commandTester->getDisplay());
289
    }
290
291
    public function testExecuteNonAdminService(): void
292
    {
293
        try {
294
            $command = $this->application->find('sonata:admin:explain');
295
            $commandTester = new CommandTester($command);
296
            $commandTester->execute(['command' => $command->getName(), 'admin' => 'nonexistent.service']);
297
        } catch (\RuntimeException $e) {
298
            $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
299
300
            return;
301
        }
302
303
        $this->fail('An expected exception has not been raised.');
304
    }
305
}
306