testCanNotBootKernelIfLoadingSecurityMiddlewareWithoutSecurityBeingTurnedOn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\Integration;
4
5
use League\Tactician\Bundle\Tests\Fake\FakeCommand;
6
use stdClass;
7
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
8
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
9
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
10
use Symfony\Component\Security\Core\Role\Role;
11
12
/**
13
 * Integration test for security middleware.
14
 *
15
 * @author Ron Rademaker
16
 *
17
 * @runTestsInSeparateProcesses
18
 */
19
class SecurityTest extends IntegrationTest
20
{
21
    public function testCanBootKernelWithSecurityMiddleware()
22
    {
23
        $this->loadSecurityConfiguration();
24
25
        $this->givenConfig('tactician', <<<'EOF'
26
commandbus:
27
    default:
28
        middleware:
29
            - tactician.middleware.security
30
EOF
31
        );
32
        static::$kernel->boot();
33
        $this->assertTrue(true);
34
    }
35
36
    public function testCanNotBootKernelIfLoadingSecurityMiddlewareWithoutSecurityBeingTurnedOn()
37
    {
38
        $this->expectException(ServiceNotFoundException::class);
39
        $this->givenConfig('tactician', <<<'EOF'
40
commandbus:
41
    default:
42
        middleware:
43
            - tactician.middleware.security
44
EOF
45
        );
46
        static::$kernel->boot();
47
    }
48
49
    public function testCanBootKernelWithoutSecurityOrSecurityMiddleware()
50
    {
51
        $this->givenConfig('tactician', <<<'EOF'
52
commandbus:
53
    default:
54
        middleware:
55
            - tactician.middleware.command_handler
56
EOF
57
        );
58
        static::$kernel->boot();
59
        $this->assertTrue(true);
60
    }
61
62
    /**
63
     * @dataProvider provideTestData
64
     */
65
    public function testSecurityMiddleware($command, string $role, string $expectedExceptionClassName = null)
66
    {
67
        if ($expectedExceptionClassName) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $expectedExceptionClassName of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
68
            $this->expectException($expectedExceptionClassName);
69
        }
70
71
        $this->loadSecurityConfiguration();
72
        $this->givenConfig('tactician', <<<'EOF'
73
commandbus:
74
    default:
75
        middleware:
76
            - tactician.middleware.security
77
security:
78
    League\Tactician\Bundle\Tests\Fake\FakeCommand:
79
        - 'ROLE_ADMIN'
80
EOF
81
        );
82
83
        static::$kernel->boot();
84
        $this->setUserRole($role);
85
86
        static::$kernel->getContainer()->get('tactician.commandbus.default')->handle($command);
87
    }
88
89
    /**
90
     * Gets test data for security middleware integration test.
91
     *
92
     * @return array
93
     */
94
    public function provideTestData(): array
95
    {
96
        return [
97
            'Role may handle the command' => [new FakeCommand(), 'ROLE_ADMIN'],
98
            'Test role hierarchy' => [new FakeCommand(), 'ROLE_SUPER_ADMIN'],
99
            'Role may not handle the command' => [new FakeCommand(), 'ROLE_USER', AccessDeniedException::class],
100
            'Deny access if command is not in the mapping' => [new stdClass(), 'ROLE_SUPER_ADMIN', AccessDeniedException::class],
101
        ];
102
    }
103
104
    /**
105
     * Security configuration.
106
     */
107
    private function loadSecurityConfiguration()
108
    {
109
        $this->givenConfig('security', <<< 'EOF'
110
access_denied_url: /
111
112
role_hierarchy:
113
    ROLE_ADMIN:       ROLE_USER
114
    ROLE_SUPER_ADMIN: ROLE_ADMIN
115
116
providers:
117
    my_in_memory_provider:
118
        memory:
119
120
firewalls:
121
    main:
122
        anonymous: ~
123
        http_basic: ~
124
EOF
125
        );
126
    }
127
128
    /**
129
     * @param string $role
130
     */
131
    protected function setUserRole(string $role)
132
    {
133
        static::$kernel->getContainer()
134
            ->get('security.token_storage')
135
            ->setToken(
136
                new AnonymousToken('test', 'anon', [$role])
137
            );
138
    }
139
}
140