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\Twig\Node; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Twig\Node\Node; |
15
|
|
|
use Twig\Node\Expression\ArrayExpression; |
16
|
|
|
use Twig\Node\Expression\ConstantExpression; |
17
|
|
|
use Twig\Error\SyntaxError; |
18
|
|
|
use Yarhon\RouteGuardBundle\Twig\Node\RouteExpression; |
19
|
|
|
|
20
|
|
|
class RouteExpressionTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @dataProvider constructorDataProvider |
24
|
|
|
*/ |
25
|
|
|
public function testConstructor($sourceArguments, $expectedArguments) |
26
|
|
|
{ |
27
|
|
|
$expression = new RouteExpression($sourceArguments); |
28
|
|
|
$arguments = $expression->getNode('arguments'); |
29
|
|
|
|
30
|
|
|
// Remove the "generateAs" node, as it's tested in separate method. |
31
|
|
|
$arguments->removeNode(3); |
32
|
|
|
|
33
|
|
|
$this->assertEquals($expectedArguments, $arguments); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function constructorDataProvider() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
[ |
40
|
|
|
new Node([ |
41
|
|
|
new ConstantExpression('secure1', 0), |
42
|
|
|
]), |
43
|
|
|
new Node([ |
44
|
|
|
new ConstantExpression('secure1', 0), |
45
|
|
|
new ArrayExpression([], 0), |
46
|
|
|
new ConstantExpression('GET', 0), |
47
|
|
|
]), |
48
|
|
|
], |
49
|
|
|
[ |
50
|
|
|
new Node([ |
51
|
|
|
new ConstantExpression('secure1', 0), |
52
|
|
|
new ArrayExpression([ |
53
|
|
|
new ConstantExpression('page', 0), |
54
|
|
|
new ConstantExpression(10, 0), |
55
|
|
|
], 0), |
56
|
|
|
]), |
57
|
|
|
new Node([ |
58
|
|
|
new ConstantExpression('secure1', 0), |
59
|
|
|
new ArrayExpression([ |
60
|
|
|
new ConstantExpression('page', 0), |
61
|
|
|
new ConstantExpression(10, 0), |
62
|
|
|
], 0), |
63
|
|
|
new ConstantExpression('GET', 0), |
64
|
|
|
]), |
65
|
|
|
], |
66
|
|
|
[ |
67
|
|
|
new Node([ |
68
|
|
|
new ConstantExpression('secure1', 0), |
69
|
|
|
new ArrayExpression([], 0), |
70
|
|
|
new ConstantExpression('POST', 0), |
71
|
|
|
]), |
72
|
|
|
new Node([ |
73
|
|
|
new ConstantExpression('secure1', 0), |
74
|
|
|
new ArrayExpression([], 0), |
75
|
|
|
new ConstantExpression('POST', 0), |
76
|
|
|
]), |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @dataProvider constructorExceptionDataProvider |
83
|
|
|
*/ |
84
|
|
|
public function testConstructorException($sourceArguments, $expected) |
85
|
|
|
{ |
86
|
|
|
$this->expectException($expected[0]); |
87
|
|
|
if (isset($expected[1])) { |
88
|
|
|
$this->expectExceptionMessage($expected[1]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$expression = new RouteExpression($sourceArguments); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function constructorExceptionDataProvider() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
[ |
98
|
|
|
new Node([]), |
99
|
|
|
[SyntaxError::class, 'At least one argument (name) is required.'], |
100
|
|
|
], |
101
|
|
|
[ |
102
|
|
|
new Node([ |
103
|
|
|
new ConstantExpression('secure1', 0), |
104
|
|
|
new ArrayExpression([], 0), |
105
|
|
|
new ConstantExpression('POST', 0), |
106
|
|
|
new ConstantExpression(false, 0), |
107
|
|
|
]), |
108
|
|
|
[SyntaxError::class, 'Unrecognized extra arguments, only 3 (name, parameters, method) allowed.'], |
109
|
|
|
], |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testDefaultGenerateAs() |
114
|
|
|
{ |
115
|
|
|
$expression = new RouteExpression(new Node([ |
116
|
|
|
new ConstantExpression('secure1', 0), |
117
|
|
|
])); |
118
|
|
|
|
119
|
|
|
$arguments = $expression->getNode('arguments'); |
120
|
|
|
$this->assertTrue($arguments->hasNode(3)); |
121
|
|
|
$generateAsNode = $arguments->getNode(3); |
122
|
|
|
|
123
|
|
|
$expected = new ArrayExpression([ |
124
|
|
|
new ConstantExpression(0, 0), |
125
|
|
|
new ConstantExpression('path', 0), |
126
|
|
|
new ConstantExpression(1, 0), |
127
|
|
|
new ConstantExpression(false, 0), |
128
|
|
|
], 0); |
129
|
|
|
|
130
|
|
|
$this->assertEquals($generateAsNode, $expected); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @dataProvider setGenerateAsDataProvider |
135
|
|
|
*/ |
136
|
|
|
public function testSetGenerateAs($generateAs, $expected) |
137
|
|
|
{ |
138
|
|
|
$expression = new RouteExpression(new Node([ |
139
|
|
|
new ConstantExpression('secure1', 0), |
140
|
|
|
])); |
141
|
|
|
|
142
|
|
|
$self = $expression->setGenerateAs(...$generateAs); |
|
|
|
|
143
|
|
|
|
144
|
|
|
$this->assertSame($expression, $self); |
145
|
|
|
|
146
|
|
|
$arguments = $expression->getNode('arguments'); |
147
|
|
|
$this->assertTrue($arguments->hasNode(3)); |
148
|
|
|
$generateAsNode = $arguments->getNode(3); |
149
|
|
|
|
150
|
|
|
$this->assertEquals($generateAsNode, $expected); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setGenerateAsDataProvider() |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
[ |
157
|
|
|
['path'], |
158
|
|
|
new ArrayExpression([ |
159
|
|
|
new ConstantExpression(0, 0), |
160
|
|
|
new ConstantExpression('path', 0), |
161
|
|
|
new ConstantExpression(1, 0), |
162
|
|
|
new ConstantExpression(false, 0), |
163
|
|
|
], 0), |
164
|
|
|
], |
165
|
|
|
[ |
166
|
|
|
['path', false], |
167
|
|
|
new ArrayExpression([ |
168
|
|
|
new ConstantExpression(0, 0), |
169
|
|
|
new ConstantExpression('path', 0), |
170
|
|
|
new ConstantExpression(1, 0), |
171
|
|
|
new ConstantExpression(false, 0), |
172
|
|
|
], 0), |
173
|
|
|
], |
174
|
|
|
[ |
175
|
|
|
['path', true], |
176
|
|
|
new ArrayExpression([ |
177
|
|
|
new ConstantExpression(0, 0), |
178
|
|
|
new ConstantExpression('path', 0), |
179
|
|
|
new ConstantExpression(1, 0), |
180
|
|
|
new ConstantExpression(true, 0), |
181
|
|
|
], 0), |
182
|
|
|
], |
183
|
|
|
[ |
184
|
|
|
['url', false], |
185
|
|
|
new ArrayExpression([ |
186
|
|
|
new ConstantExpression(0, 0), |
187
|
|
|
new ConstantExpression('url', 0), |
188
|
|
|
new ConstantExpression(1, 0), |
189
|
|
|
new ConstantExpression(false, 0), |
190
|
|
|
], 0), |
191
|
|
|
], |
192
|
|
|
[ |
193
|
|
|
['url', true], |
194
|
|
|
new ArrayExpression([ |
195
|
|
|
new ConstantExpression(0, 0), |
196
|
|
|
new ConstantExpression('url', 0), |
197
|
|
|
new ConstantExpression(1, 0), |
198
|
|
|
new ConstantExpression(true, 0), |
199
|
|
|
], 0), |
200
|
|
|
], |
201
|
|
|
[ |
202
|
|
|
['url', new ConstantExpression(5, 0)], |
203
|
|
|
new ArrayExpression([ |
204
|
|
|
new ConstantExpression(0, 0), |
205
|
|
|
new ConstantExpression('url', 0), |
206
|
|
|
new ConstantExpression(1, 0), |
207
|
|
|
new ConstantExpression(5, 0), |
208
|
|
|
], 0), |
209
|
|
|
], |
210
|
|
|
]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @dataProvider setGenerateAsExceptionDataProvider |
215
|
|
|
*/ |
216
|
|
|
public function testSetGenerateAsException($generateAs, $expected) |
217
|
|
|
{ |
218
|
|
|
$expression = new RouteExpression(new Node([ |
219
|
|
|
new ConstantExpression('secure1', 0), |
220
|
|
|
])); |
221
|
|
|
|
222
|
|
|
$this->expectException($expected[0]); |
223
|
|
|
$this->expectExceptionMessage($expected[1]); |
224
|
|
|
|
225
|
|
|
$expression->setGenerateAs(...$generateAs); |
|
|
|
|
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function setGenerateAsExceptionDataProvider() |
229
|
|
|
{ |
230
|
|
|
return [ |
231
|
|
|
[ |
232
|
|
|
['foo'], |
233
|
|
|
[SyntaxError::class, 'Invalid reference type: foo'], |
234
|
|
|
], |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|