Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

SensioSecurityResolverTest::testResolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Tests\Security\TestResolver;
12
13
use PHPUnit\Framework\TestCase;
14
use Symfony\Component\ExpressionLanguage\Expression;
15
use Symfony\Component\HttpFoundation\ParameterBag;
16
use Yarhon\RouteGuardBundle\Security\Test\SymfonySecurityTest;
17
use Yarhon\RouteGuardBundle\Routing\RouteContext;
18
use Yarhon\RouteGuardBundle\Routing\RequestAttributesFactoryInterface;
19
use Yarhon\RouteGuardBundle\Controller\ControllerArgumentResolverInterface;
20
use Yarhon\RouteGuardBundle\ExpressionLanguage\ExpressionDecorator;
21
use Yarhon\RouteGuardBundle\Security\TestProvider\SensioSecurityProvider;
22
use Yarhon\RouteGuardBundle\Security\TestResolver\SensioSecurityResolver;
23
use Yarhon\RouteGuardBundle\Exception\RuntimeException;
24
25
/**
26
 * @author Yaroslav Honcharuk <[email protected]>
27
 */
28
class SensioSecurityResolverTest extends TestCase
29
{
30
    private $controllerArgumentResolver;
31
32
    private $requestAttributesFactory;
33
34
    private $resolver;
35
36
    public function setUp()
37
    {
38
        $this->controllerArgumentResolver = $this->createMock(ControllerArgumentResolverInterface::class);
39
40
        $this->requestAttributesFactory = $this->createMock(RequestAttributesFactoryInterface::class);
41
42
        $this->resolver = new SensioSecurityResolver($this->controllerArgumentResolver, $this->requestAttributesFactory);
43
    }
44
45
    public function testSupports()
46
    {
47
        $test = new SymfonySecurityTest(['ROLE_USER']);
48
        $test->setProviderClass(SensioSecurityProvider::class);
49
50
        $this->assertTrue($this->resolver->supports($test));
51
    }
52
53
    public function testResolve()
54
    {
55
        $routeContext = new RouteContext('index');
56
57
        $resolved = $this->resolver->resolve(new SymfonySecurityTest(['ROLE_USER']), $routeContext);
58
59
        $this->assertSame([['ROLE_USER'], null], $resolved);
60
    }
61
62
    public function testResolveSubjectVariable()
63
    {
64
        $test = new SymfonySecurityTest(['ROLE_USER'], 'foo');
65
66
        $routeContext = new RouteContext('index');
67
68
        $this->controllerArgumentResolver->method('getArgument')
69
            ->with($routeContext, 'foo')
70
            ->willReturn(5);
71
72
        $resolved = $this->resolver->resolve($test, $routeContext);
73
74
        $this->assertSame([['ROLE_USER'], 5], $resolved);
75
    }
76
77
    public function testResolveSubjectVariableException()
78
    {
79
        $test = new SymfonySecurityTest(['ROLE_USER'], 'foo');
80
81
        $routeContext = new RouteContext('index');
82
83
        $this->controllerArgumentResolver->method('getArgument')
84
            ->with($routeContext, 'foo')
85
            ->willThrowException(new RuntimeException('Inner exception.'));
86
87
        $this->expectException(RuntimeException::class);
88
        $this->expectExceptionMessage('Cannot resolve subject variable "foo". Inner exception.');
89
90
        $this->resolver->resolve($test, $routeContext);
91
    }
92
93
    public function testResolveExpressionVariables()
94
    {
95
        $expression = new ExpressionDecorator(new Expression('foo == true'), ['foo']);
96
97
        $test = new SymfonySecurityTest([$expression]);
98
99
        $routeContext = new RouteContext('index');
100
101
        $this->controllerArgumentResolver->method('getArgument')
102
            ->with($routeContext, 'foo')
103
            ->willReturn(5);
104
105
        $resolved = $this->resolver->resolve($test, $routeContext);
106
107
        $this->assertSame([[$expression], null], $resolved);
108
        $this->assertEquals(['foo' => 5], $expression->getVariables());
109
    }
110
111
    public function testResolveExpressionVariablesException()
112
    {
113
        $expression = new ExpressionDecorator(new Expression('foo == true'), ['foo']);
114
115
        $test = new SymfonySecurityTest([$expression]);
116
117
        $routeContext = new RouteContext('index');
118
119
        $this->controllerArgumentResolver->method('getArgument')
120
            ->with($routeContext, 'foo')
121
            ->willThrowException(new RuntimeException('Inner exception.'));
122
123
        $this->expectException(RuntimeException::class);
124
        $this->expectExceptionMessage('Cannot resolve expression variable "foo" of expression "foo == true". Inner exception.');
125
126
        $this->resolver->resolve($test, $routeContext);
127
    }
128
129
    public function testResolveVariableFromRequestAttributes()
130
    {
131
        $test = new SymfonySecurityTest(['ROLE_USER'], 'foo');
132
        $test->setMetadata('request_attributes', ['foo']);
133
134
        $routeContext = new RouteContext('index');
135
136
        $this->requestAttributesFactory->method('createAttributes')
137
            ->with($routeContext)
138
            ->willReturn(new ParameterBag(['foo' => 5]));
139
140
        $resolved = $this->resolver->resolve($test, $routeContext);
141
142
        $this->assertSame([['ROLE_USER'], 5], $resolved);
143
    }
144
145
    public function testResolveVariableFromRequestAttributesException()
146
    {
147
        $test = new SymfonySecurityTest(['ROLE_USER'], 'foo');
148
        $test->setMetadata('request_attributes', ['foo']);
149
150
        $routeContext = new RouteContext('index');
151
152
        $this->requestAttributesFactory->method('createAttributes')
153
            ->with($routeContext)
154
            ->willReturn(new ParameterBag());
155
156
        $this->expectException(RuntimeException::class);
157
        $this->expectExceptionMessage('Cannot resolve subject variable "foo" directly from Request attributes.');
158
159
        $this->resolver->resolve($test, $routeContext);
160
    }
161
}
162