1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chris\Bundle\FrontRenderBundle\Tests\Render; |
4
|
|
|
|
5
|
|
|
use Chris\Bundle\FrontRenderBundle\Listener\TwigListener; |
6
|
|
|
use Chris\Bundle\FrontRenderBundle\Render\FrontRender; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Bundle\TwigBundle\TwigEngine; |
9
|
|
|
use Symfony\Component\Config\FileLocator; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
11
|
|
|
use Symfony\Component\Templating\TemplateNameParser; |
12
|
|
|
use Twig_Error_Syntax; |
13
|
|
|
|
14
|
|
|
class FrontRenderTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string TEMPLATE_PATH |
18
|
|
|
*/ |
19
|
|
|
const TEMPLATE_PATH = 'Tests/Template'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string TEMPLATE |
23
|
|
|
*/ |
24
|
|
|
const TEMPLATE = 'index.html.twig'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string FAILED_TEMPLATE |
28
|
|
|
*/ |
29
|
|
|
const FAILED_TEMPLATE = 'failed.html.twig'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string FAILED_PATH |
33
|
|
|
*/ |
34
|
|
|
const FAILED_PATH = 'failedPath.html.twig'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EventDispatcher |
38
|
|
|
*/ |
39
|
|
|
protected $eventDispatcher; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \Twig_Loader_Filesystem |
43
|
|
|
*/ |
44
|
|
|
protected $twigLoader; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \Twig_Environment |
48
|
|
|
*/ |
49
|
|
|
protected $twigEnvironment; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var TwigListener |
53
|
|
|
*/ |
54
|
|
|
protected $twigListener; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var TemplateNameParser |
58
|
|
|
*/ |
59
|
|
|
protected $templateNameParser; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var FileLocator |
63
|
|
|
*/ |
64
|
|
|
protected $fileLocator; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var TwigEngine |
68
|
|
|
*/ |
69
|
|
|
protected $engine; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var FrontRender |
73
|
|
|
*/ |
74
|
|
|
protected $frontRender; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set up the front render test |
78
|
|
|
*/ |
79
|
|
|
public function setUp() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
parent::setUp(); |
82
|
|
|
|
83
|
|
|
$this->eventDispatcher = \Phake::mock(EventDispatcher::class); |
|
|
|
|
84
|
|
|
$this->twigLoader = \Phake::partialMock(\Twig_Loader_Filesystem::class, [$_SERVER['KERNEL_DIR'] . self::TEMPLATE_PATH]); |
|
|
|
|
85
|
|
|
$this->twigEnvironment = \Phake::partialMock(\Twig_Environment::class, $this->twigLoader); |
|
|
|
|
86
|
|
|
$this->twigListener = \Phake::partialMock(TwigListener::class, $this->twigEnvironment); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->templateNameParser = \Phake::mock(TemplateNameParser::class); |
|
|
|
|
89
|
|
|
$this->fileLocator = \Phake::mock(FileLocator::class); |
|
|
|
|
90
|
|
|
$this->engine = \Phake::partialMock(TwigEngine::class, $this->twigEnvironment, $this->templateNameParser, $this->fileLocator); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$lexer = $this->twigListener->getLexer(); |
|
|
|
|
93
|
|
|
$this->twigEnvironment->setLexer($lexer); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Test render of the template with parameter |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function testTemplateRenderWithParameter() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE); |
102
|
|
|
|
103
|
|
|
$this->frontRender->setParameters( |
104
|
|
|
[ |
105
|
|
|
'applicationName' => 'Test Render', |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$this->assertContains( |
110
|
|
|
'Welcome on Test Render', |
111
|
|
|
$this->frontRender->render() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test render of the template without parameter |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function testTemplateRenderWithoutParameter() |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE); |
121
|
|
|
|
122
|
|
|
$this->assertNotContains( |
123
|
|
|
'Test Render', |
124
|
|
|
$this->frontRender->render() |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Test on the update of lexer |
130
|
|
|
* |
131
|
|
|
* @dataProvider lexerTags |
132
|
|
|
* |
133
|
|
|
* @param string $oldTags |
134
|
|
|
* @param string $newTags |
135
|
|
|
*/ |
136
|
|
|
public function testSetLexer($oldTags, $newTags) |
137
|
|
|
{ |
138
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::TEMPLATE); |
139
|
|
|
|
140
|
|
|
$this->frontRender->setParameters( |
141
|
|
|
[ |
142
|
|
|
'applicationName' => 'Test Render', |
143
|
|
|
] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$render = $this->frontRender->render(); |
147
|
|
|
|
148
|
|
|
$this->assertContains( |
149
|
|
|
$oldTags, |
150
|
|
|
$render |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->assertNotContains( |
154
|
|
|
$newTags, |
155
|
|
|
$render |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Test on the failed template |
161
|
|
|
* |
162
|
|
|
* @expectedException Twig_Error_Syntax |
163
|
|
|
*/ |
164
|
|
|
public function testExceptionOnSyntax() |
165
|
|
|
{ |
166
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::FAILED_TEMPLATE); |
167
|
|
|
|
168
|
|
|
$this->frontRender->render(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Test when the template doesn't exist |
173
|
|
|
* |
174
|
|
|
* @expectedException InvalidArgumentException |
175
|
|
|
*/ |
176
|
|
|
public function testExceptionOnFailedPath() |
177
|
|
|
{ |
178
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, self::FAILED_PATH); |
179
|
|
|
|
180
|
|
|
$this->frontRender->render(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Test when the template path is empty |
185
|
|
|
* |
186
|
|
|
* @expectedException Chris\Bundle\FrontRenderBundle\Exception\FrontRenderException |
187
|
|
|
*/ |
188
|
|
|
public function testExceptionOnEmptyPath() |
189
|
|
|
{ |
190
|
|
|
$this->frontRender = new FrontRender($this->engine, $this->eventDispatcher, ''); |
191
|
|
|
|
192
|
|
|
$this->frontRender->render(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Provide the lexer tags |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function lexerTags() |
201
|
|
|
{ |
202
|
|
|
return [ |
203
|
|
|
['{#', '{*'], |
204
|
|
|
['{%', '{@'], |
205
|
|
|
['{{', '{$'] |
206
|
|
|
]; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: