Completed
Pull Request — master (#41)
by Ross
03:55
created

SecurityMiddlewareTest::testAccessIsGranted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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()
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