Combine FiltersTest and SectionFiltersTest

They’re basically duplicates, so this clears up a bunch of code :)
This commit is contained in:
Justin Hileman
2014-03-25 19:04:48 -07:00
parent 2abbc4b35c
commit ad0204c372
2 changed files with 96 additions and 128 deletions
@@ -15,7 +15,6 @@
*/ */
class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_TestCase class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_TestCase
{ {
private $mustache; private $mustache;
public function setUp() public function setUp()
@@ -23,18 +22,41 @@ class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_T
$this->mustache = new Mustache_Engine; $this->mustache = new Mustache_Engine;
} }
public function testSingleFilter() /**
* @dataProvider singleFilterData
*/
public function testSingleFilter($tpl, $helpers, $data, $expect)
{ {
$tpl = $this->mustache->loadTemplate('{{% FILTERS }}{{ date | longdate }}'); $this->mustache->setHelpers($helpers);
$this->assertEquals($expect, $this->mustache->render($tpl, $data));
}
$this->mustache->addHelper('longdate', function (\DateTime $value) { public function singleFilterData()
return $value->format('Y-m-d h:m:s'); {
}); $helpers = array(
'longdate' => function (\DateTime $value) {
return $value->format('Y-m-d h:m:s');
},
'echo' => function ($value) {
return array($value, $value, $value);
},
);
$foo = new \StdClass; return array(
$foo->date = new DateTime('1/1/2000'); array(
'{{% FILTERS }}{{ date | longdate }}',
$helpers,
(object) array('date' => new DateTime('1/1/2000')),
'2000-01-01 12:01:00'
),
$this->assertEquals('2000-01-01 12:01:00', $tpl->render($foo)); array(
'{{% FILTERS }}{{# word | echo }}{{ . }}!{{/ word | echo }}',
$helpers,
array('word' => 'bacon'),
'bacon!bacon!bacon!'
),
);
} }
public function testChainedFilters() public function testChainedFilters()
@@ -55,40 +77,87 @@ class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_T
$this->assertEquals('[[2000-01-01 12:01:00]]', $tpl->render($foo)); $this->assertEquals('[[2000-01-01 12:01:00]]', $tpl->render($foo));
} }
public function testInterpolateFirst() const CHAINED_SECTION_FILTERS_TPL = <<<EOS
{{% FILTERS }}
{{# word | echo | with_index }}
{{ key }}: {{ value }}
{{/ word | echo | with_index }}
EOS;
public function testChainedSectionFilters()
{ {
$tpl = $this->mustache->loadTemplate('{{% FILTERS }}{{ foo | bar }}'); $tpl = $this->mustache->loadTemplate(self::CHAINED_SECTION_FILTERS_TPL);
$this->assertEquals('win!', $tpl->render(array(
$this->mustache->addHelper('echo', function ($value) {
return array($value, $value, $value);
});
$this->mustache->addHelper('with_index', function ($value) {
return array_map(function ($k, $v) {
return array(
'key' => $k,
'value' => $v,
);
}, array_keys($value), $value);
});
$this->assertEquals("0: bacon\n1: bacon\n2: bacon\n", $tpl->render(array('word' => 'bacon')));
}
/**
* @dataProvider interpolateFirstData
*/
public function testInterpolateFirst($tpl, $data, $expect)
{
$this->assertEquals($expect, $this->mustache->render($tpl, $data));
}
public function interpolateFirstData()
{
$data = array(
'foo' => 'FOO', 'foo' => 'FOO',
'bar' => function ($value) { 'bar' => function ($value) {
return ($value === 'FOO') ? 'win!' : 'fail :('; return ($value === 'FOO') ? 'win!' : 'fail :(';
}, },
))); );
return array(
array('{{% FILTERS }}{{ foo | bar }}', $data, 'win!'),
array('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}', $data, 'win!'),
);
} }
/** /**
* @expectedException Mustache_Exception_UnknownFilterException * @expectedException Mustache_Exception_UnknownFilterException
* @dataProvider getBrokenPipes * @dataProvider brokenPipeData
*/ */
public function testThrowsExceptionForBrokenPipes($tpl, $data) public function testThrowsExceptionForBrokenPipes($tpl, $data)
{ {
$this->mustache $this->mustache->render($tpl, $data);
->loadTemplate(sprintf('{{%% FILTERS }}{{ %s }}', $tpl))
->render($data);
} }
public function getBrokenPipes() public function brokenPipeData()
{ {
return array( return array(
array('foo | bar', array()), array('{{% FILTERS }}{{ foo | bar }}', array()),
array('foo | bar', array('foo' => 'FOO')), array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO')),
array('foo | bar', array('foo' => 'FOO', 'bar' => 'BAR')), array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO', 'bar' => 'BAR')),
array('foo | bar', array('foo' => 'FOO', 'bar' => array(1, 2))), array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO', 'bar' => array(1, 2))),
array('foo | bar | baz', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; })), array('{{% FILTERS }}{{ foo | bar | baz }}', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; })),
array('foo | bar | baz', array('foo' => 'FOO', 'baz' => function () { return 'BAZ'; })), array('{{% FILTERS }}{{ foo | bar | baz }}', array('foo' => 'FOO', 'baz' => function () { return 'BAZ'; })),
array('foo | bar | baz', array('bar' => function () { return 'BAR'; })), array('{{% FILTERS }}{{ foo | bar | baz }}', array('bar' => function () { return 'BAR'; })),
array('foo | bar | baz', array('baz' => function () { return 'BAZ'; })), array('{{% FILTERS }}{{ foo | bar | baz }}', array('baz' => function () { return 'BAZ'; })),
array('foo | bar.baz', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; }, 'baz' => function () { return 'BAZ'; })), array('{{% FILTERS }}{{ foo | bar.baz }}', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; }, 'baz' => function () { return 'BAZ'; })),
array('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}', array()),
array('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}', array('foo' => 'FOO')),
array('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}', array('foo' => 'FOO', 'bar' => 'BAR')),
array('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}', array('foo' => 'FOO', 'bar' => array(1, 2))),
array('{{% FILTERS }}{{# foo | bar | baz }}{{ . }}{{/ foo | bar | baz }}', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; })),
array('{{% FILTERS }}{{# foo | bar | baz }}{{ . }}{{/ foo | bar | baz }}', array('foo' => 'FOO', 'baz' => function () { return 'BAZ'; })),
array('{{% FILTERS }}{{# foo | bar | baz }}{{ . }}{{/ foo | bar | baz }}', array('bar' => function () { return 'BAR'; })),
array('{{% FILTERS }}{{# foo | bar | baz }}{{ . }}{{/ foo | bar | baz }}', array('baz' => function () { return 'BAZ'; })),
array('{{% FILTERS }}{{# foo | bar.baz }}{{ . }}{{/ foo | bar.baz }}', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; }, 'baz' => function () { return 'BAZ'; })),
); );
} }
} }
@@ -1,101 +0,0 @@
<?php
/*
* This file is part of Mustache.php.
*
* (c) 2010-2014 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* @group filters
* @group functional
*/
class Mustache_Test_FiveThree_Functional_SectionFiltersTest extends PHPUnit_Framework_TestCase
{
private $mustache;
public function setUp()
{
$this->mustache = new Mustache_Engine;
}
public function testSingleFilter()
{
$tpl = $this->mustache->loadTemplate('{{% FILTERS }}{{# word | echo }}{{ . }}!{{/ word | echo }}');
$this->mustache->addHelper('echo', function ($value) {
return array($value, $value, $value);
});
$this->assertEquals('bacon!bacon!bacon!', $tpl->render(array('word' => 'bacon')));
}
const CHAINED_FILTERS_TPL = <<<EOS
{{% FILTERS }}
{{# word | echo | with_index }}
{{ key }}: {{ value }}
{{/ word | echo | with_index }}
EOS;
public function testChainedFilters()
{
$tpl = $this->mustache->loadTemplate(self::CHAINED_FILTERS_TPL);
$this->mustache->addHelper('echo', function ($value) {
return array($value, $value, $value);
});
$this->mustache->addHelper('with_index', function ($value) {
return array_map(function ($k, $v) {
return array(
'key' => $k,
'value' => $v,
);
}, array_keys($value), $value);
});
$this->assertEquals("0: bacon\n1: bacon\n2: bacon\n", $tpl->render(array('word' => 'bacon')));
}
public function testInterpolateFirst()
{
$tpl = $this->mustache->loadTemplate('{{% FILTERS }}{{# foo | bar }}{{ . }}{{/ foo | bar }}');
$this->assertEquals('win!', $tpl->render(array(
'foo' => 'FOO',
'bar' => function ($value) {
return ($value === 'FOO') ? 'win!' : 'fail :(';
},
)));
}
/**
* @expectedException Mustache_Exception_UnknownFilterException
* @dataProvider getBrokenPipes
*/
public function testThrowsExceptionForBrokenPipes($tpl, $data)
{
$this->mustache
->loadTemplate(sprintf('{{%% FILTERS }}{{# %s }}{{ . }}{{/ %s }}', $tpl, $tpl))
->render($data);
}
public function getBrokenPipes()
{
return array(
array('foo | bar', array()),
array('foo | bar', array('foo' => 'FOO')),
array('foo | bar', array('foo' => 'FOO', 'bar' => 'BAR')),
array('foo | bar', array('foo' => 'FOO', 'bar' => array(1, 2))),
array('foo | bar | baz', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; })),
array('foo | bar | baz', array('foo' => 'FOO', 'baz' => function () { return 'BAZ'; })),
array('foo | bar | baz', array('bar' => function () { return 'BAR'; })),
array('foo | bar | baz', array('baz' => function () { return 'BAZ'; })),
array('foo | bar.baz', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; }, 'baz' => function () { return 'BAZ'; })),
);
}
}