diff --git a/test/Mustache/Test/MustacheCallTest.php b/test/Mustache/Test/Functional/CallTest.php similarity index 100% rename from test/Mustache/Test/MustacheCallTest.php rename to test/Mustache/Test/Functional/CallTest.php diff --git a/test/Mustache/Test/Functional/ExamplesTest.php b/test/Mustache/Test/Functional/ExamplesTest.php new file mode 100644 index 0000000..5635dd2 --- /dev/null +++ b/test/Mustache/Test/Functional/ExamplesTest.php @@ -0,0 +1,130 @@ + $partials + )); + $this->assertEquals($expected, $mustache->loadTemplate($source)->render($context)); + } + + /** + * Data provider for testExamples method. + * + * Assumes that an `examples` directory exists inside parent directory. + * This examples directory should contain any number of subdirectories, each of which contains + * three files: one Mustache class (.php), one Mustache template (.mustache), and one output file + * (.txt). + * + * This whole mess will be refined later to be more intuitive and less prescriptive, but it'll + * do for now. Especially since it means we can have unit tests :) + * + * @return array + */ + public function getExamples() { + $path = realpath(__DIR__.'/../../../../examples'); + $examples = array(); + + $handle = opendir($path); + while (($file = readdir($handle)) !== false) { + if ($file == '.' || $file == '..') { + continue; + } + + $fullpath = $path.'/'.$file; + if (is_dir($fullpath)) { + $examples[$file] = $this->loadExample($fullpath); + } + } + closedir($handle); + + return $examples; + } + + /** + * Helper method to load an example given the full path. + * + * @param string $path + * @return array arguments for testExamples + */ + private function loadExample($path) { + $context = null; + $source = null; + $partials = array(); + $expected = null; + + $handle = opendir($path); + while (($file = readdir($handle)) !== false) { + $fullpath = $path.'/'.$file; + $info = pathinfo($fullpath); + + if (is_dir($fullpath) && $info['basename'] == 'partials') { + // load partials + $partials = $this->loadPartials($fullpath); + } elseif (is_file($fullpath)) { + // load other files + switch ($info['extension']) { + case 'php': + require_once($fullpath); + $context = new $info['filename']; + break; + + case 'mustache': + $source = file_get_contents($fullpath); + break; + + case 'txt': + $expected = file_get_contents($fullpath); + break; + } + } + } + closedir($handle); + + return array($context, $source, $partials, $expected); + } + + /** + * Helper method to load partials given an example directory. + * + * @param string $path + * + * @return array $partials + */ + private function loadPartials($path) { + $partials = array(); + + $handle = opendir($path); + while (($file = readdir($handle)) !== false) { + if ($file == '.' || $file == '..') { + continue; + } + + $fullpath = $path.'/'.$file; + $info = pathinfo($fullpath); + + if ($info['extension'] === 'mustache') { + $partials[$info['filename']] = file_get_contents($fullpath); + } + } + closedir($handle); + + return $partials; + } +} diff --git a/test/Mustache/Test/MustacheHigherOrderSectionsTest.php b/test/Mustache/Test/Functional/HigherOrderSectionsTest.php similarity index 100% rename from test/Mustache/Test/MustacheHigherOrderSectionsTest.php rename to test/Mustache/Test/Functional/HigherOrderSectionsTest.php diff --git a/test/Mustache/Test/MustacheInjectionTest.php b/test/Mustache/Test/Functional/MustacheInjectionTest.php similarity index 100% rename from test/Mustache/Test/MustacheInjectionTest.php rename to test/Mustache/Test/Functional/MustacheInjectionTest.php diff --git a/test/Mustache/Test/MustacheSpecTest.php b/test/Mustache/Test/Functional/MustacheSpecTest.php similarity index 100% rename from test/Mustache/Test/MustacheSpecTest.php rename to test/Mustache/Test/Functional/MustacheSpecTest.php diff --git a/test/Mustache/Test/MustacheObjectSectionTest.php b/test/Mustache/Test/Functional/ObjectSectionTest.php similarity index 100% rename from test/Mustache/Test/MustacheObjectSectionTest.php rename to test/Mustache/Test/Functional/ObjectSectionTest.php diff --git a/test/Mustache/Test/MustacheLoaderTest.php b/test/Mustache/Test/Loader/FilesystemLoaderTest.php similarity index 100% rename from test/Mustache/Test/MustacheLoaderTest.php rename to test/Mustache/Test/Loader/FilesystemLoaderTest.php diff --git a/test/Mustache/Test/MustacheExceptionTest.php b/test/Mustache/Test/MustacheExceptionTest.php deleted file mode 100644 index 2a40223..0000000 --- a/test/Mustache/Test/MustacheExceptionTest.php +++ /dev/null @@ -1,152 +0,0 @@ -pickyMustache = new PickyMustache(); - $this->slackerMustache = new SlackerMustache(); - } - - /** - * @group interpolation - * @expectedException MustacheException - */ - public function testThrowsUnknownVariableException() { - $this->pickyMustache->render('{{not_a_variable}}'); - } - - /** - * @group sections - * @expectedException MustacheException - */ - public function testThrowsUnclosedSectionException() { - $this->pickyMustache->render('{{#unclosed}}'); - } - - /** - * @group sections - * @expectedException MustacheException - */ - public function testThrowsUnclosedInvertedSectionException() { - $this->pickyMustache->render('{{^unclosed}}'); - } - - /** - * @group sections - * @expectedException MustacheException - */ - public function testThrowsUnexpectedCloseSectionException() { - $this->pickyMustache->render('{{/unopened}}'); - } - - /** - * @group partials - * @expectedException MustacheException - */ - public function testThrowsUnknownPartialException() { - $this->pickyMustache->render('{{>impartial}}'); - } - - /** - * @group pragmas - * @expectedException MustacheException - */ - public function testThrowsUnknownPragmaException() { - $this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}'); - } - - /** - * @group sections - */ - public function testDoesntThrowUnclosedSectionException() { - $this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}')); - } - - /** - * @group sections - */ - public function testDoesntThrowUnexpectedCloseSectionException() { - $this->assertEquals('', $this->slackerMustache->render('{{/unopened}}')); - } - - /** - * @group partials - */ - public function testDoesntThrowUnknownPartialException() { - $this->assertEquals('', $this->slackerMustache->render('{{>impartial}}')); - } - - /** - * @group pragmas - * @expectedException MustacheException - */ - public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() { - $mustache = new TestableMustache(); - $mustache->testableGetPragmaOptions('PRAGMATIC'); - } - - public function testOverrideThrownExceptionsViaConstructorOptions() { - $exceptions = array( - MustacheException::UNKNOWN_VARIABLE, - MustacheException::UNCLOSED_SECTION, - MustacheException::UNEXPECTED_CLOSE_SECTION, - MustacheException::UNKNOWN_PARTIAL, - MustacheException::UNKNOWN_PRAGMA, - ); - - $one = new TestableMustache(null, null, null, array( - 'throws_exceptions' => array_fill_keys($exceptions, true) - )); - - $thrownExceptions = $one->getThrownExceptions(); - foreach ($exceptions as $exception) { - $this->assertTrue($thrownExceptions[$exception]); - } - - $two = new TestableMustache(null, null, null, array( - 'throws_exceptions' => array_fill_keys($exceptions, false) - )); - - $thrownExceptions = $two->getThrownExceptions(); - foreach ($exceptions as $exception) { - $this->assertFalse($thrownExceptions[$exception]); - } - } -} - -class PickyMustache extends Mustache { - protected $_throwsExceptions = array( - MustacheException::UNKNOWN_VARIABLE => true, - MustacheException::UNCLOSED_SECTION => true, - MustacheException::UNEXPECTED_CLOSE_SECTION => true, - MustacheException::UNKNOWN_PARTIAL => true, - MustacheException::UNKNOWN_PRAGMA => true, - ); -} - -class SlackerMustache extends Mustache { - protected $_throwsExceptions = array( - MustacheException::UNKNOWN_VARIABLE => false, - MustacheException::UNCLOSED_SECTION => false, - MustacheException::UNEXPECTED_CLOSE_SECTION => false, - MustacheException::UNKNOWN_PARTIAL => false, - MustacheException::UNKNOWN_PRAGMA => false, - ); -} - -class TestableMustache extends Mustache { - public function testableGetPragmaOptions($pragma_name) { - return $this->_getPragmaOptions($pragma_name); - } - - public function getThrownExceptions() { - return $this->_throwsExceptions; - } -} diff --git a/test/Mustache/Test/MustachePragmaTest.php b/test/Mustache/Test/MustachePragmaTest.php deleted file mode 100644 index c00441c..0000000 --- a/test/Mustache/Test/MustachePragmaTest.php +++ /dev/null @@ -1,74 +0,0 @@ -render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}'); - } catch (MustacheException $e) { - $this->assertEquals(MustacheException::UNKNOWN_PRAGMA, $e->getCode(), 'Caught exception code was not MustacheException::UNKNOWN_PRAGMA'); - return; - } - - $this->fail('Mustache should have thrown an unknown pragma exception'); - } - - public function testSuppressUnknownPragmaException() { - $m = new LessWhinyMustache(); - - try { - $this->assertEquals('', $m->render('{{%I-HAVE-THE-GREATEST-MUSTACHE}}')); - } catch (MustacheException $e) { - if ($e->getCode() == MustacheException::UNKNOWN_PRAGMA) { - $this->fail('Mustache should have thrown an unknown pragma exception'); - } else { - throw $e; - } - } - } - - public function testPragmaReplace() { - $m = new Mustache(); - $this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed'); - } - - public function testPragmaReplaceMultiple() { - $m = new Mustache(); - - $this->assertEquals('', $m->render('{{% UNESCAPED }}'), 'Pragmas should allow whitespace'); - $this->assertEquals('', $m->render('{{% UNESCAPED foo=bar }}'), 'Pragmas should allow whitespace'); - $this->assertEquals('', $m->render("{{%UNESCAPED}}\n{{%UNESCAPED}}"), 'Multiple pragma tags not removed'); - $this->assertEquals(' ', $m->render('{{%UNESCAPED}} {{%UNESCAPED}}'), 'Multiple pragma tags not removed'); - } - - public function testPragmaReplaceNewline() { - $m = new Mustache(); - $this->assertEquals('', $m->render("{{%UNESCAPED}}\n"), 'Trailing newline after pragma tag not removed'); - $this->assertEquals("\n", $m->render("\n{{%UNESCAPED}}\n"), 'Too many newlines removed with pragma tag'); - $this->assertEquals("1\n23", $m->render("1\n2{{%UNESCAPED}}\n3"), 'Wrong newline removed with pragma tag'); - } - - public function testPragmaReset() { - $m = new Mustache('', array('symbol' => '>>>')); - $this->assertEquals('>>>', $m->render('{{{symbol}}}')); - $this->assertEquals('>>>', $m->render('{{%UNESCAPED}}{{symbol}}')); - $this->assertEquals('>>>', $m->render('{{{symbol}}}')); - } -} - -class LessWhinyMustache extends Mustache { - protected $_throwsExceptions = array( - MustacheException::UNKNOWN_VARIABLE => false, - MustacheException::UNCLOSED_SECTION => true, - MustacheException::UNEXPECTED_CLOSE_SECTION => true, - MustacheException::UNKNOWN_PARTIAL => false, - MustacheException::UNKNOWN_PRAGMA => false, - ); -} \ No newline at end of file diff --git a/test/Mustache/Test/MustachePragmaUnescapedTest.php b/test/Mustache/Test/MustachePragmaUnescapedTest.php deleted file mode 100644 index df6d94c..0000000 --- a/test/Mustache/Test/MustachePragmaUnescapedTest.php +++ /dev/null @@ -1,19 +0,0 @@ - 'Bear > Shark')); - - $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{title}}')); - $this->assertEquals('Bear > Shark', $m->render('{{title}}')); - $this->assertEquals('Bear > Shark', $m->render('{{%UNESCAPED}}{{{title}}}')); - $this->assertEquals('Bear > Shark', $m->render('{{{title}}}')); - } - -} \ No newline at end of file diff --git a/test/fixtures/bar.mustache b/test/fixtures/bar.mustache deleted file mode 100644 index 139594d..0000000 --- a/test/fixtures/bar.mustache +++ /dev/null @@ -1 +0,0 @@ -{{# truthy }}{{ bar }}{{/ truthy }} \ No newline at end of file diff --git a/test/fixtures/foo.mustache b/test/fixtures/foo.mustache deleted file mode 100644 index 008c714..0000000 --- a/test/fixtures/foo.mustache +++ /dev/null @@ -1 +0,0 @@ -{{ foo }} \ No newline at end of file