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