Refactor duplication out of Mustache injection test.
This commit is contained in:
@@ -23,105 +23,58 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
|
|||||||
$this->mustache = new Mustache_Engine;
|
$this->mustache = new Mustache_Engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
// interpolation
|
/**
|
||||||
|
* @dataProvider injectionData
|
||||||
public function testInterpolationInjection()
|
*/
|
||||||
|
public function testInjection($tpl, $data, $partials, $expect)
|
||||||
{
|
{
|
||||||
$tpl = $this->mustache->loadTemplate('{{ a }}');
|
$this->mustache->setPartials($partials);
|
||||||
|
$this->assertEquals($expect, $this->mustache->render($tpl, $data));
|
||||||
|
}
|
||||||
|
|
||||||
$data = array(
|
public function injectionData()
|
||||||
|
{
|
||||||
|
$interpolationData = array(
|
||||||
'a' => '{{ b }}',
|
'a' => '{{ b }}',
|
||||||
'b' => 'FAIL'
|
'b' => 'FAIL'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('{{ b }}', $tpl->render($data));
|
$sectionData = array(
|
||||||
}
|
|
||||||
|
|
||||||
public function testUnescapedInterpolationInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{{ a }}}');
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => '{{ b }}',
|
|
||||||
'b' => 'FAIL'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals('{{ b }}', $tpl->render($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
// sections
|
|
||||||
|
|
||||||
public function testSectionInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{# a }}{{ b }}{{/ a }}');
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => true,
|
'a' => true,
|
||||||
'b' => '{{ c }}',
|
'b' => '{{ c }}',
|
||||||
'c' => 'FAIL'
|
'c' => 'FAIL'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('{{ c }}', $tpl->render($data));
|
$partialData = array(
|
||||||
}
|
|
||||||
|
|
||||||
public function testUnescapedSectionInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{# a }}{{{ b }}}{{/ a }}');
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => true,
|
|
||||||
'b' => '{{ c }}',
|
|
||||||
'c' => 'FAIL'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals('{{ c }}', $tpl->render($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
// partials
|
|
||||||
|
|
||||||
public function testPartialInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{> partial }}');
|
|
||||||
$this->mustache->setPartials(array(
|
|
||||||
'partial' => '{{ a }}',
|
|
||||||
));
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => '{{ b }}',
|
'a' => '{{ b }}',
|
||||||
'b' => 'FAIL'
|
'b' => 'FAIL'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('{{ b }}', $tpl->render($data));
|
$lambdaInterpolationData = array(
|
||||||
}
|
|
||||||
|
|
||||||
public function testPartialUnescapedInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{> partial }}');
|
|
||||||
$this->mustache->setPartials(array(
|
|
||||||
'partial' => '{{{ a }}}',
|
|
||||||
));
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => '{{ b }}',
|
|
||||||
'b' => 'FAIL'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals('{{ b }}', $tpl->render($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
// lambdas
|
|
||||||
|
|
||||||
public function testLambdaInterpolationInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{ a }}');
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => array($this, 'lambdaInterpolationCallback'),
|
'a' => array($this, 'lambdaInterpolationCallback'),
|
||||||
'b' => '{{ c }}',
|
'b' => '{{ c }}',
|
||||||
'c' => 'FAIL'
|
'c' => 'FAIL'
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals('{{ c }}', $tpl->render($data));
|
$lambdaSectionData = array(
|
||||||
|
'a' => array($this, 'lambdaSectionCallback'),
|
||||||
|
'b' => '{{ c }}',
|
||||||
|
'c' => 'FAIL'
|
||||||
|
);
|
||||||
|
|
||||||
|
return array(
|
||||||
|
array('{{ a }}', $interpolationData, array(), '{{ b }}'),
|
||||||
|
array('{{{ a }}}', $interpolationData, array(), '{{ b }}'),
|
||||||
|
|
||||||
|
array('{{# a }}{{ b }}{{/ a }}', $sectionData, array(), '{{ c }}'),
|
||||||
|
array('{{# a }}{{{ b }}}{{/ a }}', $sectionData, array(), '{{ c }}'),
|
||||||
|
|
||||||
|
array('{{> partial }}', $interpolationData, array('partial' => '{{ a }}'), '{{ b }}'),
|
||||||
|
array('{{> partial }}', $interpolationData, array('partial' => '{{{ a }}}'), '{{ b }}'),
|
||||||
|
|
||||||
|
array('{{ a }}', $lambdaInterpolationData, array(), '{{ c }}'),
|
||||||
|
array('{{# a }}b{{/ a }}', $lambdaSectionData, array(), '{{ c }}'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function lambdaInterpolationCallback()
|
public static function lambdaInterpolationCallback()
|
||||||
@@ -129,19 +82,6 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T
|
|||||||
return '{{ b }}';
|
return '{{ b }}';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testLambdaSectionInjection()
|
|
||||||
{
|
|
||||||
$tpl = $this->mustache->loadTemplate('{{# a }}b{{/ a }}');
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'a' => array($this, 'lambdaSectionCallback'),
|
|
||||||
'b' => '{{ c }}',
|
|
||||||
'c' => 'FAIL'
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertEquals('{{ c }}', $tpl->render($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function lambdaSectionCallback($text)
|
public static function lambdaSectionCallback($text)
|
||||||
{
|
{
|
||||||
return '{{ ' . $text . ' }}';
|
return '{{ ' . $text . ' }}';
|
||||||
|
|||||||
Reference in New Issue
Block a user