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' => '