Completed
Push — 3.x ( ca65af...ac0550 )
by Grégoire
03:27
created

testExecuteWithDeprecatedDoctrineService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Doctrine\Common\Persistence\ManagerRegistry;
17
use PHPUnit\Framework\TestCase;
18
use Prophecy\Argument;
19
use Sonata\AdminBundle\Admin\AbstractAdmin;
20
use Sonata\AdminBundle\Admin\Pool;
21
use Sonata\AdminBundle\Command\GenerateObjectAclCommand;
22
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
23
use Symfony\Bridge\Doctrine\RegistryInterface;
24
use Symfony\Component\Console\Application;
25
use Symfony\Component\Console\Output\StreamOutput;
26
use Symfony\Component\Console\Tester\CommandTester;
27
use Symfony\Component\DependencyInjection\ContainerInterface;
28
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
29
30
/**
31
 * @author Javier Spagnoletti <[email protected]>
32
 */
33
class GenerateObjectAclCommandTest extends TestCase
34
{
35
    /**
36
     * @var ContainerInterface
37
     */
38
    private $container;
39
40
    protected function setUp(): void
41
    {
42
        parent::setUp();
43
44
        $this->container = $this->createMock(ContainerInterface::class);
45
46
        $this->container->expects($this->any())
47
            ->method('has')
48
            ->willReturnCallback(static function (string $id): bool {
49
                switch ($id) {
50
                    case 'doctrine':
51
                        return false;
52
                }
53
            });
54
    }
55
56
    public function testExecuteWithoutDoctrineService(): void
57
    {
58
        $generateObjectAclCommand = new GenerateObjectAclCommand(new Pool($this->container, '', ''), []);
59
60
        $application = new Application();
61
        $application->add($generateObjectAclCommand);
62
63
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
64
        $commandTester = new CommandTester($command);
65
66
        $this->assertFalse($this->container->has('doctrine'));
67
68
        $this->expectException(ServiceNotFoundException::class);
69
        $this->expectExceptionMessage(sprintf('The command "%s" has a dependency on a non-existent service "doctrine".', GenerateObjectAclCommand::getDefaultName()));
70
71
        $commandTester->execute(['command' => GenerateObjectAclCommand::getDefaultName()]);
72
    }
73
74
    public function testExecuteWithDeprecatedDoctrineService(): void
75
    {
76
        $pool = new Pool($this->container, '', '');
77
78
        $registry = $this->prophesize(RegistryInterface::class)->reveal();
79
        $command = new GenerateObjectAclCommand($pool, [], $registry);
80
81
        $application = new Application();
82
        $application->add($command);
83
84
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
85
        $commandTester = new CommandTester($command);
86
        $commandTester->execute(['command' => $command->getName()]);
87
88
        $this->assertRegExp('/No manipulators are implemented : ignoring/', $commandTester->getDisplay());
89
    }
90
91
    public function testExecuteWithEmptyManipulators(): void
92
    {
93
        $pool = new Pool($this->container, '', '');
94
95
        $registry = $this->prophesize(ManagerRegistry::class)->reveal();
96
        $command = new GenerateObjectAclCommand($pool, [], $registry);
97
98
        $application = new Application();
99
        $application->add($command);
100
101
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
102
        $commandTester = new CommandTester($command);
103
        $commandTester->execute(['command' => $command->getName()]);
104
105
        $this->assertRegExp('/No manipulators are implemented : ignoring/', $commandTester->getDisplay());
106
    }
107
108
    public function testExecuteWithManipulatorNotFound(): void
109
    {
110
        $admin = $this->prophesize(AbstractAdmin::class);
111
        $registry = $this->prophesize(ManagerRegistry::class);
112
        $pool = $this->prophesize(Pool::class);
113
114
        $admin->getManagerType(Argument::any())->willReturn('bar');
115
116
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
117
118
        $pool->getInstance(Argument::any())->willReturn($admin->reveal());
119
120
        $aclObjectManipulators = [
121
            'bar' => new \stdClass(),
122
        ];
123
124
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
125
126
        $application = new Application();
127
        $application->add($command);
128
129
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
130
        $commandTester = new CommandTester($command);
131
        $commandTester->execute(['command' => $command->getName()]);
132
133
        $this->assertRegExp('/Admin class is using a manager type that has no manipulator implemented : ignoring/', $commandTester->getDisplay());
134
    }
135
136
    public function testExecuteWithManipulatorNotObjectAclManipulatorInterface(): void
137
    {
138
        $admin = $this->prophesize(AbstractAdmin::class);
139
        $registry = $this->prophesize(ManagerRegistry::class);
140
        $pool = $this->prophesize(Pool::class);
141
142
        $admin->getManagerType(Argument::any())->willReturn('bar');
143
144
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
145
        $pool->getInstance(Argument::any())->willReturn($admin->reveal());
146
147
        $aclObjectManipulators = [
148
            'sonata.admin.manipulator.acl.object.bar' => new \stdClass(),
149
        ];
150
151
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
152
153
        $application = new Application();
154
        $application->add($command);
155
156
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
157
        $commandTester = new CommandTester($command);
158
        $commandTester->execute(['command' => $command->getName()]);
159
160
        $this->assertRegExp('/The interface "ObjectAclManipulatorInterface" is not implemented for/', $commandTester->getDisplay());
161
    }
162
163
    public function testExecuteWithManipulator(): void
164
    {
165
        $admin = $this->prophesize(AbstractAdmin::class);
166
        $registry = $this->prophesize(ManagerRegistry::class);
167
        $pool = $this->prophesize(Pool::class);
168
169
        $admin->getManagerType(Argument::any())->willReturn('bar');
170
        $admin = $admin->reveal();
171
172
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
173
        $pool->getInstance(Argument::any())->willReturn($admin);
174
175
        $manipulator = $this->prophesize(ObjectAclManipulatorInterface::class);
176
        $manipulator
177
            ->batchConfigureAcls(Argument::type(StreamOutput::class), Argument::is($admin), null)
178
            ->shouldBeCalledTimes(1);
179
180
        $aclObjectManipulators = [
181
            'sonata.admin.manipulator.acl.object.bar' => $manipulator->reveal(),
182
        ];
183
184
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
185
186
        $application = new Application();
187
        $application->add($command);
188
189
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
190
        $commandTester = new CommandTester($command);
191
        $commandTester->execute(['command' => $command->getName()]);
192
    }
193
}
194