]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/zordius/lightncandy/tests/errorTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / zordius / lightncandy / tests / errorTest.php
1 <?php
2
3 require_once('src/lightncandy.php');
4 require_once('tests/helpers_for_test.php');
5
6 $tmpdir = sys_get_temp_dir();
7 $errlog_fn = tempnam($tmpdir, 'terr_');
8
9 function start_catch_error_log() {
10     global $errlog_fn;
11     date_default_timezone_set('GMT');
12     if (file_exists($errlog_fn)) {
13         unlink($errlog_fn);
14     }
15     return ini_set('error_log', $errlog_fn);
16 }
17
18 function stop_catch_error_log() {
19     global $errlog_fn;
20     ini_restore('error_log');
21     if (!file_exists($errlog_fn)) {
22         return null;
23     }
24     return array_map(function ($l) {
25         $l = rtrim($l);
26         preg_match('/GMT\] (.+)/', $l, $m);
27         return isset($m[1]) ? $m[1] : $l;
28     }, file($errlog_fn));
29 }
30
31 class errorTest extends PHPUnit_Framework_TestCase
32 {
33     public function testException()
34     {
35         $this->setExpectedException('Exception', 'Bad token {{{foo}} ! Do you mean {{foo}} or {{{foo}}}?');
36         $php = LightnCandy::compile('{{{foo}}', Array('flags' => LightnCandy::FLAG_ERROR_EXCEPTION));
37     }
38
39     public function testErrorLog()
40     {
41         start_catch_error_log();
42         $php = LightnCandy::compile('{{{foo}}', Array('flags' => LightnCandy::FLAG_ERROR_LOG));
43         $e = stop_catch_error_log();
44         if ($e) {
45             $this->assertEquals(Array('Bad token {{{foo}} ! Do you mean {{foo}} or {{{foo}}}?'), $e);
46         } else {
47             $this->markTestIncomplete('skip HHVM');
48         }
49     }
50
51     /**
52      * @dataProvider renderErrorProvider
53      */
54     public function testRenderingException($test)
55     {
56         $this->setExpectedException('Exception', $test['expected']);
57         $php = LightnCandy::compile($test['template'], $test['options']);
58         $renderer = LightnCandy::prepare($php);
59         $renderer(null, LCRun3::DEBUG_ERROR_EXCEPTION);
60     }
61
62     /**
63      * @dataProvider renderErrorProvider
64      */
65     public function testRenderingErrorLog($test)
66     {
67         start_catch_error_log();
68         $php = LightnCandy::compile($test['template'], $test['options']);
69         $renderer = LightnCandy::prepare($php);
70         $renderer(null, LCRun3::DEBUG_ERROR_LOG);
71         $e = stop_catch_error_log();
72         if ($e) {
73             $this->assertEquals(Array($test['expected']), $e);
74         } else {
75             $this->markTestIncomplete('skip HHVM');
76         }
77     }
78
79     public function renderErrorProvider()
80     {
81         $errorCases = Array(
82              Array(
83                  'template' => '{{{foo}}}',
84                  'expected' => 'LCRun3: [foo] is not exist',
85              ),
86              Array(
87                  'template' => '{{foo}}',
88                  'options' => Array(
89                      'hbhelpers' => Array(
90                          'foo' => function () {
91                              return 1/0;
92                          }
93                      ),
94                  ),
95                  'expected' => 'LCRun3: call custom helper \'foo\' error: Division by zero',
96              ),
97         );
98
99         return array_map(function($i) {
100             if (!isset($i['options'])) {
101                 $i['options'] = Array('flags' => LightnCandy::FLAG_RENDER_DEBUG);
102             }
103             if (!isset($i['options']['flags'])) {
104                 $i['options']['flags'] = LightnCandy::FLAG_RENDER_DEBUG;
105             }
106             return Array($i);
107         }, $errorCases);
108     }
109
110     /**
111      * @dataProvider errorProvider
112      */
113     public function testErrors($test)
114     {
115         global $tmpdir;
116
117         $php = LightnCandy::compile($test['template'], $test['options']);
118         $context = LightnCandy::getContext();
119
120         // This case should be compiled without error
121         if (!isset($test['expected'])) {
122             return;
123         }
124
125         $this->assertEquals($test['expected'], $context['error'], "Code: $php");
126     }
127
128     public function errorProvider()
129     {
130         $errorCases = Array(
131             Array(
132                 'template' => '{{testerr1}}}',
133                 'expected' => 'Bad token {{testerr1}}} ! Do you mean {{testerr1}} or {{{testerr1}}}?',
134             ),
135             Array(
136                 'template' => '{{{testerr2}}',
137                 'expected' => 'Bad token {{{testerr2}} ! Do you mean {{testerr2}} or {{{testerr2}}}?',
138             ),
139             Array(
140                 'template' => '{{{#testerr3}}}',
141                 'expected' => 'Bad token {{{#testerr3}}} ! Do you mean {{#testerr3}} ?',
142             ),
143             Array(
144                 'template' => '{{{!testerr4}}}',
145                 'expected' => 'Bad token {{{!testerr4}}} ! Do you mean {{!testerr4}} ?',
146             ),
147             Array(
148                 'template' => '{{{^testerr5}}}',
149                 'expected' => 'Bad token {{{^testerr5}}} ! Do you mean {{^testerr5}} ?',
150             ),
151             Array(
152                 'template' => '{{{/testerr6}}}',
153                 'expected' => 'Bad token {{{/testerr6}}} ! Do you mean {{/testerr6}} ?',
154             ),
155             Array(
156                 'template' => '{{win[ner.test1}}',
157                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
158                 'expected' => 'Wrong variable naming in {{win[ner.test1}}',
159             ),
160             Array(
161                 'template' => '{{win]ner.test2}}',
162                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
163                 'expected' => 'Wrong variable naming as \'win]ner.test2\' in {{win]ner.test2}} !',
164             ),
165             Array(
166                 'template' => '{{wi[n]ner.test3}}',
167                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
168                 'expected' => 'Wrong variable naming as \'wi[n]ner.test3\' in {{wi[n]ner.test3}} !',
169             ),
170             Array(
171                 'template' => '{{winner].[test4]}}',
172                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
173                 'expected' => 'Wrong variable naming as \'winner].[test4]\' in {{winner].[test4]}} !',
174             ),
175             Array(
176                 'template' => '{{winner[.test5]}}',
177                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
178                 'expected' => 'Wrong variable naming as \'winner[.test5]\' in {{winner[.test5]}} !',
179             ),
180             Array(
181                 'template' => '{{winner.[.test6]}}',
182                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
183             ),
184             Array(
185                 'template' => '{{winner.[#te.st7]}}',
186                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
187             ),
188             Array(
189                 'template' => '{{test8}}',
190                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
191             ),
192             Array(
193                 'template' => '{{test9]}}',
194                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
195                 'expected' => 'Wrong variable naming as \'test9]\' in {{test9]}} !',
196             ),
197             Array(
198                 'template' => '{{testA[}}',
199                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
200                 'expected' => 'Wrong variable naming in {{testA[}}',
201             ),
202             Array(
203                 'template' => '{{[testB}}',
204                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
205                 'expected' => 'Wrong variable naming in {{[testB}}',
206             ),
207             Array(
208                 'template' => '{{]testC}}',
209                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
210                 'expected' => 'Wrong variable naming as \']testC\' in {{]testC}} !',
211             ),
212             Array(
213                 'template' => '{{[testD]}}',
214                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
215             ),
216             Array(
217                 'template' => '{{te]stE}}',
218                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
219                 'expected' => 'Wrong variable naming as \'te]stE\' in {{te]stE}} !',
220             ),
221             Array(
222                 'template' => '{{tee[stF}}',
223                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
224                 'expected' => 'Wrong variable naming in {{tee[stF}}',
225             ),
226             Array(
227                 'template' => '{{te.e[stG}}',
228                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
229                 'expected' => 'Wrong variable naming in {{te.e[stG}}',
230             ),
231             Array(
232                 'template' => '{{te.e]stH}}',
233                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
234                 'expected' => 'Wrong variable naming as \'te.e]stH\' in {{te.e]stH}} !',
235             ),
236             Array(
237                 'template' => '{{te.e[st.endI}}',
238                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
239                 'expected' => 'Wrong variable naming in {{te.e[st.endI}}',
240             ),
241             Array(
242                 'template' => '{{te.e]st.endJ}}',
243                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
244                 'expected' => 'Wrong variable naming as \'te.e]st.endJ\' in {{te.e]st.endJ}} !',
245             ),
246             Array(
247                 'template' => '{{te.[est].endK}}',
248                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
249             ),
250             Array(
251                 'template' => '{{te.t[est].endL}}',
252                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
253                 'expected' => 'Wrong variable naming as \'te.t[est].endL\' in {{te.t[est].endL}} !',
254             ),
255             Array(
256                 'template' => '{{te.t[est]o.endM}}',
257                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
258                 'expected' => 'Wrong variable naming as \'te.t[est]o.endM\' in {{te.t[est]o.endM}} !',
259             ),
260             Array(
261                 'template' => '{{te.[est]o.endN}}',
262                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
263                 'expected' => 'Wrong variable naming as \'te.[est]o.endN\' in {{te.[est]o.endN}} !',
264             ),
265             Array(
266                 'template' => '{{te.[e.st].endO}}',
267                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
268             ),
269             Array(
270                 'template' => '{{te.[e.s[t].endP}}',
271                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
272             ),
273             Array(
274                 'template' => '{{te.[e[s.t].endQ}}',
275                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
276             ),
277             Array(
278                 'template' => '{{helper}}',
279                 'options' => Array('helpers' => Array(
280                     'helper' => Array('bad input'),
281                 )),
282                 'expected' => 'I found an array in helpers with key as helper, please fix it.',
283             ),
284             Array(
285                 'template' => '<ul>{{#each item}}<li>{{name}}</li>',
286                 'expected' => 'Unclosed token {{#each item}} !!',
287             ),
288             Array(
289                 'template' => 'issue63: {{test_join}} Test! {{this}} {{/test_join}}',
290                 'expected' => 'Unexpect token: {{/test_join}} !',
291             ),
292             Array(
293                 'template' => '{{#if a}}TEST{{/with}}',
294                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
295                 'expected' => 'Unexpect token: {{/with}} !',
296             ),
297             Array(
298                 'template' => '{{#foo}}error{{/bar}}',
299                 'expected' => 'Unexpect token {{/bar}} ! Previous token {{#[foo]}} is not closed',
300             ),
301             Array(
302                 'template' => '{{../foo}}',
303                 'expected' => 'Do not support {{../var}}, you should do compile with LightnCandy::FLAG_PARENT flag',
304             ),
305             Array(
306                 'template' => '{{..}}',
307                 'expected' => 'Do not support {{../var}}, you should do compile with LightnCandy::FLAG_PARENT flag',
308             ),
309             Array(
310                 'template' => '{{test_join [a]=b}}',
311                 'options' => Array(
312                     'flags' => LightnCandy::FLAG_NAMEDARG,
313                     'helpers' => Array('test_join')
314                 ),
315                 'expected' => "Wrong argument name as '[a]' in {{test_join [a]=b}} ! You should fix your template or compile with LightnCandy::FLAG_ADVARNAME flag.",
316             ),
317             Array(
318                 'template' => '{{a=b}}',
319                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
320                 'expected' => 'Do not support name=value in {{a=b}}, you should use it after a custom helper.',
321             ),
322             Array(
323                 'template' => '{{test a=b}}',
324                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
325                 'expected' => 'Do not support name=value in {{test a=b}}, maybe you missing the custom helper?',
326             ),
327             Array(
328                 'template' => '{{#test a=b}}YA~{{/test}}',
329                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
330                 'expected' => 'Do not support name=value in {{#test a=b}}, maybe you missing the block custom helper?',
331             ),
332             Array(
333                 'template' => '{{#foo}}1{{^}}2{{/foo}}',
334                 'expected' => 'Do not support {{^}}, you should do compile with LightnCandy::FLAG_ELSE flag',
335             ),
336             Array(
337                 'template' => '{{#with a}OK!{{/with}}',
338                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
339                 'expected' => 'Unclosed token {{#with a}OK!{{/with}} !!',
340             ),
341             Array(
342                 'template' => '{{#each a}OK!{{/each}}',
343                 'expected' => 'Unclosed token {{#each a}OK!{{/each}} !!',
344             ),
345             Array(
346                 'template' => '{{#with items}}OK!{{/with}}',
347                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
348             ),
349             Array(
350                 'template' => '{{#with}}OK!{{/with}}',
351                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
352                 'expected' => 'No argument after {{#with}} !',
353             ),
354             Array(
355                 'template' => '{{>not_found}}',
356                 'expected' => "Can not find partial file for 'not_found', you should set correct basedir and fileext in options",
357             ),
358             Array(
359                 'template' => '{{>tests/test1 foo}}',
360                 'options' => Array('basedir' => '.'),
361                 'expected' => 'Do not support {{>tests/test1 [foo]}}, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag',
362             ),
363             Array(
364                 'template' => '{{#with foo}}ABC{{/with}}',
365                 'expected' => 'Do not support {{#with var}}, you should do compile with LightnCandy::FLAG_WITH flag',
366             ),
367             Array(
368                 'template' => '{{abc}}',
369                 'options' => Array('helpers' => Array('abc')),
370                 'expected' => 'Can not find custom helper function defination abc() !',
371             ),
372             Array(
373                 'template' => '{{=~= =~=}}',
374                 'expected' => "Can not set delimiter contains '=' , you try to set delimiter as '~=' and '=~'.",
375             ),
376             Array(
377                 'template' => '{{>recursive}}',
378                 'options' => Array('basedir' => 'tests', 'flags' => LightnCandy::FLAG_WITH),
379                 'expected' => Array(
380                     'I found recursive partial includes as the path: recursive -> recursive! You should fix your template or compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag.',
381                     "Skip rendering partial 'recursive' again due to recursive detected",
382                 )
383             ),
384             Array(
385                 'template' => '{{test_join (foo bar)}}',
386                 'options' => Array(
387                     'flags' => LightnCandy::FLAG_ADVARNAME,
388                     'helpers' => Array('test_join'),
389                 ),
390                 'expected' => "Can not find custom helper function defination foo() !",
391             ),
392             Array(
393                 'template' => '{{1 + 2}}',
394                 'options' => Array(
395                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
396                     'helpers' => Array('test_join'),
397                 ),
398                 'expected' => "Wrong variable naming as '+' in {{1 + 2}} ! You should wrap ! \" # % & ' * + , ; < = > { | } ~ into [ ]",
399             ),
400             Array(
401                 'template' => '{{> (foo) bar}}',
402                 'options' => Array(
403                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
404                     'basedir' => '.',
405                 ),
406                 'expected' => Array(
407                     "Can not find custom helper function defination foo() !",
408                     "You use dynamic partial name as '(foo)', this only works with option FLAG_RUNTIMEPARTIAL enabled",
409                 )
410             ),
411             Array(
412                 'template' => '{{{{#foo}}}',
413                 'options' => Array(
414                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
415                 ),
416                 'expected' => Array(
417                     'Bad token {{{{#foo}}} ! Do you mean {{{{#foo}}}} ?',
418                     'Wrong raw block begin with {{{{#foo}}} ! Remove "#" to fix this issue.',
419                     'Unclosed token {{{{foo}}}} !!',
420                 )
421             ),
422             Array(
423                 'template' => '{{{{foo}}}} {{ {{{{/bar}}}}',
424                 'options' => Array(
425                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
426                 ),
427                 'expected' => Array(
428                     'Unclosed token {{{{foo}}}} !!',
429                 )
430             ),
431             Array(
432                 'template' => '{{{{foo}}}} {{ {{{{#foo}}}}',
433                 'options' => Array(
434                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
435                 ),
436                 'expected' => Array(
437                     'Unclosed token {{{{foo}}}} !!',
438                 )
439             ),
440         );
441
442         return array_map(function($i) {
443             if (!isset($i['options'])) {
444                 $i['options'] = Array('flags' => 0);
445             }
446             if (!isset($i['options']['flags'])) {
447                 $i['options']['flags'] = 0;
448             }
449             if (isset($i['expected']) && !is_array($i['expected'])) {
450                 $i['expected'] = Array($i['expected']);
451             }
452             return Array($i);
453         }, $errorCases);
454     }
455 }
456
457
458 ?>