Completed
Push — master ( 46c3a0...a94e83 )
by Grégoire
13s queued 11s
created

testExecuteWithManipulatorNotFound()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
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 PHPUnit\Framework\TestCase;
17
use Prophecy\Argument;
18
use Sonata\AdminBundle\Admin\AbstractAdmin;
19
use Sonata\AdminBundle\Admin\Pool;
20
use Sonata\AdminBundle\Command\GenerateObjectAclCommand;
21
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
22
use Symfony\Bridge\Doctrine\RegistryInterface;
23
use Symfony\Component\Console\Application;
24
use Symfony\Component\Console\Output\StreamOutput;
25
use Symfony\Component\Console\Tester\CommandTester;
26
use Symfony\Component\DependencyInjection\ContainerInterface;
27
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
28
29
/**
30
 * @author Javier Spagnoletti <[email protected]>
31
 */
32
class GenerateObjectAclCommandTest extends TestCase
33
{
34
    /**
35
     * @var ContainerInterface
36
     */
37
    private $container;
38
39
    protected function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...tainerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Symfony\Component...ion\ContainerInterface> of property $container.

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...
44
45
        $this->container->expects($this->any())
46
            ->method('has')
47
            ->willReturnCallback(static function (string $id): bool {
48
                switch ($id) {
49
                    case 'doctrine':
50
                        return false;
51
                }
52
            });
53
    }
54
55
    public function testExecuteWithoutDoctrineService(): void
56
    {
57
        $generateObjectAclCommand = new GenerateObjectAclCommand(new Pool($this->container, '', ''), []);
58
59
        $application = new Application();
60
        $application->add($generateObjectAclCommand);
61
62
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
63
        $commandTester = new CommandTester($command);
64
65
        $this->assertFalse($this->container->has('doctrine'));
66
67
        $this->expectException(ServiceNotFoundException::class);
68
        $this->expectExceptionMessage(sprintf('The command "%s" has a dependency on a non-existent service "doctrine".', GenerateObjectAclCommand::getDefaultName()));
69
70
        $commandTester->execute(['command' => GenerateObjectAclCommand::getDefaultName()]);
71
    }
72
73
    public function testExecuteWithEmptyManipulators(): void
74
    {
75
        $pool = new Pool($this->container, '', '');
76
77
        $registry = $this->prophesize(RegistryInterface::class)->reveal();
78
        $command = new GenerateObjectAclCommand($pool, [], $registry);
79
80
        $application = new Application();
81
        $application->add($command);
82
83
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
84
        $commandTester = new CommandTester($command);
85
        $commandTester->execute(['command' => $command->getName()]);
86
87
        $this->assertRegExp('/No manipulators are implemented : ignoring/', $commandTester->getDisplay());
88
    }
89
90
    public function testExecuteWithManipulatorNotFound(): void
91
    {
92
        $admin = $this->prophesize(AbstractAdmin::class);
93
        $registry = $this->prophesize(RegistryInterface::class);
94
        $pool = $this->prophesize(Pool::class);
95
96
        $admin->getManagerType(Argument::any())->willReturn('bar');
97
98
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
99
100
        $pool->getInstance(Argument::any())->willReturn($admin->reveal());
101
102
        $aclObjectManipulators = [
103
            'bar' => new \stdClass(),
104
        ];
105
106
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
107
108
        $application = new Application();
109
        $application->add($command);
110
111
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
112
        $commandTester = new CommandTester($command);
113
        $commandTester->execute(['command' => $command->getName()]);
114
115
        $this->assertRegExp('/Admin class is using a manager type that has no manipulator implemented : ignoring/', $commandTester->getDisplay());
116
    }
117
118
    public function testExecuteWithManipulatorNotObjectAclManipulatorInterface(): void
119
    {
120
        $admin = $this->prophesize(AbstractAdmin::class);
121
        $registry = $this->prophesize(RegistryInterface::class);
122
        $pool = $this->prophesize(Pool::class);
123
124
        $admin->getManagerType(Argument::any())->willReturn('bar');
125
126
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
127
        $pool->getInstance(Argument::any())->willReturn($admin->reveal());
128
129
        $aclObjectManipulators = [
130
            'sonata.admin.manipulator.acl.object.bar' => new \stdClass(),
131
        ];
132
133
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
134
135
        $application = new Application();
136
        $application->add($command);
137
138
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
139
        $commandTester = new CommandTester($command);
140
        $commandTester->execute(['command' => $command->getName()]);
141
142
        $this->assertRegExp('/The interface "ObjectAclManipulatorInterface" is not implemented for/', $commandTester->getDisplay());
143
    }
144
145
    public function testExecuteWithManipulator(): void
146
    {
147
        $admin = $this->prophesize(AbstractAdmin::class);
148
        $registry = $this->prophesize(RegistryInterface::class);
149
        $pool = $this->prophesize(Pool::class);
150
151
        $admin->getManagerType(Argument::any())->willReturn('bar');
152
        $admin = $admin->reveal();
153
154
        $pool->getAdminServiceIds()->willReturn(['acme.admin.foo']);
155
        $pool->getInstance(Argument::any())->willReturn($admin);
156
157
        $manipulator = $this->prophesize(ObjectAclManipulatorInterface::class);
158
        $manipulator
159
            ->batchConfigureAcls(Argument::type(StreamOutput::class), Argument::is($admin), null)
160
            ->shouldBeCalledTimes(1);
161
162
        $aclObjectManipulators = [
163
            'sonata.admin.manipulator.acl.object.bar' => $manipulator->reveal(),
164
        ];
165
166
        $command = new GenerateObjectAclCommand($pool->reveal(), $aclObjectManipulators, $registry->reveal());
167
168
        $application = new Application();
169
        $application->add($command);
170
171
        $command = $application->find(GenerateObjectAclCommand::getDefaultName());
172
        $commandTester = new CommandTester($command);
173
        $commandTester->execute(['command' => $command->getName()]);
174
    }
175
}
176