|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TwigOverride\Test; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use TwigOverride\Test\Providers\TestRewriteProvider; |
|
7
|
|
|
use TwigOverride\TwigOverrideExtension; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Tests the twig extension and node visitor on a real twig environment. |
|
11
|
|
|
*/ |
|
12
|
|
|
class TwigOverrideFunctionalTest extends TestCase { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A map where keys are template names and values are template contents. |
|
16
|
|
|
* |
|
17
|
|
|
* This contains the "twig file" set that will be used for all tests. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private $testFiles = [ |
|
22
|
|
|
'static1' => 'test1 {{arg1}} {{arg2}}', |
|
23
|
|
|
'static2' => 'test2 {{arg1}} {{arg2}}', |
|
24
|
|
|
'include' => '{% include "static1" %}', |
|
25
|
|
|
'include_with' => '{% include "static1" with { arg2: "value2" } %}', |
|
26
|
|
|
'include_with_only' => '{% include "static1" with { arg2: "value2" } only %}', |
|
27
|
|
|
'embed' => '{% embed "static1" %}{% endembed %}', |
|
28
|
|
|
'embed_with' => '{% embed "static1" with { arg2: "value2" } %}{% endembed %}', |
|
29
|
|
|
'embed_with_only' => '{% embed "static1" with { arg2: "value2" } only %}{% endembed %}', |
|
30
|
|
|
'extends' => '{% extends "static1" %}', |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Tests rendering a file with a given set of rewrites and execution context. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $file_name |
|
37
|
|
|
* The twig template to render. This should be one of the keys in the |
|
38
|
|
|
* $testFiles property. |
|
39
|
|
|
* @param bool $use_template_rewrites |
|
40
|
|
|
* TRUE to test with template rewriting enabled, FALSE to disable. |
|
41
|
|
|
* @param bool $use_param_rewrites |
|
42
|
|
|
* TRUE to test with param rewriting enabled, FALSE to disable. |
|
43
|
|
|
* @param string $expected_output |
|
44
|
|
|
* The expected rendered twig result. |
|
45
|
|
|
* |
|
46
|
|
|
* @dataProvider provideTwigExamples |
|
47
|
|
|
*/ |
|
48
|
|
|
public function testTwigExamples($file_name, $use_template_rewrites, $use_param_rewrites, $expected_output) { |
|
49
|
|
|
$_context = ['arg1' => 'value1']; |
|
50
|
|
|
$file_rewrites = $use_template_rewrites ? ['static1' => 'static2'] : []; |
|
51
|
|
|
$param_rewrites = $use_template_rewrites ? ['arg2' => 'value3'] : []; |
|
52
|
|
|
$twig = $this->createTwig($file_rewrites, $param_rewrites); |
|
53
|
|
|
$actual_output = $twig->render($file_name, $_context); |
|
54
|
|
|
$this->assertEquals($expected_output, $actual_output); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Provides test cases for testTwigExamples. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
* See PHPUnit docs for info about data providers. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function provideTwigExamples() { |
|
64
|
|
|
return [ |
|
65
|
|
|
['include', FALSE, FALSE, 'test1 value1 '], |
|
66
|
|
|
['include', TRUE, FALSE, 'test2 value1 '], |
|
67
|
|
|
['include_with', FALSE, FALSE, 'test1 value1 value2'], |
|
68
|
|
|
['include_with', TRUE, TRUE, 'test2 value1 value3'], |
|
69
|
|
|
['include_with_only', FALSE, FALSE, 'test1 value2'], |
|
70
|
|
|
['include_with_only', TRUE, TRUE, 'test2 value3'], |
|
71
|
|
|
['embed', FALSE, FALSE, 'test1 value1 '], |
|
72
|
|
|
['embed', TRUE, FALSE, 'test2 value1 '], |
|
73
|
|
|
['embed_with', FALSE, FALSE, 'test1 value1 value2'], |
|
74
|
|
|
['embed_with', TRUE, TRUE, 'test2 value1 value3'], |
|
75
|
|
|
['embed_with_only', FALSE, FALSE, 'test1 value2'], |
|
76
|
|
|
['embed_with_only', TRUE, TRUE, 'test2 value3'], |
|
77
|
|
|
['extends', FALSE, FALSE, 'test1 value1 '], |
|
78
|
|
|
['extends', TRUE, FALSE, 'test2 value1 '], |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Creates a live twig execution environment for testing. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $file_rewrites |
|
86
|
|
|
* A map where keys are 'from' file names, and values are 'to' file names. |
|
87
|
|
|
* @param array $param_rewrites |
|
88
|
|
|
* A map where keys are 'from' param names, and values are 'to' param names. |
|
89
|
|
|
* |
|
90
|
|
|
* @return \Twig_Environment |
|
91
|
|
|
* The generated twig execution environment. |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function createTwig(array $file_rewrites, array $param_rewrites) { |
|
94
|
|
|
$twig = new \Twig_Environment(new \Twig_Loader_Array($this->testFiles)); |
|
95
|
|
|
$twig->addExtension(new TwigOverrideExtension([ |
|
96
|
|
|
new TestRewriteProvider($file_rewrites, $param_rewrites), |
|
97
|
|
|
])); |
|
98
|
|
|
return $twig; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|