Completed
Push — 3.x ( 3e834f...38b337 )
by Grégoire
03:36
created

tests/Command/ExplainAdminCommandTest.php (8 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\Container;
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
61
        $container = new Container();
62
63
        $this->admin = $this->createMock(AdminInterface::class);
64
65
        $this->admin
66
            ->method('getCode')
67
            ->willReturn('foo');
68
69
        $this->admin
70
            ->method('getClass')
71
            ->willReturn('Acme\Entity\Foo');
72
73
        $this->admin
74
            ->method('getBaseControllerName')
75
            ->willReturn(CRUDController::class);
76
77
        $routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', CRUDController::class);
78
        $routeCollection->add('list');
79
        $routeCollection->add('edit');
80
81
        $this->admin
82
            ->method('getRoutes')
83
            ->willReturn($routeCollection);
84
85
        $fieldDescription1 = $this->createMock(FieldDescriptionInterface::class);
86
87
        $fieldDescription1
88
            ->method('getType')
89
            ->willReturn('text');
90
91
        $fieldDescription1
92
            ->method('getTemplate')
93
            ->willReturn('@SonataAdmin/CRUD/foo_text.html.twig');
94
95
        $fieldDescription2 = $this->createMock(FieldDescriptionInterface::class);
96
97
        $fieldDescription2
98
            ->method('getType')
99
            ->willReturn('datetime');
100
101
        $fieldDescription2
102
            ->method('getTemplate')
103
            ->willReturn('@SonataAdmin/CRUD/bar_datetime.html.twig');
104
105
        $this->admin
106
            ->method('getListFieldDescriptions')
107
            ->willReturn([
108
                'fooTextField' => $fieldDescription1,
109
                'barDateTimeField' => $fieldDescription2,
110
            ]);
111
112
        $this->admin
113
            ->method('getFilterFieldDescriptions')
114
            ->willReturn([
115
                'fooTextField' => $fieldDescription1,
116
                'barDateTimeField' => $fieldDescription2,
117
            ]);
118
119
        $this->admin
120
            ->method('getFormTheme')
121
            ->willReturn(['@Foo/bar.html.twig']);
122
123
        $this->admin
124
            ->method('getFormFieldDescriptions')
125
            ->willReturn([
126
                'fooTextField' => $fieldDescription1,
127
                'barDateTimeField' => $fieldDescription2,
128
            ]);
129
130
        $this->admin
131
            ->method('isChild')
132
            ->willReturn(true);
133
134
        $this->admin
135
            ->method('getParent')
136
            ->willReturnCallback(function () {
137
                $adminParent = $this->createMock(AdminInterface::class);
138
139
                $adminParent
140
                    ->method('getCode')
141
                    ->willReturn('foo_child');
142
143
                return $adminParent;
144
            });
145
146
        $container->set('acme.admin.foo', $this->admin);
147
148
        $pool = new Pool($container, '', '');
149
        $pool->setAdminServiceIds(['acme.admin.foo', 'acme.admin.bar']);
150
151
        $this->validatorFactory = $this->createMock(MetadataFactoryInterface::class);
152
153
        $command = new ExplainAdminCommand($pool, $this->validatorFactory);
154
155
        $this->application->add($command);
156
    }
157
158
    public function testExecute(): void
