summaryrefslogtreecommitdiff
path: root/vendor/zordius/lightncandy/tests/errorTest.php
blob: 8c1cbbf1b04a901059834c7b0a5013098d2b1f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php

require_once('src/lightncandy.php');
require_once('tests/helpers_for_test.php');

$tmpdir = sys_get_temp_dir();
$errlog_fn = tempnam($tmpdir, 'terr_');

function start_catch_error_log() {
    global $errlog_fn;
    date_default_timezone_set('GMT');
    if (file_exists($errlog_fn)) {
        unlink($errlog_fn);
    }
    return ini_set('error_log', $errlog_fn);
}

function stop_catch_error_log() {
    global $errlog_fn;
    ini_restore('error_log');
    if (!file_exists($errlog_fn)) {
        return null;
    }
    return array_map(function ($l) {
        $l = rtrim($l);
        preg_match('/GMT\] (.+)/', $l, $m);
        return isset($m[1]) ? $m[1] : $l;
    }, file($errlog_fn));
}

class errorTest extends PHPUnit_Framework_TestCase
{
    public function testException()
    {
        $this->setExpectedException('Exception', 'Bad token {{{foo}} ! Do you mean {{foo}} or {{{foo}}}?');
        $php = LightnCandy::compile('{{{foo}}', Array('flags' => LightnCandy::FLAG_ERROR_EXCEPTION));
    }

    public function testErrorLog()
    {
        start_catch_error_log();
        $php = LightnCandy::compile('{{{foo}}', Array('flags' => LightnCandy::FLAG_ERROR_LOG));
        $e = stop_catch_error_log();
        if ($e) {
            $this->assertEquals(Array('Bad token {{{foo}} ! Do you mean {{foo}} or {{{foo}}}?'), $e);
        } else {
            $this->markTestIncomplete('skip HHVM');
        }
    }

    /**
     * @dataProvider renderErrorProvider
     */
    public function testRenderingException($test)
    {
        $this->setExpectedException('Exception', $test['expected']);
        $php = LightnCandy::compile($test['template'], $test['options']);
        $renderer = LightnCandy::prepare($php);
        $renderer(null, LCRun3::DEBUG_ERROR_EXCEPTION);
    }

    /**
     * @dataProvider renderErrorProvider
     */
    public function testRenderingErrorLog($test)
    {
        start_catch_error_log();
        $php = LightnCandy::compile($test['template'], $test['options']);
        $renderer = LightnCandy::prepare($php);
        $renderer(null, LCRun3::DEBUG_ERROR_LOG);
        $e = stop_catch_error_log();
        if ($e) {
            $this->assertEquals(Array($test['expected']), $e);
        } else {
            $this->markTestIncomplete('skip HHVM');
        }
    }

    public function renderErrorProvider()
    {
        $errorCases = Array(
             Array(
                 'template' => '{{{foo}}}',
                 'expected' => 'LCRun3: [foo] is not exist',
             ),
             Array(
                 'template' => '{{foo}}',
                 'options' => Array(
                     'hbhelpers' => Array(
                         'foo' => function () {
                             return 1/0;
                         }
                     ),
                 ),
                 'expected' => 'LCRun3: call custom helper \'foo\' error: Division by zero',
             ),
        );

        return array_map(function($i) {
            if (!isset($i['options'])) {
                $i['options'] = Array('flags' => LightnCandy::FLAG_RENDER_DEBUG);
            }
            if (!isset($i['options']['flags'])) {
                $i['options']['flags'] = LightnCandy::FLAG_RENDER_DEBUG;
            }
            return Array($i);
        }, $errorCases);
    }

    /**
     * @dataProvider errorProvider
     */
    public function testErrors($test)
    {
        global $tmpdir;

        $php = LightnCandy::compile($test['template'], $test['options']);
        $context = LightnCandy::getContext();

        // This case should be compiled without error
        if (!isset($test['expected'])) {
            return;
        }

        $this->assertEquals($test['expected'], $context['error'], "Code: $php");
    }

