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