159
    {
160
        $metadata = $this->createMock(MetadataInterface::class);
161
162
        $this->validatorFactory->expects($this->once())
163
            ->method('getMetadataFor')
164
            ->with($this->equalTo('Acme\Entity\Foo'))
165
            ->willReturn($metadata);
166
167
        $propertyMetadata = $this->getMockForAbstractClass(GenericMetadata::class);
168
        $propertyMetadata->constraints = [
169
            new NotNull(),
170
            new Length(['min' => 2, 'max' => 50, 'groups' => ['create', 'edit']]),
171
        ];
172
173
        $metadata->properties = ['firstName' => $propertyMetadata];
174
175
        $getterMetadata = $this->getMockForAbstractClass(GenericMetadata::class);
176
        $getterMetadata->constraints = [
177
            new NotNull(),
178
            new Email(['groups' => ['registration', 'edit']]),
179
        ];
180
181
        $metadata->getters = ['email' => $getterMetadata];
182
183
        $modelManager = $this->createMock(ModelManagerInterface::class);
184
185
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
186
            ->method('getModelManager')
187
            ->willReturn($modelManager);
188
189
        $formBuilder = $this->createMock(FormBuilderInterface::class);
190
191
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
192
             ->method('getFormBuilder')
193
             ->willReturn($formBuilder);
194
195
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
196
197
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
198
            ->method('getDatagridBuilder')
199
            ->willReturn($datagridBuilder);
200
201
        $listBuilder = $this->createMock(ListBuilderInterface::class);
202
203
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
204
            ->method('getListBuilder')
205
            ->willReturn($listBuilder);
206
207
        $command = $this->application->find('sonata:admin:explain');
208
        $commandTester = new CommandTester($command);
209
        $commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']);
210
211
        $this->assertSame(sprintf(
212
            str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
213
            \get_class($this->admin),
214
            \get_class($modelManager),
215
            \get_class($formBuilder),
216
            \get_class($datagridBuilder),
217
            \get_class($listBuilder)
218
        ), $commandTester->getDisplay());
219
    }
220
221
    public function testExecuteEmptyValidator(): void
222
    {
223
        $metadata = $this->createMock(MetadataInterface::class);
224
225
        $this->validatorFactory->expects($this->once())
226
            ->method('getMetadataFor')
227
            ->with($this->equalTo('Acme\Entity\Foo'))
228
            ->willReturn($metadata);
229
230
        $metadata->properties = [];
231
        $metadata->getters = [];
232
233
        $modelManager = $this->createMock(ModelManagerInterface::class);
234
235
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
236
            ->method('getModelManager')
237
            ->willReturn($modelManager);
238
239
        $formBuilder = $this->createMock(FormBuilderInterface::class);
240
241
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
242
             ->method('getFormBuilder')
243
             ->willReturn($formBuilder);
244
245
        $datagridBuilder = $this->createMock(DatagridBuilderInterface::class);
246
247
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
248
            ->method('getDatagridBuilder')
249
            ->willReturn($datagridBuilder);
250
251
        $listBuilder = $this->createMock(ListBuilderInterface::class);
252
253
        $this->admin
0 ignored issues
show
The method method() does not seem to exist on object<Sonata\AdminBundle\Admin\AdminInterface>.

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.

Loading history...
254
            ->method('getListBuilder')
255
            ->willReturn($listBuilder);
256
257
        $command = $this->application->find('sonata:admin:explain');
258
        $commandTester = new CommandTester($command);
259
        $commandTester->execute(['command' => $command->getName(), 'admin' => 'acme.admin.foo']);
260
261
        $this->assertSame(sprintf(
262
            str_replace(
263
                "\n",
264
                PHP_EOL,
265
                file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
266
            ),
267
            \get_class($this->admin),
268
            \get_class($modelManager),
269
            \get_class($formBuilder),
270
            \get_class($datagridBuilder),
271
            \get_class($listBuilder)
272
        ), $commandTester->getDisplay());
273
    }
274
275
    public function testExecuteNonAdminService(): void
276
    {
277
        $command = $this->application->find('sonata:admin:explain');
278
        $commandTester = new CommandTester($command);
279
280
        $this->expectException(\InvalidArgumentException::class);
281
        $this->expectExceptionMessage('Admin service "nonexistent.service" not found in admin pool. Did you mean "acme.admin.bar" or one of those: []');
282
283
        $commandTester->execute(['command' => $command->getName(), 'admin' => 'nonexistent.service']);
284
    }
285
}
286