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); |
|
|
|
|
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)); |
|
|
|
|
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()) |
|
|
|
|
70
|
|
|
->method('get') |
71
|
|
|
->willReturnCallback(static function (string $id) { |
|
|
|
|
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)); |
|
|
|
|
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()); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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..