From d1912038f3a3b6bfadefbabafc19ae823cb639c4 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Tue, 1 May 2012 14:03:11 -0700 Subject: [PATCH] Segregate 5.3+ tests, exclude them from phpunit config. --- phpunit.xml.dist | 3 +- .../Functional/HigherOrderSectionsTest.php | 71 +++++++++++ .../FiveThree/Functional/MustacheSpecTest.php | 114 ++++++++++++++++++ .../Functional/HigherOrderSectionsTest.php | 48 -------- .../Test/Functional/MustacheInjectionTest.php | 16 ++- .../Test/Functional/MustacheSpecTest.php | 35 ------ test/Mustache/Test/HelperCollectionTest.php | 10 +- test/Mustache/Test/MustacheTest.php | 14 ++- 8 files changed, 215 insertions(+), 96 deletions(-) create mode 100644 test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php create mode 100644 test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8863a591..462675a0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,8 @@ - ./test + ./test/Mustache/Test/FiveThree + ./test diff --git a/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php new file mode 100644 index 00000000..dc3a39a5 --- /dev/null +++ b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php @@ -0,0 +1,71 @@ +mustache = new Mustache_Mustache; + } + + public function testAnonymousFunctionSectionCallback() { + $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}'); + + $foo = new Mustache_Test_FiveThree_Functional_Foo; + $foo->name = 'Mario'; + $foo->wrapper = function($text) { + return sprintf('
%s
', $text); + }; + + $this->assertEquals(sprintf('
%s
', $foo->name), $tpl->render($foo)); + } + + public function testSectionCallback() { + $one = $this->mustache->loadTemplate('{{name}}'); + $two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}'); + + $foo = new Mustache_Test_FiveThree_Functional_Foo; + $foo->name = 'Luigi'; + + $this->assertEquals($foo->name, $one->render($foo)); + $this->assertEquals(sprintf('%s', $foo->name), $two->render($foo)); + } + + public function testViewArrayAnonymousSectionCallback() { + $tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}'); + + $data = array( + 'name' => 'Bob', + 'wrap' => function($text) { + return sprintf('[[%s]]', $text); + } + ); + + $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data)); + } +} + +class Mustache_Test_FiveThree_Functional_Foo { + public $name = 'Justin'; + public $lorem = 'Lorem ipsum dolor sit amet,'; + public $wrap; + + public function __construct() { + $this->wrap = function($text) { + return sprintf('%s', $text); + }; + } +} diff --git a/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php b/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php new file mode 100644 index 00000000..686d2b2b --- /dev/null +++ b/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php @@ -0,0 +1,114 @@ +markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"'); + } + } + + /** + * @group lambdas + * @dataProvider loadLambdasSpec + */ + public function testLambdasSpec($desc, $source, $partials, $data, $expected) { + $template = self::loadTemplate($source, $partials); + $this->assertEquals($expected, $template($this->prepareLambdasSpec($data)), $desc); + } + + public function loadLambdasSpec() { + return $this->loadSpec('~lambdas'); + } + + /** + * Extract and lambdafy any 'lambda' values found in the $data array. + */ + private function prepareLambdasSpec($data) { + foreach ($data as $key => $val) { + if ($key === 'lambda') { + if (!isset($val['php'])) { + $this->markTestSkipped(sprintf('PHP lambda test not implemented for this test.')); + } + + $func = $val['php']; + $data[$key] = function($text = null) use ($func) { + return eval($func); + }; + } else if (is_array($val)) { + $data[$key] = $this->prepareLambdasSpec($val); + } + } + + return $data; + } + + /** + * Data provider for the mustache spec test. + * + * Loads YAML files from the spec and converts them to PHPisms. + * + * @access public + * @return array + */ + private function loadSpec($name) { + $filename = dirname(__FILE__) . '/../../../../../vendor/spec/specs/' . $name . '.yml'; + if (!file_exists($filename)) { + return array(); + } + + $data = array(); + $yaml = new sfYamlParser; + $file = file_get_contents($filename); + + // @hack: pre-process the 'lambdas' spec so the Symfony YAML parser doesn't complain. + if ($name === '~lambdas') { + $file = str_replace(" !code\n", "\n", $file); + } + + $spec = $yaml->parse($file); + + foreach ($spec['tests'] as $test) { + $data[] = array( + $test['name'] . ': ' . $test['desc'], + $test['template'], + isset($test['partials']) ? $test['partials'] : array(), + $test['data'], + $test['expected'], + ); + } + + return $data; + } + + private static function loadTemplate($source, $partials) { + self::$mustache->setPartials($partials); + + return self::$mustache->loadTemplate($source); + } +} diff --git a/test/Mustache/Test/Functional/HigherOrderSectionsTest.php b/test/Mustache/Test/Functional/HigherOrderSectionsTest.php index eef60fde..9c48dac9 100644 --- a/test/Mustache/Test/Functional/HigherOrderSectionsTest.php +++ b/test/Mustache/Test/Functional/HigherOrderSectionsTest.php @@ -21,34 +21,6 @@ public function setUp() { $this->mustache = new Mustache_Mustache; } - public function testAnonymousFunctionSectionCallback() { - if (version_compare(PHP_VERSION, '5.3.0', '<')) { - $this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3'); - return; - } - - $tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}'); - - $foo = new Mustache_Test_Functional_Foo; - $foo->name = 'Mario'; - $foo->wrapper = function($text) { - return sprintf('
%s
', $text); - }; - - $this->assertEquals(sprintf('
%s
', $foo->name), $tpl->render($foo)); - } - - public function testSectionCallback() { - $one = $this->mustache->loadTemplate('{{name}}'); - $two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}'); - - $foo = new Mustache_Test_Functional_Foo; - $foo->name = 'Luigi'; - - $this->assertEquals($foo->name, $one->render($foo)); - $this->assertEquals(sprintf('%s', $foo->name), $two->render($foo)); - } - public function testRuntimeSectionCallback() { $tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}'); @@ -80,19 +52,6 @@ public function testViewArraySectionCallback() { $this->assertEquals($data['name'], $tpl->render($data)); } - public function testViewArrayAnonymousSectionCallback() { - $tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}'); - - $data = array( - 'name' => 'Bob', - 'wrap' => function($text) { - return sprintf('[[%s]]', $text); - } - ); - - $this->assertEquals(sprintf('[[%s]]', $data['name']), $tpl->render($data)); - } - public function testMonsters() { $tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}'); @@ -111,13 +70,6 @@ public function testMonsters() { class Mustache_Test_Functional_Foo { public $name = 'Justin'; public $lorem = 'Lorem ipsum dolor sit amet,'; - public $wrap; - - public function __construct() { - $this->wrap = function($text) { - return sprintf('%s', $text); - }; - } public function wrapWithEm($text) { return sprintf('%s', $text); diff --git a/test/Mustache/Test/Functional/MustacheInjectionTest.php b/test/Mustache/Test/Functional/MustacheInjectionTest.php index e16bd7f1..add5b8d5 100644 --- a/test/Mustache/Test/Functional/MustacheInjectionTest.php +++ b/test/Mustache/Test/Functional/MustacheInjectionTest.php @@ -110,9 +110,7 @@ public function testLambdaInterpolationInjection() { $tpl = $this->mustache->loadTemplate('{{ a }}'); $data = array( - 'a' => function() { - return '{{ b }}'; - }, + 'a' => array($this, 'lambdaInterpolationCallback'), 'b' => '{{ c }}', 'c' => 'FAIL' ); @@ -120,17 +118,23 @@ public function testLambdaInterpolationInjection() { $this->assertEquals('{{ c }}', $tpl->render($data)); } + public static function lambdaInterpolationCallback() { + return '{{ b }}'; + } + public function testLambdaSectionInjection() { $tpl = $this->mustache->loadTemplate('{{# a }}b{{/ a }}'); $data = array( - 'a' => function ($text) { - return '{{ ' . $text . ' }}'; - }, + 'a' => array($this, 'lambdaSectionCallback'), 'b' => '{{ c }}', 'c' => 'FAIL' ); $this->assertEquals('{{ c }}', $tpl->render($data)); } + + public static function lambdaSectionCallback($text) { + return '{{ ' . $text . ' }}'; + } } diff --git a/test/Mustache/Test/Functional/MustacheSpecTest.php b/test/Mustache/Test/Functional/MustacheSpecTest.php index e10d60c8..25493f37 100644 --- a/test/Mustache/Test/Functional/MustacheSpecTest.php +++ b/test/Mustache/Test/Functional/MustacheSpecTest.php @@ -86,41 +86,6 @@ public function loadInvertedSpec() { return $this->loadSpec('inverted'); } - /** - * @group lambdas - * @dataProvider loadLambdasSpec - */ - public function testLambdasSpec($desc, $source, $partials, $data, $expected) { - $template = self::loadTemplate($source, $partials); - $this->assertEquals($expected, $template($this->prepareLambdasSpec($data)), $desc); - } - - public function loadLambdasSpec() { - return $this->loadSpec('~lambdas'); - } - - /** - * Extract and lambdafy any 'lambda' values found in the $data array. - */ - private function prepareLambdasSpec($data) { - foreach ($data as $key => $val) { - if ($key === 'lambda') { - if (!isset($val['php'])) { - $this->markTestSkipped(sprintf('PHP lambda test not implemented for this test.')); - } - - $func = $val['php']; - $data[$key] = function($text = null) use ($func) { - return eval($func); - }; - } else if (is_array($val)) { - $data[$key] = $this->prepareLambdasSpec($val); - } - } - - return $data; - } - /** * @group partials * @dataProvider loadPartialsSpec diff --git a/test/Mustache/Test/HelperCollectionTest.php b/test/Mustache/Test/HelperCollectionTest.php index 1a643d4a..ed3f393b 100644 --- a/test/Mustache/Test/HelperCollectionTest.php +++ b/test/Mustache/Test/HelperCollectionTest.php @@ -11,7 +11,7 @@ class Mustache_Test_HelperCollectionTest extends PHPUnit_Framework_TestCase { public function testConstructor() { - $foo = function() { echo 'foo'; }; + $foo = array($this, 'getFoo'); $bar = 'BAR'; $helpers = new Mustache_HelperCollection(array( @@ -23,8 +23,12 @@ public function testConstructor() { $this->assertSame($bar, $helpers->get('bar')); } + public static function getFoo() { + echo 'foo'; + } + public function testAccessorsAndMutators() { - $foo = function() { echo 'foo'; }; + $foo = array($this, 'getFoo'); $bar = 'BAR'; $helpers = new Mustache_HelperCollection; @@ -49,7 +53,7 @@ public function testAccessorsAndMutators() { } public function testMagicMethods() { - $foo = function() { echo 'foo'; }; + $foo = array($this, 'getFoo'); $bar = 'BAR'; $helpers = new Mustache_HelperCollection; diff --git a/test/Mustache/Test/MustacheTest.php b/test/Mustache/Test/MustacheTest.php index ab7c8af1..4a23eb34 100644 --- a/test/Mustache/Test/MustacheTest.php +++ b/test/Mustache/Test/MustacheTest.php @@ -35,7 +35,7 @@ public function testConstructor() { 'foo' => '{{ foo }}', ), 'helpers' => array( - 'foo' => function() { return 'foo'; }, + 'foo' => array($this, 'getFoo'), 'bar' => 'BAR', ), 'escape' => 'strtoupper', @@ -53,6 +53,10 @@ public function testConstructor() { $this->assertFalse($mustache->hasHelper('baz')); } + public static function getFoo() { + return 'foo'; + } + public function testRender() { $source = '{{ foo }}'; $data = array('bar' => 'baz'); @@ -158,7 +162,7 @@ public function testMissingPartialsTreatedAsEmptyString() { } public function testHelpers() { - $foo = function() { return 'foo'; }; + $foo = array($this, 'getFoo'); $bar = 'BAR'; $mustache = new Mustache_Mustache(array('helpers' => array( 'foo' => $foo, @@ -178,7 +182,7 @@ public function testHelpers() { $mustache->addHelper('bar', $bar); $this->assertSame($bar, $mustache->getHelper('bar')); - $baz = function($text) { return '__'.$text.'__'; }; + $baz = array($this, 'wrapWithUnderscores'); $this->assertFalse($mustache->hasHelper('baz')); $this->assertFalse($helpers->has('baz')); @@ -192,6 +196,10 @@ public function testHelpers() { $this->assertEquals('foo - BAR - __qux__', $tpl->render(array('qux' => "won't mess things up"))); } + public static function wrapWithUnderscores($text) { + return '__'.$text.'__'; + } + /** * @expectedException \InvalidArgumentException */