Completed
Push — 3.x ( 1cd1d0...b03505 )
by Grégoire
16:27 queued 03:11
created

SetupAclCommandTest::setUp()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
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\ContainerInterface;
24
25
/**
26
 * @author Andrej Hudec <[email protected]>
27
 */
28
class SetupAclCommandTest extends TestCase
29
{
30
    /**
31
     * @var ContainerInterface
32
     */
33
    private $container;
34
35
    protected function setUp(): void
36
    {
37
        $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...
38
        $admin = $this->createMock(AdminInterface::class);
39
40
        $this->container->expects($this->any())
41
            ->method('get')
42
            ->willReturnCallback(static function (string $id) use ($admin): AdminInterface {
43
                switch ($id) {
44
                    case 'acme.admin.foo':
45
                        return $admin;
46
                }
47
            });
48
    }
49
50
    public function testExecute(): void
51
    {
52
        $pool = new Pool($this->container, '', '');
53
        $pool->setAdminServiceIds(['acme.admin.foo']);
54
55
        $command = new SetupAclCommand($pool, $this->createMock(AdminAclManipulatorInterface::class));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Sonat...ulatorInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, 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...
56
57
        $application = new Application();
58
        $application->add($command);
59
60
        $command = $application->find('sonata:admin:setup-acl');
61
        $commandTester = new CommandTester($command);
62
        $commandTester->execute(['command' => $command->getName()]);
63
64
        $this->assertRegExp('/Starting ACL AdminBundle configuration/', $commandTester->getDisplay());
65
    }
66
67
    public function testExecuteWithException1(): void
68
    {
69
        $this->container->expects($this->any())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

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...
70
            ->method('get')
71
            ->willReturnCallback(static function (string $id) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
72
                throw new \Exception('Foo Exception');
73
            });
74
75
        $pool = new Pool($this->container, '', '');
76
        $pool->setAdminServiceIds(['acme.admin.foo']);
77
78
        $command = new SetupAclCommand($pool, $this->createMock(AdminAclManipulatorInterface::class));
0 ignored issues
show
Documentation introduced by
$this->createMock(\Sonat...ulatorInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, 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...
79
80
        $application = new Application();
81
        $application->add($command);
82
83
        $command = $application->find('sonata:admin:setup-acl');
84
        $commandTester = new CommandTester($command);
85
        $commandTester->execute(['command' => $command->getName()]);
86
87
        $this->assertRegExp('@Starting ACL AdminBundle configuration\s+Warning : The admin class cannot be initiated from the command line\s+Foo Exception@', $commandTester->getDisplay());
88
    }
89
90
    public function testExecuteWithException2(): void
91
    {
92
        $pool = new Pool($this->container, '', '');
93
94
        $this->expectException(\TypeError::class);
95
        $this->expectExceptionMessage(sprintf('Argument 2 passed to %s::__construct() must implement interface %s, instance of %s given', SetupAclCommand::class, AdminAclManipulatorInterface::class, \stdClass::class));
96
97
        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...
98
    }
99
}
100