|
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\ExpressionLanguage; |
|
12
|
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage; |
|
15
|
|
|
use Yarhon\RouteGuardBundle\ExpressionLanguage\ExpressionAnalyzer; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class ExpressionAnalyzerTest extends TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
private $expressionLanguage; |
|
23
|
|
|
|
|
24
|
|
|
private $analyzer; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->expressionLanguage = new ExpressionLanguage(); |
|
29
|
|
|
$this->analyzer = new ExpressionAnalyzer(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @dataProvider getUsedVariablesDataProvider |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testGetUsedVariables($expression, $allowedVariables, $expected) |
|
36
|
|
|
{ |
|
37
|
|
|
$parsedExpression = $this->expressionLanguage->parse($expression, $allowedVariables); |
|
38
|
|
|
|
|
39
|
|
|
$usedVariables = $this->analyzer->getUsedVariables($parsedExpression); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertEquals($expected, $usedVariables); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getUsedVariablesDataProvider() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
['request.getMethod == "GET" and (foo == true or bar == { i: "val" })', ['request', 'foo', 'bar'], ['request', 'foo', 'bar']], |
|
48
|
|
|
['foo == 5 or foo == 6', ['foo'], ['foo']], |
|
49
|
|
|
['request.getMethod(foo)', ['request', 'foo'], ['request', 'foo']], |
|
50
|
|
|
['5 < 4', ['request'], []], |
|
51
|
|
|
['5 < 4', [], []], |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @dataProvider getVariableAttributesCallsDataProvider |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testGetVariableAttributesCalls($expression, $allowedVariables, $variable, $expected) |
|
59
|
|
|
{ |
|
60
|
|
|
$parsedExpression = $this->expressionLanguage->parse($expression, $allowedVariables); |
|
61
|
|
|
|
|
62
|
|
|
$attributes = $this->analyzer->getVariableAttributesCalls($parsedExpression, $variable); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertEquals($expected, $attributes); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getVariableAttributesCallsDataProvider() |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
['request.getMethod == "GET"', ['request'], 'request', ['getMethod']], |
|
71
|
|
|
['request.getMethod == "GET"', ['request'], 'foo', []], |
|
72
|
|
|
['request.getMethod == "GET" or request.getMethod == "POST"', ['request'], 'request', ['getMethod']], |
|
73
|
|
|
['request == true', ['request'], 'request', []], |
|
74
|
|
|
['request.getFormat(request.getMethod)', ['request'], 'request', ['getFormat', 'getMethod']], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|