compiler = new Dwoo_Compiler(); $this->dwoo = new Dwoo(DWOO_COMPILE_DIR, DWOO_CACHE_DIR); } public function testVarReplacement() { $tpl = new Dwoo_Template_String('{$foo}'); $tpl->forceCompilation(); $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testComplexVarReplacement() { $tpl = new Dwoo_Template_String('{$_root[$a].0}{$_[$a][0]}{$_[$c.d].0}{$_.$a.0}{$_[$c[$x.0]].0}{$_[$c.$y.0].0}'); $tpl->forceCompilation(); $this->assertEquals('cccccc', $this->dwoo->get($tpl, array('a'=>'b', 'x'=>array('d'), 'y'=>'e', 'b'=>array('c','d'),'c'=>array('d'=>'b','e'=>array('b'))), $this->compiler)); } public function testModifier() { $tpl = new Dwoo_Template_String('{$foo|upper}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testModifierArgs() { $tpl = new Dwoo_Template_String('{$foo|spacify:"-"|upper}'); $tpl->forceCompilation(); $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testModifierArgsVars() { $tpl = new Dwoo_Template_String('{$foo|spacify:$bar|upper}'); $tpl->forceCompilation(); $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar', 'bar'=>'-'), $this->compiler)); } public function testModifierOnString() { $tpl = new Dwoo_Template_String('{"bar"|spacify:"-"|upper}'); $tpl->forceCompilation(); $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array(), $this->compiler)); } public function testModifierOnStringWithVar() { $tpl = new Dwoo_Template_String('{"bar"|spacify:$bar|upper}'); $tpl->forceCompilation(); $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('bar'=>'-'), $this->compiler)); } public function testModifierWithModifier() { $tpl = new Dwoo_Template_String('{$foo.0}{assign $foo|reverse foo}{$foo.0}{assign $foo|@reverse foo}{$foo.0}'); $tpl->forceCompilation(); $this->assertEquals('barbazzab', $this->dwoo->get($tpl, array('foo'=>array('bar','baz')), $this->compiler)); } public function testDwooFunc() { $tpl = new Dwoo_Template_String('{upper($foo)}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testDwooLoose() { $tpl = new Dwoo_Template_String('{upper $foo}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testNamedParameter() { $tpl = new Dwoo_Template_String('{upper value=$foo}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testNamedParameter2() { $tpl = new Dwoo_Template_String('{replace value=$foo search="BAR"|lower replace="BAR"}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testNamedParameter3() { $tpl = new Dwoo_Template_String('{assign value=reverse(array(foo=3,boo=5, 3=4), true) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}'); $tpl->forceCompilation(); $this->assertEquals('34boo5foo3', $this->dwoo->get($tpl, array(), $this->compiler)); } public function testMixedParameters() { $tpl = new Dwoo_Template_String('{assign value=array(3, boo=5, 3=4) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}'); $tpl->forceCompilation(); $this->assertEquals('03boo534', $this->dwoo->get($tpl, array(), $this->compiler)); } /** * @expectedException Dwoo_Compilation_Exception */ public function testMixedParametersWrongOrder() { $tpl = new Dwoo_Template_String("{array(boo=5, 3)}"); $tpl->forceCompilation(); $this->dwoo->get($tpl, array(), $this->compiler); } public function testMixedParamsMultiline() { $tpl = new Dwoo_Template_String('{replace( $foo search="BAR"|lower replace="BAR" )}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); $tpl = new Dwoo_Template_String('{replace( $foo search=$bar|lower replace="BAR" )}'); $tpl->forceCompilation(); $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar','bar'=>'BAR'), $this->compiler)); } public function testRecursiveCall() { $tpl = new Dwoo_Template_String('{lower(reverse(upper($foo)))}'); $tpl->forceCompilation(); $this->assertEquals('rab', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler)); } public function testComplexRecursiveCall() { $tpl = new Dwoo_Template_String('{lower reverse($foo|reverse|upper)}'); $tpl->forceCompilation(); $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'BaR'), $this->compiler)); } public function testComplexRecursiveCall2() { $tpl = new Dwoo_Template_String('{str_repeat "AB`$foo|reverse|spacify:o`CD" 3}'); $tpl->forceCompilation(); $this->assertEquals('AB3o2o1CDAB3o2o1CDAB3o2o1CD', $this->dwoo->get($tpl, array('foo'=>'123'), $this->compiler)); } public function testWhitespace() { $tpl = new Dwoo_Template_String("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}"); $tpl->forceCompilation(); $this->assertEquals("aa\na\n\na\n\n\na", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler)); } public function testLiteral() { $tpl = new Dwoo_Template_String('{literal}{$foo}{hurray}{/literal}'); $tpl->forceCompilation(); $this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler)); } /** * @expectedException Dwoo_Compilation_Exception */ public function testUnclosedLiteral() { $tpl = new Dwoo_Template_String('{literal}{$foo}{hurray}'); $tpl->forceCompilation(); $this->dwoo->get($tpl, array(), $this->compiler); } public function testEscaping() { $tpl = new Dwoo_Template_String('\{foo\{bar\\\\{$var}}{"tes}t"}{"foo\"lol\"bar"}'); $tpl->forceCompilation(); $this->assertEquals('{foo{bar\\1}tes}tfoo"lol"bar', $this->dwoo->get($tpl, array('var'=>1), $this->compiler)); } public function testFunctions() { $tpl = new Dwoo_Template_String('{dump()}{dump( )}{dump}'); $tpl->forceCompilation(); $this->assertEquals('