From ad0204c37287a5e6df2b410c12dd947d90613ca1 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 25 Mar 2014 19:04:48 -0700 Subject: [PATCH] Combine FiltersTest and SectionFiltersTest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They’re basically duplicates, so this clears up a bunch of code :) --- .../Test/FiveThree/Functional/FiltersTest.php | 123 ++++++++++++++---- .../Functional/SectionFiltersTest.php | 101 -------------- 2 files changed, 96 insertions(+), 128 deletions(-) delete mode 100644 test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php diff --git a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php index 96e590a..a55a1b6 100644 --- a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php +++ b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php @@ -15,7 +15,6 @@ */ class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_TestCase { - private $mustache; public function setUp() @@ -23,18 +22,41 @@ class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_T $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) { - return $value->format('Y-m-d h:m:s'); - }); + public function singleFilterData() + { + $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; - $foo->date = new DateTime('1/1/2000'); + return array( + 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() @@ -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)); } - public function testInterpolateFirst() + const CHAINED_SECTION_FILTERS_TPL = <<mustache->loadTemplate('{{% FILTERS }}{{ foo | bar }}'); - $this->assertEquals('win!', $tpl->render(array( + $tpl = $this->mustache->loadTemplate(self::CHAINED_SECTION_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'))); + } + + /** + * @dataProvider interpolateFirstData + */ + public function testInterpolateFirst($tpl, $data, $expect) + { + $this->assertEquals($expect, $this->mustache->render($tpl, $data)); + } + + public function interpolateFirstData() + { + $data = array( 'foo' => 'FOO', 'bar' => function ($value) { 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 - * @dataProvider getBrokenPipes + * @dataProvider brokenPipeData */ public function testThrowsExceptionForBrokenPipes($tpl, $data) { - $this->mustache - ->loadTemplate(sprintf('{{%% FILTERS }}{{ %s }}', $tpl)) - ->render($data); + $this->mustache->render($tpl, $data); } - public function getBrokenPipes() + public function brokenPipeData() { 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'; })), + array('{{% FILTERS }}{{ foo | bar }}', array()), + array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO')), + array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO', 'bar' => 'BAR')), + array('{{% FILTERS }}{{ foo | bar }}', array('foo' => 'FOO', 'bar' => array(1, 2))), + array('{{% FILTERS }}{{ foo | bar | baz }}', array('foo' => 'FOO', 'bar' => function () { return 'BAR'; })), + array('{{% FILTERS }}{{ foo | bar | baz }}', array('foo' => 'FOO', 'baz' => function () { return 'BAZ'; })), + array('{{% FILTERS }}{{ foo | bar | baz }}', array('bar' => function () { return 'BAR'; })), + array('{{% FILTERS }}{{ foo | bar | baz }}', array('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'; })), ); } } diff --git a/test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php b/test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php deleted file mode 100644 index 0c437f6..0000000 --- a/test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php +++ /dev/null @@ -1,101 +0,0 @@ -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 = <<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'; })), - ); - } - -}