1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\Middleware; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\Middleware\SecurityMiddleware; |
6
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
7
|
|
|
use Mockery; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Unit test for the security middleware. |
14
|
|
|
* |
15
|
|
|
* @author Ron Rademaker |
16
|
|
|
*/ |
17
|
|
|
class SecurityMiddlewareTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Authorization checker mock. |
21
|
|
|
*/ |
22
|
|
|
private $authorizationChecker; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set up. |
26
|
|
|
*/ |
27
|
|
|
public function setUp(): void |
28
|
|
|
{ |
29
|
|
|
$this->authorizationChecker = Mockery::mock(AuthorizationCheckerInterface::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Tests the command is handled if access is granted. |
34
|
|
|
*/ |
35
|
|
|
public function testAccessIsGranted() |
36
|
|
|
{ |
37
|
|
|
$this->authorizationChecker->shouldReceive('isGranted')->andReturn(true); |
38
|
|
|
$middleware = new SecurityMiddleware($this->authorizationChecker); |
|
|
|
|
39
|
|
|
$handled = false; |
40
|
|
|
$middleware->execute(new FakeCommand(), function () use(&$handled) { |
41
|
|
|
$handled = true; |
42
|
|
|
}); |
43
|
|
|
|
44
|
|
|
$this->assertTrue($handled); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Tests the command is not handled if access is denied and an AccessDenied exception is thrown. |
49
|
|
|
*/ |
50
|
|
|
public function testAccessIsNotGranted() |
51
|
|
|
{ |
52
|
|
|
$this->expectException(AccessDeniedException::class); |
53
|
|
|
$this->authorizationChecker->shouldReceive('isGranted')->andReturn(false); |
54
|
|
|
$middleware = new SecurityMiddleware($this->authorizationChecker); |
|
|
|
|
55
|
|
|
$handled = false; |
56
|
|
|
$middleware->execute(new FakeCommand(), function () use(&$handled) { |
57
|
|
|
$handled = true; |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
$this->assertFalse($handled); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
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: