|
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 Twig\Node\Node; |
|
14
|
|
|
use Twig\Error\SyntaxError; |
|
15
|
|
|
use Twig\Error\RuntimeError; |
|
16
|
|
|
use Yarhon\RouteGuardBundle\Tests\Twig\AbstractNodeTest; |
|
17
|
|
|
use Yarhon\RouteGuardBundle\Twig\Node\RouteNode; |
|
18
|
|
|
use Yarhon\RouteGuardBundle\Twig\Node\RouteExpression; |
|
19
|
|
|
|
|
20
|
|
|
class RouteNodeTest extends AbstractNodeTest |
|
21
|
|
|
{ |
|
22
|
|
|
private $tagVariableName = '_route'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @dataProvider compileDataProvider |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testCompile($source, $expected) |
|
28
|
|
|
{ |
|
29
|
|
|
$node = $this->parse($source); |
|
30
|
|
|
$source = $this->compile($node); |
|
31
|
|
|
|
|
32
|
|
|
$conditionNode = $node->getNode('condition'); |
|
33
|
|
|
$conditionSource = $this->compile($conditionNode); |
|
34
|
|
|
$expected = sprintf($expected, $this->tagVariableName, $this->tagVariableName, $conditionSource, $this->tagVariableName); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertEquals($expected, $source); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function compileDataProvider() |
|
40
|
|
|
{ |
|
41
|
|
|
$dataSet = []; |
|
42
|
|
|
|
|
43
|
|
|
// general test |
|
44
|
|
|
$dataSet[0][0] = '{% $tagName "secure1" %}body text{% end$tagName %}'; |
|
45
|
|
|
$dataSet[0][1] = <<<'EOD' |
|
46
|
|
|
$context["%s"] = array(); |
|
47
|
|
|
if (false !== ($context["%s"]["ref"] = %s)) { |
|
48
|
|
|
echo "body text"; |
|
49
|
|
|
} |
|
50
|
|
|
unset($context["%s"]); |
|
51
|
|
|
|
|
52
|
|
|
EOD; |
|
53
|
|
|
// else node test |
|
54
|
|
|
$dataSet[1][] = '{% $tagName "secure1" %}body text{% else %}else text{% end$tagName %}'; |
|
55
|
|
|
$dataSet[1][] = <<<'EOD' |
|
56
|
|
|
$context["%s"] = array(); |
|
57
|
|
|
if (false !== ($context["%s"]["ref"] = %s)) { |
|
58
|
|
|
echo "body text"; |
|
59
|
|
|
} else { |
|
60
|
|
|
echo "else text"; |
|
61
|
|
|
} |
|
62
|
|
|
unset($context["%s"]); |
|
63
|
|
|
|
|
64
|
|
|
EOD; |
|
65
|
|
|
|
|
66
|
|
|
return $dataSet; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testCompileWithoutConditionException() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->expectException(SyntaxError::class); |
|
72
|
|
|
$this->expectExceptionMessage('Condition node is required.'); |
|
73
|
|
|
|
|
74
|
|
|
$node = new RouteNode(null, new Node()); |
|
75
|
|
|
$this->compile($node); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testSetVariableName() |
|
79
|
|
|
{ |
|
80
|
|
|
RouteNode::setVariableName('foo'); |
|
81
|
|
|
|
|
82
|
|
|
$this->assertAttributeEquals('foo', 'variableName', RouteNode::class); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testSetVariableNameException() |
|
86
|
|
|
{ |
|
87
|
|
|
RouteNode::setVariableName(null); |
|
88
|
|
|
|
|
89
|
|
|
$routeExpression = $this->createMock(RouteExpression::class); |
|
90
|
|
|
$node = new RouteNode($routeExpression, new Node()); |
|
91
|
|
|
|
|
92
|
|
|
$this->expectException(RuntimeError::class); |
|
93
|
|
|
$this->expectExceptionMessage('variableName is not set. setVariableName() method should be called before compiling.'); |
|
94
|
|
|
|
|
95
|
|
|
$this->compile($node); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|