SecurityMiddlewareTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testAccessIsGranted() 0 11 1
A testAccessIsNotGranted() 0 12 1
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);
0 ignored issues
show
Documentation introduced by
$this->authorizationChecker is of type object<Mockery\LegacyMockInterface>, but the function expects a object<Symfony\Component...zationCheckerInterface>.

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...
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);
0 ignored issues
show
Documentation introduced by
$this->authorizationChecker is of type object<Mockery\LegacyMockInterface>, but the function expects a object<Symfony\Component...zationCheckerInterface>.

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...
55
        $handled = false;
56
        $middleware->execute(new FakeCommand(), function () use(&$handled) {
57
            $handled = true;
58
        });
59
60
        $this->assertFalse($handled);
61
    }
62
}
63