    public function errorProvider()
    {
        $errorCases = Array(
             Array(
                 'template' => '{{testerr1}}}',
                 'expected' => 'Bad token {{testerr1}}} ! Do you mean {{testerr1}} or {{{testerr1}}}?',
             ),
             Array(
                 'template' => '{{{testerr2}}',
                 'expected' => 'Bad token {{{testerr2}} ! Do you mean {{testerr2}} or {{{testerr2}}}?',
             ),
             Array(
                 'template' => '{{{#testerr3}}}',
                 'expected' => 'Bad token {{{#testerr3}}} ! Do you mean {{#testerr3}} ?',
             ),
             Array(
                 'template' => '{{{!testerr4}}}',
                 'expected' => 'Bad token {{{!testerr4}}} ! Do you mean {{!testerr4}} ?',
             ),
             Array(
                 'template' => '{{{^testerr5}}}',
                 'expected' => 'Bad token {{{^testerr5}}} ! Do you mean {{^testerr5}} ?',
             ),
             Array(
                 'template' => '{{{/testerr6}}}',
                 'expected' => 'Bad token {{{/testerr6}}} ! Do you mean {{/testerr6}} ?',
             ),
             Array(
                 'template' => '{{win[ner.test1}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{win[ner.test1}}',
             ),
             Array(
                 'template' => '{{win]ner.test2}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'win]ner.test2\' in {{win]ner.test2}} !',
             ),
             Array(
                 'template' => '{{wi[n]ner.test3}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'wi[n]ner.test3\' in {{wi[n]ner.test3}} !',
             ),
             Array(
                 'template' => '{{winner].[test4]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'winner].[test4]\' in {{winner].[test4]}} !',
             ),
             Array(
                 'template' => '{{winner[.test5]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'winner[.test5]\' in {{winner[.test5]}} !',
             ),
             Array(
                 'template' => '{{winner.[.test6]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{winner.[#te.st7]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{test8}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{test9]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'test9]\' in {{test9]}} !',
             ),
             Array(
                 'template' => '{{testA[}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{testA[}}',
             ),
             Array(
                 'template' => '{{[testB}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{[testB}}',
             ),
             Array(
                 'template' => '{{]testC}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \']testC\' in {{]testC}} !',
             ),
             Array(
                 'template' => '{{[testD]}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{te]stE}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te]stE\' in {{te]stE}} !',
             ),
             Array(
                 'template' => '{{tee[stF}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{tee[stF}}',
             ),
             Array(
                 'template' => '{{te.e[stG}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{te.e[stG}}',
             ),
             Array(
                 'template' => '{{te.e]stH}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te.e]stH\' in {{te.e]stH}} !',
             ),
             Array(
                 'template' => '{{te.e[st.endI}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming in {{te.e[st.endI}}',
             ),
             Array(
                 'template' => '{{te.e]st.endJ}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te.e]st.endJ\' in {{te.e]st.endJ}} !',
             ),
             Array(
                 'template' => '{{te.[est].endK}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{te.t[est].endL}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te.t[est].endL\' in {{te.t[est].endL}} !',
             ),
             Array(
                 'template' => '{{te.t[est]o.endM}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te.t[est]o.endM\' in {{te.t[est]o.endM}} !',
             ),
             Array(
                 'template' => '{{te.[est]o.endN}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
                 'expected' => 'Wrong variable naming as \'te.[est]o.endN\' in {{te.[est]o.endN}} !',
             ),
             Array(
                 'template' => '{{te.[e.st].endO}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{te.[e.s[t].endP}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{te.[e[s.t].endQ}}',
                 'options' => Array('flags' => LightnCandy::FLAG_ADVARNAME),
             ),
             Array(
                 'template' => '{{helper}}',
                 'options' => Array('helpers' => Array(
                     'helper' => Array('bad input'),
                 )),
                 'expected' => 'I found an array in helpers with key as helper, please fix it.',
             ),
             Array(
                 'template' => '<ul>{{#each item}}<li>{{name}}</li>',
                 'expected' => 'Unclosed token {{{#each item}}} !!',
             ),
             Array(
                 'template' => 'issue63: {{test_join}} Test! {{this}} {{/test_join}}',
                 'expected' => 'Unexpect token: {{/test_join}} !',
             ),
             Array(
                 'template' => '{{#if a}}TEST{{/with}}',
                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
                 'expected' => 'Unexpect token: {{/with}} !',
             ),
             Array(
                 'template' => '{{#foo}}error{{/bar}}',
                 'expected' => 'Unexpect token {{/bar}} ! Previous token {{#[foo]}} is not closed',
             ),
             Array(
                 'template' => '{{../foo}}',
                 'expected' => 'Do not support {{../var}}, you should do compile with LightnCandy::FLAG_PARENT flag',
             ),
             Array(
                 'template' => '{{..}}',
                 'expected' => 'Do not support {{../var}}, you should do compile with LightnCandy::FLAG_PARENT flag',
             ),
             Array(
                 'template' => '{{test_join [a]=b}}',
                 'options' => Array(
                     'flags' => LightnCandy::FLAG_NAMEDARG,
                     'helpers' => Array('test_join')
                 ),
                 'expected' => "Wrong argument name as '[a]' in {{test_join [a]=b}} ! You should fix your template or compile with LightnCandy::FLAG_ADVARNAME flag.",
             ),
             Array(
                 'template' => '{{a=b}}',
                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
                 'expected' => 'Do not support name=value in {{a=b}}, you should use it after a custom helper.',
             ),
             Array(
                 'template' => '{{test a=b}}',
                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
                 'expected' => 'Do not support name=value in {{test a=b}}, maybe you missing the custom helper?',
             ),
             Array(
                 'template' => '{{#test a=b}}YA~{{/test}}',
                 'options' => Array('flags' => LightnCandy::FLAG_NAMEDARG),
                 'expected' => 'Do not support name=value in {{#test a=b}}, maybe you missing the block custom helper?',
             ),
             Array(
                 'template' => '{{#foo}}1{{^}}2{{/foo}}',
                 'expected' => 'Do not support {{^}}, you should do compile with LightnCandy::FLAG_ELSE flag',
             ),
             Array(
                 'template' => '{{#with a}OK!{{/with}}',
                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
                 'expected' => 'Unclosed token {{{#with a}OK!{{/with}}} !!',
             ),
             Array(
                 'template' => '{{#each a}OK!{{/each}}',
                 'expected' => 'Unclosed token {{{#each a}OK!{{/each}}} !!',
             ),
             Array(
                 'template' => '{{#with items}}OK!{{/with}}',
                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
             ),
             Array(
                 'template' => '{{#with}}OK!{{/with}}',
                 'options' => Array('flags' => LightnCandy::FLAG_WITH),
                 'expected' => 'No argument after {{#with}} !',
             ),
             Array(
                 'template' => '{{>not_found}}',
                 'expected' => "Can not find partial file for 'not_found', you should set correct basedir and fileext in options",
             ),
             Array(
                 'template' => '{{>tests/test1 foo}}',
                 'options' => Array('basedir' => '.'),
                 'expected' => 'Do not support {{>tests/test1 [foo]}}, you should do compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag',
             ),
             Array(
                 'template' => '{{#with foo}}ABC{{/with}}',
                 'expected' => 'Do not support {{#with var}}, you should do compile with LightnCandy::FLAG_WITH flag',
             ),
             Array(
                 'template' => '{{abc}}',
                 'options' => Array('helpers' => Array('abc')),
                 'expected' => 'Can not find custom helper function defination abc() !',
             ),
             Array(
                 'template' => '{{=~= =~=}}',
                 'expected' => "Can not set delimiter contains '=' , you try to set delimiter as '~=' and '=~'.",
             ),
             Array(
                 'template' => '{{>recursive}}',
                 'options' => Array('basedir' => 'tests', 'flags' => LightnCandy::FLAG_WITH),
                 'expected' => Array(
                     'I found recursive partial includes as the path: recursive -> recursive! You should fix your template or compile with LightnCandy::FLAG_RUNTIMEPARTIAL flag.',
                     "Skip rendering partial 'recursive' again due to recursive detected",
                 )
             ),
             Array(
                 'template' => '{{test_join (foo bar)}}',
                 'options' => Array(
                     'flags' => LightnCandy::FLAG_ADVARNAME,
                     'helpers' => Array('test_join'),
                 ),
                 'expected' => "Can not find custom helper function defination foo() !",
             ),
             Array(
                 'template' => '{{1 + 2}}',
                 'options' => Array(
                     'flags' => LightnCandy::FLAG_HANDLEBARSJS,
                     'helpers' => Array('test_join'),
                 ),
                 'expected' => "Wrong variable naming as '+' in {{1 + 2}} ! You should wrap ! \" # % & ' * + , ; < = > { | } ~ into [ ]",
             ),
            Array(
                'template' => '{{> (foo) bar}}',
                'options' => Array(
                    'flags' => LightnCandy::FLAG_HANDLEBARSJS,
                    'basedir' => '.',
                ),
                'expected' => Array(
                    "Can not find custom helper function defination foo() !",
                    "You use dynamic partial name as '(foo)', this only works with option FLAG_RUNTIMEPARTIAL enabled",
                )
            ),
        );

        return array_map(function($i) {
            if (!isset($i['options'])) {
                $i['options'] = Array('flags' => 0);
            }
            if (!isset($i['options']['flags'])) {
                $i['options']['flags'] = 0;
            }
            if (isset($i['expected']) && !is_array($i['expected'])) {
                $i['expected'] = Array($i['expected']);
            }
            return Array($i);
        }, $errorCases);
    }
}


?>