SetupAclCommandTest::testExecuteWithException1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
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 Sonata\AdminBundle\Admin\AdminInterface;
18
use Sonata\AdminBundle\Admin\Pool;
19
use Sonata\AdminBundle\Command\SetupAclCommand;
20
use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
21
use Symfony\Component\Console\Application;
22
use Symfony\Component\Console\Tester\CommandTester;
23
use Symfony\Component\DependencyInjection\Container;
24
25
/**
26
 * @author Andrej Hudec <[email protected]>
27
 */
28
class SetupAclCommandTest extends TestCase
29
{
30
    /**
31
     * @var Container
32
     */
33
    private $container;
34
35
    protected function setUp(): void
36
    {
37
        $this->container = new Container();
38
        $admin = $this->createMock(AdminInterface::class);
39
40
        $this->container->set('acme.admin.foo', $admin);
41
    }
42
43
    public function testExecute(): void
44
    {
45
        $pool = new Pool($this->container, '', '');
46
        $pool->setAdminServiceIds(['acme.admin.foo']);
47
48
        $command = new SetupAclCommand($pool, $this->createMock(AdminAclManipulatorInterface::class));
49
50
        $application = new Application();
51
        $application->add($command);
52
53
        $command = $application->find('sonata:admin:setup-acl');
54
        $commandTester = new CommandTester($command);
55
        $commandTester->execute(['command' => $command->getName()]);
56
57
        $this->assertRegExp('/Starting ACL AdminBundle configuration/', $commandTester->getDisplay());
58
    }
59
60
    public function testExecuteWithException1(): void
61
    {
62
        $this->container->set('acme.admin.foo', null);
63
        $pool = new Pool($this->container, '', '');
64
        $pool->setAdminServiceIds(['acme.admin.foo']);
65
66
        $command = new SetupAclCommand($pool, $this->createMock(AdminAclManipulatorInterface::class));
67
68
        $application = new Application();
69
        $application->add($command);
70
71
        $command = $application->find('sonata:admin:setup-acl');
72
        $commandTester = new CommandTester($command);
73
        $commandTester->execute(['command' => $command->getName()]);
74
75
        $this->assertRegExp(
76
            '@Starting ACL AdminBundle configuration\s+Warning : The admin class cannot be initiated from the command line\s+You have requested a non-existent service "acme.admin.foo".@',
77
            $commandTester->getDisplay()
78
        );
79
    }
80
81
    public function testExecuteWithException2(): void
82
    {
83
        $pool = new Pool($this->container, '', '');
84
85
        $this->expectException(\TypeError::class);
86
        $this->expectExceptionMessage(sprintf('Argument 2 passed to %s::__construct() must implement interface %s, instance of %s given', SetupAclCommand::class, AdminAclManipulatorInterface::class, \stdClass::class));
87
88
        new SetupAclCommand($pool, new \stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<Sonata\AdminBundl...clManipulatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
    }
90
}
91