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\SensioExtraTest; |
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\TestResolver\SensioExtraResolver; |
22
|
|
|
use Yarhon\RouteGuardBundle\Exception\RuntimeException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class SensioExtraResolverTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
private $controllerArgumentResolver; |
30
|
|
|
|
31
|
|
|
private $requestAttributesFactory; |
32
|
|
|
|
33
|
|
|
private $resolver; |
34
|
|
|
|
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
$this->controllerArgumentResolver = $this->createMock(ControllerArgumentResolverInterface::class); |
38
|
|
|
|
39
|
|
|
$this->requestAttributesFactory = $this->createMock(RequestAttributesFactoryInterface::class); |
40
|
|
|
|
41
|
|
|
$this->resolver = new SensioExtraResolver($this->controllerArgumentResolver, $this->requestAttributesFactory); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testSupports() |
45
|
|
|
{ |
46
|
|
|
$test = new SensioExtraTest(['ROLE_USER']); |
47
|
|
|
|
48
|
|
|
$this->assertTrue($this->resolver->supports($test)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testResolve() |
52
|
|
|
{ |
53
|
|
|
$routeContext = new RouteContext('index'); |
54
|
|
|
|
55
|
|
|
$resolved = $this->resolver->resolve(new SensioExtraTest(['ROLE_USER']), $routeContext); |
56
|
|
|
|
57
|
|
|
$this->assertSame([['ROLE_USER'], null], $resolved); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testResolveSubjectVariable() |
61
|
|
|
{ |
62
|
|
|
$test = new SensioExtraTest(['ROLE_USER'], 'foo'); |
63
|
|
|
|
64
|
|
|
$routeContext = new RouteContext('index'); |
65
|
|
|
|
66
|
|
|
$this->controllerArgumentResolver->method('getArgument') |
67
|
|
|
->with($routeContext, 'foo') |
68
|
|
|
->willReturn(5); |
69
|
|
|
|
70
|
|
|
$resolved = $this->resolver->resolve($test, $routeContext); |
71
|
|
|
|
72
|
|
|
$this->assertSame([['ROLE_USER'], 5], $resolved); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testResolveSubjectVariableException() |
76
|
|
|
{ |
77
|
|
|
$test = new SensioExtraTest(['ROLE_USER'], 'foo'); |
78
|
|
|
|
79
|
|
|
$routeContext = new RouteContext('index'); |
80
|
|
|
|
81
|
|
|
$this->controllerArgumentResolver->method('getArgument') |
82
|
|
|
->with($routeContext, 'foo') |
83
|
|
|
->willThrowException(new RuntimeException('Inner exception.')); |
84
|
|
|
|
85
|
|
|
$this->expectException(RuntimeException::class); |
86
|
|
|
$this->expectExceptionMessage('Cannot resolve subject variable "foo". Inner exception.'); |
87
|
|
|
|
88
|
|
|
$this->resolver->resolve($test, $routeContext); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testResolveExpressionVariables() |
92
|
|
|
{ |
93
|
|
|
$expression = new ExpressionDecorator(new Expression('foo == true'), ['foo']); |
94
|
|
|
|
95
|
|
|
$test = new SensioExtraTest([$expression]); |
96
|
|
|
|
97
|
|
|
$routeContext = new RouteContext('index'); |
98
|
|
|
|
99
|
|
|
$this->controllerArgumentResolver->method('getArgument') |
100
|
|
|
->with($routeContext, 'foo') |
101
|
|
|
->willReturn(5); |
102
|
|
|
|
103
|
|
|
$resolved = $this->resolver->resolve($test, $routeContext); |
104
|
|
|
|
105
|
|
|
$this->assertSame([[$expression], null], $resolved); |
106
|
|
|
$this->assertEquals(['foo' => 5], $expression->getVariables()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testResolveExpressionVariablesException() |
110
|
|
|
{ |
111
|
|
|
$expression = new ExpressionDecorator(new Expression('foo == true'), ['foo']); |
112
|
|
|
|
113
|
|
|
$test = new SensioExtraTest([$expression]); |
114
|
|
|
|
115
|
|
|
$routeContext = new RouteContext('index'); |
116
|
|
|
|
117
|
|
|
$this->controllerArgumentResolver->method('getArgument') |
118
|
|
|
->with($routeContext, 'foo') |
119
|
|
|
->willThrowException(new RuntimeException('Inner exception.')); |
120
|
|
|
|
121
|
|
|
$this->expectException(RuntimeException::class); |
122
|
|
|
$this->expectExceptionMessage('Cannot resolve expression variable "foo" of expression "foo == true". Inner exception.'); |
123
|
|
|
|
124
|
|
|
$this->resolver->resolve($test, $routeContext); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testResolveVariableFromRequestAttributes() |
128
|
|
|
{ |
129
|
|
|
$test = new SensioExtraTest(['ROLE_USER'], 'foo'); |
130
|
|
|
$test->setMetadata('request_attributes', ['foo']); |
131
|
|
|
|
132
|
|
|
$routeContext = new RouteContext('index'); |
133
|
|
|
|
134
|
|
|
$this->requestAttributesFactory->method('createAttributes') |
135
|
|
|
->with($routeContext) |
136
|
|
|
->willReturn(new ParameterBag(['foo' => 5])); |
137
|
|
|
|
138
|
|
|
$resolved = $this->resolver->resolve($test, $routeContext); |
139
|
|
|
|
140
|
|
|
$this->assertSame([['ROLE_USER'], 5], $resolved); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testResolveVariableFromRequestAttributesException() |
144
|
|
|
{ |
145
|
|
|
$test = new SensioExtraTest(['ROLE_USER'], 'foo'); |
146
|
|
|
$test->setMetadata('request_attributes', ['foo']); |
147
|
|
|
|
148
|
|
|
$routeContext = new RouteContext('index'); |
149
|
|
|
|
150
|
|
|
$this->requestAttributesFactory->method('createAttributes') |
151
|
|
|
->with($routeContext) |
152
|
|
|
->willReturn(new ParameterBag()); |
153
|
|
|
|
154
|
|
|
$this->expectException(RuntimeException::class); |
155
|
|
|
$this->expectExceptionMessage('Cannot resolve subject variable "foo" directly from Request attributes.'); |
156
|
|
|
|
157
|
|
|
$this->resolver->resolve($test, $routeContext); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|