diff --git a/.travis.yml b/.travis.yml index 47022c5..e73c8b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,3 +4,9 @@ php: - 5.3 - 5.4 - 5.5 + - 5.6 + - hhvm + +matrix: + allow_failures: + - php: hhvm # See https://github.com/facebook/hhvm/pull/1860 \ No newline at end of file diff --git a/LICENSE b/LICENSE index 6db5300..0bdbc04 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,6 @@ The MIT License (MIT) -Copyright (c) 2010 Justin Hileman + +Copyright (c) 2010-2014 Justin Hileman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1c49ae3..b33b355 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,9 @@ Mustache.php A [Mustache](http://mustache.github.com/) implementation in PHP. -[![Build Status](https://secure.travis-ci.org/bobthecow/mustache.php.png?branch=dev)](http://travis-ci.org/bobthecow/mustache.php) +[![Package version](http://img.shields.io/packagist/v/mustache/mustache.svg)](https://packagist.org/packages/mustache/mustache) +[![Build status](http://img.shields.io/travis/bobthecow/mustache.php/dev.svg)](http://travis-ci.org/bobthecow/mustache.php) +[![Monthly downloads](http://img.shields.io/packagist/dm/mustache/mustache.svg)](https://packagist.org/packages/mustache/mustache) Usage diff --git a/bin/build_bootstrap.php b/bin/build_bootstrap.php index 061532b..f9fa2af 100755 --- a/bin/build_bootstrap.php +++ b/bin/build_bootstrap.php @@ -4,7 +4,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -85,7 +85,7 @@ class SymfonyClassCollectionLoader /* * This file is part of Mustache.php. * - * (c) %d Justin Hileman + * (c) 2010-%d Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Autoloader.php b/src/Mustache/Autoloader.php index 6936b06..04b9e6f 100644 --- a/src/Mustache/Autoloader.php +++ b/src/Mustache/Autoloader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Cache.php b/src/Mustache/Cache.php index 6387872..c8fc5d5 100644 --- a/src/Mustache/Cache.php +++ b/src/Mustache/Cache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Cache/AbstractCache.php b/src/Mustache/Cache/AbstractCache.php index 9ee7157..98b6451 100644 --- a/src/Mustache/Cache/AbstractCache.php +++ b/src/Mustache/Cache/AbstractCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Cache/FilesystemCache.php b/src/Mustache/Cache/FilesystemCache.php index e4926bc..120ad2d 100644 --- a/src/Mustache/Cache/FilesystemCache.php +++ b/src/Mustache/Cache/FilesystemCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Cache/NoopCache.php b/src/Mustache/Cache/NoopCache.php index a5b9ad9..d3a7e1f 100644 --- a/src/Mustache/Cache/NoopCache.php +++ b/src/Mustache/Cache/NoopCache.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Compiler.php b/src/Mustache/Compiler.php index c13e60f..06af266 100644 --- a/src/Mustache/Compiler.php +++ b/src/Mustache/Compiler.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Context.php b/src/Mustache/Context.php index c6900d7..1966d25 100644 --- a/src/Mustache/Context.php +++ b/src/Mustache/Context.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Engine.php b/src/Mustache/Engine.php index 0bfc9cc..b395336 100644 --- a/src/Mustache/Engine.php +++ b/src/Mustache/Engine.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -23,7 +23,7 @@ */ class Mustache_Engine { - const VERSION = '2.5.1'; + const VERSION = '2.6.0'; const SPEC_VERSION = '1.1.2'; const PRAGMA_FILTERS = 'FILTERS'; @@ -45,6 +45,11 @@ class Mustache_Engine private $logger; private $strictCallables = false; + // Services + private $tokenizer; + private $parser; + private $compiler; + /** * Mustache class constructor. * @@ -79,12 +84,12 @@ class Mustache_Engine * // An array of 'helpers'. Helpers can be global variables or objects, closures (e.g. for higher order * // sections), or any other valid Mustache context value. They will be prepended to the context stack, * // so they will be available in any template loaded by this Mustache instance. - * 'helpers' => array('i18n' => function($text) { + * 'helpers' => array('i18n' => function ($text) { * // do something translatey here... * }), * * // An 'escape' callback, responsible for escaping double-mustache variables. - * 'escape' => function($value) { + * 'escape' => function ($value) { * return htmlspecialchars($buffer, ENT_COMPAT, 'UTF-8'); * }, * @@ -194,7 +199,7 @@ class Mustache_Engine /** * Get the current Mustache escape callback. * - * @return mixed Callable or null + * @return callable|null */ public function getEscape() { @@ -657,9 +662,9 @@ class Mustache_Engine } if (!class_exists($className, false)) { - if (!$this->getCache()->load($className)) { + if (!$cache->load($className)) { $compiled = $this->compile($source); - $this->getCache()->cache($className, $compiled); + $cache->cache($className, $compiled); } } diff --git a/src/Mustache/Exception.php b/src/Mustache/Exception.php index b4f8300..8a2b01c 100644 --- a/src/Mustache/Exception.php +++ b/src/Mustache/Exception.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Exception/InvalidArgumentException.php b/src/Mustache/Exception/InvalidArgumentException.php index 34a6fd6..9bd1107 100644 --- a/src/Mustache/Exception/InvalidArgumentException.php +++ b/src/Mustache/Exception/InvalidArgumentException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Exception/LogicException.php b/src/Mustache/Exception/LogicException.php index a2063cd..255ce54 100644 --- a/src/Mustache/Exception/LogicException.php +++ b/src/Mustache/Exception/LogicException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Exception/RuntimeException.php b/src/Mustache/Exception/RuntimeException.php index f637ceb..a3c48f7 100644 --- a/src/Mustache/Exception/RuntimeException.php +++ b/src/Mustache/Exception/RuntimeException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Exception/SyntaxException.php b/src/Mustache/Exception/SyntaxException.php index fad60fc..7a16f7e 100644 --- a/src/Mustache/Exception/SyntaxException.php +++ b/src/Mustache/Exception/SyntaxException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,12 +16,19 @@ class Mustache_Exception_SyntaxException extends LogicException implements Musta { protected $token; + /** + * @param string $msg + * @param array $token + */ public function __construct($msg, array $token) { $this->token = $token; parent::__construct($msg); } + /** + * @return array + */ public function getToken() { return $this->token; diff --git a/src/Mustache/Exception/UnknownFilterException.php b/src/Mustache/Exception/UnknownFilterException.php index f5c0884..b9a315a 100644 --- a/src/Mustache/Exception/UnknownFilterException.php +++ b/src/Mustache/Exception/UnknownFilterException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,6 +16,9 @@ class Mustache_Exception_UnknownFilterException extends UnexpectedValueException { protected $filterName; + /** + * @param string $filterName + */ public function __construct($filterName) { $this->filterName = $filterName; diff --git a/src/Mustache/Exception/UnknownHelperException.php b/src/Mustache/Exception/UnknownHelperException.php index 98af13e..226d774 100644 --- a/src/Mustache/Exception/UnknownHelperException.php +++ b/src/Mustache/Exception/UnknownHelperException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,6 +16,9 @@ class Mustache_Exception_UnknownHelperException extends InvalidArgumentException { protected $helperName; + /** + * @param string $helperName + */ public function __construct($helperName) { $this->helperName = $helperName; diff --git a/src/Mustache/Exception/UnknownTemplateException.php b/src/Mustache/Exception/UnknownTemplateException.php index 141d372..5dafe89 100644 --- a/src/Mustache/Exception/UnknownTemplateException.php +++ b/src/Mustache/Exception/UnknownTemplateException.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -16,6 +16,9 @@ class Mustache_Exception_UnknownTemplateException extends InvalidArgumentExcepti { protected $templateName; + /** + * @param string $templateName + */ public function __construct($templateName) { $this->templateName = $templateName; diff --git a/src/Mustache/HelperCollection.php b/src/Mustache/HelperCollection.php index 4fee762..bbeb277 100644 --- a/src/Mustache/HelperCollection.php +++ b/src/Mustache/HelperCollection.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/LambdaHelper.php b/src/Mustache/LambdaHelper.php index 0ae0b64..7cd8092 100644 --- a/src/Mustache/LambdaHelper.php +++ b/src/Mustache/LambdaHelper.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -38,7 +38,7 @@ class Mustache_LambdaHelper * * @param string $string * - * @return Rendered template. + * @return string Rendered template. */ public function render($string) { diff --git a/src/Mustache/Loader.php b/src/Mustache/Loader.php index 9fa82db..9378c2c 100644 --- a/src/Mustache/Loader.php +++ b/src/Mustache/Loader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Loader/ArrayLoader.php b/src/Mustache/Loader/ArrayLoader.php index dfd9e02..e7ece91 100644 --- a/src/Mustache/Loader/ArrayLoader.php +++ b/src/Mustache/Loader/ArrayLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -26,6 +26,7 @@ */ class Mustache_Loader_ArrayLoader implements Mustache_Loader, Mustache_Loader_MutableLoader { + private $templates; /** * ArrayLoader constructor. diff --git a/src/Mustache/Loader/CascadingLoader.php b/src/Mustache/Loader/CascadingLoader.php index 192edb9..d02a273 100644 --- a/src/Mustache/Loader/CascadingLoader.php +++ b/src/Mustache/Loader/CascadingLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -25,7 +25,7 @@ class Mustache_Loader_CascadingLoader implements Mustache_Loader * new Mustache_Loader_FilesystemLoader(__DIR__.'/templates') * )); * - * @param array $loaders An array of Mustache Loader instances + * @param Mustache_Loader[] $loaders */ public function __construct(array $loaders = array()) { @@ -38,7 +38,7 @@ class Mustache_Loader_CascadingLoader implements Mustache_Loader /** * Add a Loader instance. * - * @param Mustache_Loader $loader A Mustache Loader instance + * @param Mustache_Loader $loader */ public function addLoader(Mustache_Loader $loader) { diff --git a/src/Mustache/Loader/FilesystemLoader.php b/src/Mustache/Loader/FilesystemLoader.php index 8abe6c6..8a27b01 100644 --- a/src/Mustache/Loader/FilesystemLoader.php +++ b/src/Mustache/Loader/FilesystemLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Loader/InlineLoader.php b/src/Mustache/Loader/InlineLoader.php index e08852e..9e4ab42 100644 --- a/src/Mustache/Loader/InlineLoader.php +++ b/src/Mustache/Loader/InlineLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -35,7 +35,7 @@ * 'mustache.loader' => new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__) * )); * - * $app->get('/{name}', function($name) use ($app) { + * $app->get('/{name}', function ($name) use ($app) { * return $app['mustache']->render('hello', compact('name')); * }) * ->value('name', 'world'); diff --git a/src/Mustache/Loader/MutableLoader.php b/src/Mustache/Loader/MutableLoader.php index f606edb..f59f95d 100644 --- a/src/Mustache/Loader/MutableLoader.php +++ b/src/Mustache/Loader/MutableLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -19,6 +19,8 @@ interface Mustache_Loader_MutableLoader * Set an associative array of Template sources for this loader. * * @param array $templates + * + * @return void */ public function setTemplates(array $templates); @@ -27,6 +29,8 @@ interface Mustache_Loader_MutableLoader * * @param string $name * @param string $template Mustache Template source + * + * @return void */ public function setTemplate($name, $template); } diff --git a/src/Mustache/Loader/StringLoader.php b/src/Mustache/Loader/StringLoader.php index 295ace4..5570cda 100644 --- a/src/Mustache/Loader/StringLoader.php +++ b/src/Mustache/Loader/StringLoader.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Logger.php b/src/Mustache/Logger.php index 3874bba..2e5d674 100644 --- a/src/Mustache/Logger.php +++ b/src/Mustache/Logger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Logger/AbstractLogger.php b/src/Mustache/Logger/AbstractLogger.php index 44f99d3..3dd96e7 100644 --- a/src/Mustache/Logger/AbstractLogger.php +++ b/src/Mustache/Logger/AbstractLogger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Logger/StreamLogger.php b/src/Mustache/Logger/StreamLogger.php index c79e6cb..d422340 100644 --- a/src/Mustache/Logger/StreamLogger.php +++ b/src/Mustache/Logger/StreamLogger.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -31,14 +31,15 @@ class Mustache_Logger_StreamLogger extends Mustache_Logger_AbstractLogger self::EMERGENCY => 600, ); + protected $level; protected $stream = null; protected $url = null; /** * @throws InvalidArgumentException if the logging level is unknown. * - * @param string $stream Resource instance or URL - * @param integer $level The minimum logging level at which this handler will be triggered + * @param resource|string $stream Resource instance or URL + * @param integer $level The minimum logging level at which this handler will be triggered */ public function __construct($stream, $level = Mustache_Logger::ERROR) { diff --git a/src/Mustache/Parser.php b/src/Mustache/Parser.php index 227e00b..54f52fc 100644 --- a/src/Mustache/Parser.php +++ b/src/Mustache/Parser.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -71,12 +71,22 @@ class Mustache_Parser case Mustache_Tokenizer::T_END_SECTION: if (!isset($parent)) { - $msg = sprintf('Unexpected closing tag: /%s', $token[Mustache_Tokenizer::NAME]); + $msg = sprintf( + 'Unexpected closing tag: /%s on line %d', + $token[Mustache_Tokenizer::NAME], + $token[Mustache_Tokenizer::LINE] + ); throw new Mustache_Exception_SyntaxException($msg, $token); } if ($token[Mustache_Tokenizer::NAME] !== $parent[Mustache_Tokenizer::NAME]) { - $msg = sprintf('Nesting error: %s vs. %s', $parent[Mustache_Tokenizer::NAME], $token[Mustache_Tokenizer::NAME]); + $msg = sprintf( + 'Nesting error: %s (on line %d) vs. %s (on line %d)', + $parent[Mustache_Tokenizer::NAME], + $parent[Mustache_Tokenizer::LINE], + $token[Mustache_Tokenizer::NAME], + $token[Mustache_Tokenizer::LINE] + ); throw new Mustache_Exception_SyntaxException($msg, $token); } @@ -85,7 +95,6 @@ class Mustache_Parser $parent[Mustache_Tokenizer::NODES] = $nodes; return $parent; - break; case Mustache_Tokenizer::T_PARTIAL: case Mustache_Tokenizer::T_PARTIAL_2: @@ -109,7 +118,11 @@ class Mustache_Parser } if (isset($parent)) { - $msg = sprintf('Missing closing tag: %s', $parent[Mustache_Tokenizer::NAME]); + $msg = sprintf( + 'Missing closing tag: %s opened on line %d', + $parent[Mustache_Tokenizer::NAME], + $parent[Mustache_Tokenizer::LINE] + ); throw new Mustache_Exception_SyntaxException($msg, $parent); } @@ -144,7 +157,6 @@ class Mustache_Parser } } - $next = null; if ($next = reset($tokens)) { // If we're on a new line, bail. if ($next[Mustache_Tokenizer::LINE] !== $this->lineNum) { diff --git a/src/Mustache/Template.php b/src/Mustache/Template.php index 8c76916..c9621c8 100644 --- a/src/Mustache/Template.php +++ b/src/Mustache/Template.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/src/Mustache/Tokenizer.php b/src/Mustache/Tokenizer.php index c701b25..4b5a33c 100644 --- a/src/Mustache/Tokenizer.php +++ b/src/Mustache/Tokenizer.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -79,6 +79,8 @@ class Mustache_Tokenizer private $line; private $otag; private $ctag; + private $otagLen; + private $ctagLen; /** * Scan and tokenize template source. @@ -90,24 +92,30 @@ class Mustache_Tokenizer */ public function scan($text, $delimiters = null) { + // Setting mbstring.func_overload makes things *really* slow. + // Let's do everyone a favor and scan this string as ASCII instead. + $encoding = null; + if (function_exists('mb_internal_encoding') && ini_get('mbstring.func_overload') & 2) { + $encoding = mb_internal_encoding(); + mb_internal_encoding('ASCII'); + } + $this->reset(); if ($delimiters = trim($delimiters)) { - list($otag, $ctag) = explode(' ', $delimiters); - $this->otag = $otag; - $this->ctag = $ctag; + $this->setDelimiters($delimiters); } $len = strlen($text); for ($i = 0; $i < $len; $i++) { switch ($this->state) { case self::IN_TEXT: - if ($this->tagChange($this->otag, $text, $i)) { + if ($this->tagChange($this->otag, $this->otagLen, $text, $i)) { $i--; $this->flushBuffer(); $this->state = self::IN_TAG_TYPE; } else { - $char = substr($text, $i, 1); + $char = $text[$i]; $this->buffer .= $char; if ($char == "\n") { $this->flushBuffer(); @@ -117,8 +125,8 @@ class Mustache_Tokenizer break; case self::IN_TAG_TYPE: - $i += strlen($this->otag) - 1; - $char = substr($text, $i + 1, 1); + $i += $this->otagLen - 1; + $char = $text[$i + 1]; if (isset(self::$tagTypes[$char])) { $tag = $char; $this->tagType = $tag; @@ -143,18 +151,18 @@ class Mustache_Tokenizer break; default: - if ($this->tagChange($this->ctag, $text, $i)) { + if ($this->tagChange($this->ctag, $this->ctagLen, $text, $i)) { $this->tokens[] = array( self::TYPE => $this->tagType, self::NAME => trim($this->buffer), self::OTAG => $this->otag, self::CTAG => $this->ctag, self::LINE => $this->line, - self::INDEX => ($this->tagType == self::T_END_SECTION) ? $this->seenTag - strlen($this->otag) : $i + strlen($this->ctag) + self::INDEX => ($this->tagType == self::T_END_SECTION) ? $this->seenTag - $this->otagLen : $i + $this->ctagLen ); $this->buffer = ''; - $i += strlen($this->ctag) - 1; + $i += $this->ctagLen - 1; $this->state = self::IN_TEXT; if ($this->tagType == self::T_UNESCAPED) { if ($this->ctag == '}}') { @@ -168,7 +176,7 @@ class Mustache_Tokenizer } } } else { - $this->buffer .= substr($text, $i, 1); + $this->buffer .= $text[$i]; } break; } @@ -176,6 +184,11 @@ class Mustache_Tokenizer $this->flushBuffer(); + // Restore the user's encoding... + if ($encoding) { + mb_internal_encoding($encoding); + } + return $this->tokens; } @@ -184,15 +197,17 @@ class Mustache_Tokenizer */ private function reset() { - $this->state = self::IN_TEXT; - $this->tagType = null; - $this->tag = null; - $this->buffer = ''; - $this->tokens = array(); - $this->seenTag = false; - $this->line = 0; - $this->otag = '{{'; - $this->ctag = '}}'; + $this->state = self::IN_TEXT; + $this->tagType = null; + $this->tag = null; + $this->buffer = ''; + $this->tokens = array(); + $this->seenTag = false; + $this->line = 0; + $this->otag = '{{'; + $this->ctag = '}}'; + $this->otagLen = 2; + $this->ctagLen = 2; } /** @@ -224,9 +239,7 @@ class Mustache_Tokenizer $close = '='.$this->ctag; $closeIndex = strpos($text, $close, $index); - list($otag, $ctag) = explode(' ', trim(substr($text, $startIndex, $closeIndex - $startIndex))); - $this->otag = $otag; - $this->ctag = $ctag; + $this->setDelimiters(trim(substr($text, $startIndex, $closeIndex - $startIndex))); $this->tokens[] = array( self::TYPE => self::T_DELIM_CHANGE, @@ -236,6 +249,20 @@ class Mustache_Tokenizer return $closeIndex + strlen($close) - 1; } + /** + * Set the current Mustache `otag` and `ctag` delimiters. + * + * @param string $delimiters + */ + private function setDelimiters($delimiters) + { + list($otag, $ctag) = explode(' ', $delimiters); + $this->otag = $otag; + $this->ctag = $ctag; + $this->otagLen = strlen($otag); + $this->ctagLen = strlen($ctag); + } + /** * Add pragma token. * @@ -259,20 +286,21 @@ class Mustache_Tokenizer self::LINE => 0, )); - return $end + strlen($this->ctag) - 1; + return $end + $this->ctagLen - 1; } /** * Test whether it's time to change tags. * - * @param string $tag Current tag name - * @param string $text Mustache template source - * @param int $index Current tokenizer index + * @param string $tag Current tag name + * @param int $tagLen Current tag name length + * @param string $text Mustache template source + * @param int $index Current tokenizer index * * @return boolean True if this is a closing section tag */ - private function tagChange($tag, $text, $index) + private function tagChange($tag, $tagLen, $text, $index) { - return substr($text, $index, strlen($tag)) === $tag; + return substr($text, $index, $tagLen) === $tag; } } diff --git a/test/Mustache/Test/AutoloaderTest.php b/test/Mustache/Test/AutoloaderTest.php index 7ab45ca..32fb0dc 100644 --- a/test/Mustache/Test/AutoloaderTest.php +++ b/test/Mustache/Test/AutoloaderTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Cache/AbstractCacheTest.php b/test/Mustache/Test/Cache/AbstractCacheTest.php index 142c12e..1a9c629 100644 --- a/test/Mustache/Test/Cache/AbstractCacheTest.php +++ b/test/Mustache/Test/Cache/AbstractCacheTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Cache/FilesystemCacheTest.php b/test/Mustache/Test/Cache/FilesystemCacheTest.php index 87b33f3..5bfb18c 100644 --- a/test/Mustache/Test/Cache/FilesystemCacheTest.php +++ b/test/Mustache/Test/Cache/FilesystemCacheTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -12,18 +12,8 @@ /** * @group functional */ -class Mustache_Test_Cache_FilesystemCacheTest extends PHPUnit_Framework_TestCase +class Mustache_Test_Cache_FilesystemCacheTest extends Mustache_Test_FunctionalTestCase { - private static $tempDir; - - public static function setUpBeforeClass() - { - self::$tempDir = sys_get_temp_dir() . '/mustache_test'; - if (file_exists(self::$tempDir)) { - self::rmdir(self::$tempDir); - } - } - public function testCacheGetNone() { $key = 'some key'; @@ -43,25 +33,4 @@ class Mustache_Test_Cache_FilesystemCacheTest extends PHPUnit_Framework_TestCase $this->assertTrue($loaded); } - - private static function rmdir($path) - { - $path = rtrim($path, '/').'/'; - $handle = opendir($path); - while (($file = readdir($handle)) !== false) { - if ($file == '.' || $file == '..') { - continue; - } - - $fullpath = $path.$file; - if (is_dir($fullpath)) { - self::rmdir($fullpath); - } else { - unlink($fullpath); - } - } - - closedir($handle); - rmdir($path); - } } diff --git a/test/Mustache/Test/CompilerTest.php b/test/Mustache/Test/CompilerTest.php index 8f8ec4c..a9c7b3a 100644 --- a/test/Mustache/Test/CompilerTest.php +++ b/test/Mustache/Test/CompilerTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -142,6 +142,9 @@ class Mustache_Test_CompilerTest extends PHPUnit_Framework_TestCase $compiler->compile('', array(array(Mustache_Tokenizer::TYPE => 'invalid')), 'SomeClass'); } + /** + * @param string $value + */ private function createTextToken($value) { return array( diff --git a/test/Mustache/Test/ContextTest.php b/test/Mustache/Test/ContextTest.php index b4f36a7..1e74754 100644 --- a/test/Mustache/Test/ContextTest.php +++ b/test/Mustache/Test/ContextTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/EngineTest.php b/test/Mustache/Test/EngineTest.php index 1ab3078..6904d18 100644 --- a/test/Mustache/Test/EngineTest.php +++ b/test/Mustache/Test/EngineTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -12,19 +12,8 @@ /** * @group unit */ -class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase +class Mustache_Test_EngineTest extends Mustache_Test_FunctionalTestCase { - - private static $tempDir; - - public static function setUpBeforeClass() - { - self::$tempDir = sys_get_temp_dir() . '/mustache_test'; - if (file_exists(self::$tempDir)) { - self::rmdir(self::$tempDir); - } - } - public function testConstructor() { $logger = new Mustache_Logger_StreamLogger(tmpfile()); @@ -309,65 +298,35 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase public function testCacheWarningLogging() { - $name = tempnam(sys_get_temp_dir(), 'mustache-test'); - $mustache = new Mustache_Engine(array( - 'logger' => new Mustache_Logger_StreamLogger($name, Mustache_Logger::WARNING) - )); - - $result = $mustache->render('{{ foo }}', array('foo' => 'FOO')); - $this->assertEquals('FOO', $result); - + list($name, $mustache) = $this->getLoggedMustache(Mustache_Logger::WARNING); + $mustache->render('{{ foo }}', array('foo' => 'FOO')); $this->assertContains('WARNING: Template cache disabled, evaluating', file_get_contents($name)); } public function testLoggingIsNotTooAnnoying() { - $name = tempnam(sys_get_temp_dir(), 'mustache-test'); - $mustache = new Mustache_Engine(array( - 'logger' => new Mustache_Logger_StreamLogger($name) - )); - - $result = $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO')); - $this->assertEquals('FOO', $result); - + list($name, $mustache) = $this->getLoggedMustache(); + $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO')); $this->assertEmpty(file_get_contents($name)); } public function testVerboseLoggingIsVerbose() { - $name = tempnam(sys_get_temp_dir(), 'mustache-test'); - $mustache = new Mustache_Engine(array( - 'logger' => new Mustache_Logger_StreamLogger($name, Mustache_Logger::DEBUG) - )); - - $result = $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO')); - $this->assertEquals('FOO', $result); - + list($name, $mustache) = $this->getLoggedMustache(Mustache_Logger::DEBUG); + $mustache->render('{{ foo }}{{> bar }}', array('foo' => 'FOO')); $log = file_get_contents($name); - - $this->assertContains("DEBUG: Instantiating template: ", $log); + $this->assertContains("DEBUG: Instantiating template: ", $log); $this->assertContains("WARNING: Partial not found: \"bar\"", $log); } - private static function rmdir($path) + private function getLoggedMustache($level = Mustache_Logger::ERROR) { - $path = rtrim($path, '/').'/'; - $handle = opendir($path); - while (($file = readdir($handle)) !== false) { - if ($file == '.' || $file == '..') { - continue; - } + $name = tempnam(sys_get_temp_dir(), 'mustache-test'); + $mustache = new Mustache_Engine(array( + 'logger' => new Mustache_Logger_StreamLogger($name, $level) + )); - $fullpath = $path.$file; - if (is_dir($fullpath)) { - self::rmdir($fullpath); - } else { - unlink($fullpath); - } - } - - closedir($handle); - rmdir($path); + return array($name, $mustache); } } diff --git a/test/Mustache/Test/Exception/SyntaxExceptionTest.php b/test/Mustache/Test/Exception/SyntaxExceptionTest.php index 8914fbd..8b176c5 100644 --- a/test/Mustache/Test/Exception/SyntaxExceptionTest.php +++ b/test/Mustache/Test/Exception/SyntaxExceptionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Exception/UnknownFilterExceptionTest.php b/test/Mustache/Test/Exception/UnknownFilterExceptionTest.php index 8ba2e4d..9daf643 100644 --- a/test/Mustache/Test/Exception/UnknownFilterExceptionTest.php +++ b/test/Mustache/Test/Exception/UnknownFilterExceptionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Exception/UnknownHelperExceptionTest.php b/test/Mustache/Test/Exception/UnknownHelperExceptionTest.php index 60567f9..b5da3ed 100644 --- a/test/Mustache/Test/Exception/UnknownHelperExceptionTest.php +++ b/test/Mustache/Test/Exception/UnknownHelperExceptionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Exception/UnknownTemplateExceptionTest.php b/test/Mustache/Test/Exception/UnknownTemplateExceptionTest.php index 001bc63..501181b 100644 --- a/test/Mustache/Test/Exception/UnknownTemplateExceptionTest.php +++ b/test/Mustache/Test/Exception/UnknownTemplateExceptionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/FiveThree/Functional/ClosureQuirksTest.php b/test/Mustache/Test/FiveThree/Functional/ClosureQuirksTest.php index 64651b6..23ac43b 100644 --- a/test/Mustache/Test/FiveThree/Functional/ClosureQuirksTest.php +++ b/test/Mustache/Test/FiveThree/Functional/ClosureQuirksTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -13,7 +13,7 @@ * @group lambdas * @group functional */ -class Mustache_Test_FiveThree_Functional_ClosuresQuirksTest extends PHPUnit_Framework_TestCase +class Mustache_Test_FiveThree_Functional_ClosureQuirksTest extends PHPUnit_Framework_TestCase { private $mustache; @@ -25,6 +25,6 @@ class Mustache_Test_FiveThree_Functional_ClosuresQuirksTest extends PHPUnit_Fram public function testClosuresDontLikeItWhenYouTouchTheirProperties() { $tpl = $this->mustache->loadTemplate('{{ foo.bar }}'); - $this->assertEquals('', $tpl->render(array('foo' => function() { return 'FOO'; }))); + $this->assertEquals('', $tpl->render(array('foo' => function () { return 'FOO'; }))); } } diff --git a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php index 9b8b815..a55a1b6 100644 --- a/test/Mustache/Test/FiveThree/Functional/FiltersTest.php +++ b/test/Mustache/Test/FiveThree/Functional/FiltersTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,7 +15,6 @@ */ class Mustache_Test_FiveThree_Functional_FiltersTest extends PHPUnit_Framework_TestCase { - private $mustache; public function setUp() @@ -23,29 +22,52 @@ 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() { $tpl = $this->mustache->loadTemplate('{{% FILTERS }}{{ date | longdate | withbrackets }}'); - $this->mustache->addHelper('longdate', function(\DateTime $value) { + $this->mustache->addHelper('longdate', function (\DateTime $value) { return $value->format('Y-m-d h:m:s'); }); - $this->mustache->addHelper('withbrackets', function($value) { + $this->mustache->addHelper('withbrackets', function ($value) { return sprintf('[[%s]]', $value); }); @@ -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) { + '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/HigherOrderSectionsTest.php b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php index 5e3ac7c..597c93a 100644 --- a/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php +++ b/test/Mustache/Test/FiveThree/Functional/HigherOrderSectionsTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -28,7 +28,7 @@ class Mustache_Test_FiveThree_Functional_HigherOrderSectionsTest extends PHPUnit $foo = new Mustache_Test_FiveThree_Functional_Foo; $foo->name = 'Mario'; - $foo->wrapper = function($text) { + $foo->wrapper = function ($text) { return sprintf('
%s
', $text); }; @@ -53,7 +53,7 @@ class Mustache_Test_FiveThree_Functional_HigherOrderSectionsTest extends PHPUnit $data = array( 'name' => 'Bob', - 'wrap' => function($text) { + 'wrap' => function ($text) { return sprintf('[[%s]]', $text); } ); @@ -70,7 +70,7 @@ class Mustache_Test_FiveThree_Functional_Foo public function __construct() { - $this->wrap = function($text) { + $this->wrap = function ($text) { return sprintf('%s', $text); }; } diff --git a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php index a73473e..cf24804 100644 --- a/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php +++ b/test/Mustache/Test/FiveThree/Functional/LambdaHelperTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -29,7 +29,7 @@ class Mustache_Test_FiveThree_Functional_LambdaHelperTest extends PHPUnit_Framew $foo = new StdClass; $foo->name = 'Mario'; - $foo->lambda = function($text, $mustache) { + $foo->lambda = function ($text, $mustache) { return strtoupper($mustache->render($text)); }; diff --git a/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php b/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php index dcea68f..88094fb 100644 --- a/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php +++ b/test/Mustache/Test/FiveThree/Functional/MustacheSpecTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,15 +15,8 @@ * @group mustache-spec * @group functional */ -class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase +class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends Mustache_Test_SpecTestCase { - private static $mustache; - - public static function setUpBeforeClass() - { - self::$mustache = new Mustache_Engine; - } - /** * For some reason data providers can't mark tests skipped, so this test exists * simply to provide a 'skipped' test if the `spec` submodule isn't initialized. @@ -62,7 +55,7 @@ class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends PHPUnit_Framew } $func = $val['php']; - $data[$key] = function($text = null) use ($func) { + $data[$key] = function ($text = null) use ($func) { return eval($func); }; } elseif (is_array($val)) { @@ -72,50 +65,4 @@ class Mustache_Test_FiveThree_Functional_MustacheSpecTest extends PHPUnit_Framew 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/FiveThree/Functional/PartialLambdaIndentTest.php b/test/Mustache/Test/FiveThree/Functional/PartialLambdaIndentTest.php index 5d9e4d1..ecc5e23 100644 --- a/test/Mustache/Test/FiveThree/Functional/PartialLambdaIndentTest.php +++ b/test/Mustache/Test/FiveThree/Functional/PartialLambdaIndentTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -51,7 +51,7 @@ class Mustache_Test_Functional_ClassWithLambda { public function _t() { - return function($val) { + return function ($val) { return strtoupper($val); }; } diff --git a/test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php b/test/Mustache/Test/FiveThree/Functional/SectionFiltersTest.php deleted file mode 100644 index cbd6823..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'; })), - ); - } - -} diff --git a/test/Mustache/Test/FiveThree/Functional/StrictCallablesTest.php b/test/Mustache/Test/FiveThree/Functional/StrictCallablesTest.php index 39b5dd9..479e038 100644 --- a/test/Mustache/Test/FiveThree/Functional/StrictCallablesTest.php +++ b/test/Mustache/Test/FiveThree/Functional/StrictCallablesTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -18,9 +18,9 @@ class Mustache_Test_FiveThree_Functional_StrictCallablesTest extends PHPUnit_Fra /** * @dataProvider callables */ - public function testStrictCallablesDisabled($name, $section, $expected) + public function testStrictCallables($strict, $name, $section, $expected) { - $mustache = new Mustache_Engine(array('strict_callables' => false)); + $mustache = new Mustache_Engine(array('strict_callables' => $strict)); $tpl = $mustache->loadTemplate('{{# section }}{{ name }}{{/ section }}'); $data = new StdClass; @@ -32,91 +32,76 @@ class Mustache_Test_FiveThree_Functional_StrictCallablesTest extends PHPUnit_Fra public function callables() { - $lambda = function($tpl, $mustache) { + $lambda = function ($tpl, $mustache) { return strtoupper($mustache->render($tpl)); }; return array( // Interpolation lambdas array( + false, array($this, 'instanceName'), $lambda, 'YOSHI', ), array( + false, array(__CLASS__, 'staticName'), $lambda, 'YOSHI', ), array( - function() { return 'Yoshi'; }, + false, + function () { return 'Yoshi'; }, $lambda, 'YOSHI', ), // Section lambdas array( + false, 'Yoshi', array($this, 'instanceCallable'), 'YOSHI', ), array( + false, 'Yoshi', array(__CLASS__, 'staticCallable'), 'YOSHI', ), array( + false, 'Yoshi', $lambda, 'YOSHI', ), - ); - } - /** - * @group wip - * @dataProvider strictCallables - */ - public function testStrictCallablesEnabled($name, $section, $expected) - { - $mustache = new Mustache_Engine(array('strict_callables' => true)); - $tpl = $mustache->loadTemplate('{{# section }}{{ name }}{{/ section }}'); - - $data = new StdClass; - $data->name = $name; - $data->section = $section; - - $this->assertEquals($expected, $tpl->render($data)); - } - - public function strictCallables() - { - $lambda = function($tpl, $mustache) { - return strtoupper($mustache->render($tpl)); - }; - - return array( - // Interpolation lambdas + // Strict interpolation lambdas array( - function() { return 'Yoshi'; }, + true, + function () { return 'Yoshi'; }, $lambda, 'YOSHI', ), - // Section lambdas + // Strict section lambdas array( + true, 'Yoshi', array($this, 'instanceCallable'), 'YoshiYoshi', ), array( + true, 'Yoshi', array(__CLASS__, 'staticCallable'), 'YoshiYoshi', ), array( + true, 'Yoshi', - function($tpl, $mustache) { + function ($tpl, $mustache) { return strtoupper($mustache->render($tpl)); }, 'YOSHI', diff --git a/test/Mustache/Test/Functional/CallTest.php b/test/Mustache/Test/Functional/CallTest.php index 4841ba9..8fafd9a 100644 --- a/test/Mustache/Test/Functional/CallTest.php +++ b/test/Mustache/Test/Functional/CallTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Functional/ExamplesTest.php b/test/Mustache/Test/Functional/ExamplesTest.php index 3c7a5a5..43eab61 100644 --- a/test/Mustache/Test/Functional/ExamplesTest.php +++ b/test/Mustache/Test/Functional/ExamplesTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/Functional/HigherOrderSectionsTest.php b/test/Mustache/Test/Functional/HigherOrderSectionsTest.php index 078988b..b042455 100644 --- a/test/Mustache/Test/Functional/HigherOrderSectionsTest.php +++ b/test/Mustache/Test/Functional/HigherOrderSectionsTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -13,9 +13,8 @@ * @group lambdas * @group functional */ -class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework_TestCase +class Mustache_Test_Functional_HigherOrderSectionsTest extends Mustache_Test_FunctionalTestCase { - private $mustache; public function setUp() @@ -23,24 +22,26 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework $this->mustache = new Mustache_Engine; } - public function testRuntimeSectionCallback() + /** + * @dataProvider sectionCallbackData + */ + public function testSectionCallback($data, $tpl, $expect) { - $tpl = $this->mustache->loadTemplate('{{#doublewrap}}{{name}}{{/doublewrap}}'); + $this->assertEquals($expect, $this->mustache->render($tpl, $data)); + } + public function sectionCallbackData() + { $foo = new Mustache_Test_Functional_Foo; $foo->doublewrap = array($foo, 'wrapWithBoth'); - $this->assertEquals(sprintf('%s', $foo->name), $tpl->render($foo)); - } + $bar = new Mustache_Test_Functional_Foo; + $bar->trimmer = array(get_class($bar), 'staticTrim'); - public function testStaticSectionCallback() - { - $tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}'); - - $foo = new Mustache_Test_Functional_Foo; - $foo->trimmer = array(get_class($foo), 'staticTrim'); - - $this->assertEquals($foo->name, $tpl->render($foo)); + return array( + array($foo, '{{#doublewrap}}{{name}}{{/doublewrap}}', sprintf('%s', $foo->name)), + array($bar, '{{#trimmer}} {{name}} {{/trimmer}}', $bar->name), + ); } public function testViewArraySectionCallback() @@ -100,6 +101,44 @@ class Mustache_Test_Functional_HigherOrderSectionsTest extends PHPUnit_Framework $this->assertEquals('' . $foo->name . '', $tpl->render($foo)); } + + /** + * @dataProvider cacheLambdaTemplatesData + */ + public function testCacheLambdaTemplatesOptionWorks($dirName, $tplPrefix, $enable, $expect) + { + $cacheDir = $this->setUpCacheDir($dirName); + $mustache = new Mustache_Engine(array( + 'template_class_prefix' => $tplPrefix, + 'cache' => $cacheDir, + 'cache_lambda_templates' => $enable, + )); + + $tpl = $mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}'); + $foo = new Mustache_Test_Functional_Foo; + $foo->wrap = array($foo, 'wrapWithEm'); + $this->assertEquals('' . $foo->name . '', $tpl->render($foo)); + $this->assertCount($expect, glob($cacheDir . '/*.php')); + } + + public function cacheLambdaTemplatesData() + { + return array( + array('test_enabling_lambda_cache', '_TestEnablingLambdaCache_', true, 2), + array('test_disabling_lambda_cache', '_TestDisablingLambdaCache_', false, 1), + ); + } + + protected function setUpCacheDir($name) + { + $cacheDir = self::$tempDir . '/' . $name; + if (file_exists($cacheDir)) { + self::rmdir($cacheDir); + } + mkdir($cacheDir, 0777, true); + + return $cacheDir; + } } class Mustache_Test_Functional_Foo @@ -112,6 +151,9 @@ class Mustache_Test_Functional_Foo return sprintf('%s', $text); } + /** + * @param string $text + */ public function wrapWithStrong($text) { return sprintf('%s', $text); diff --git a/test/Mustache/Test/Functional/MustacheInjectionTest.php b/test/Mustache/Test/Functional/MustacheInjectionTest.php index 8c95494..a36c469 100644 --- a/test/Mustache/Test/Functional/MustacheInjectionTest.php +++ b/test/Mustache/Test/Functional/MustacheInjectionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -23,105 +23,53 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T $this->mustache = new Mustache_Engine; } - // interpolation - - public function testInterpolationInjection() + /** + * @dataProvider injectionData + */ + 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 }}', 'b' => 'FAIL' ); - $this->assertEquals('{{ b }}', $tpl->render($data)); - } - - 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( + $sectionData = array( 'a' => true, 'b' => '{{ c }}', 'c' => 'FAIL' ); - $this->assertEquals('{{ c }}', $tpl->render($data)); - } - - 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 }}', - 'b' => 'FAIL' - ); - - $this->assertEquals('{{ b }}', $tpl->render($data)); - } - - 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( + $lambdaInterpolationData = array( 'a' => array($this, 'lambdaInterpolationCallback'), 'b' => '{{ c }}', '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() @@ -129,19 +77,6 @@ class Mustache_Test_Functional_MustacheInjectionTest extends PHPUnit_Framework_T 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) { return '{{ ' . $text . ' }}'; diff --git a/test/Mustache/Test/Functional/MustacheSpecTest.php b/test/Mustache/Test/Functional/MustacheSpecTest.php index 312a4c9..96fcf40 100644 --- a/test/Mustache/Test/Functional/MustacheSpecTest.php +++ b/test/Mustache/Test/Functional/MustacheSpecTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -15,16 +15,8 @@ * @group mustache-spec * @group functional */ -class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCase +class Mustache_Test_Functional_MustacheSpecTest extends Mustache_Test_SpecTestCase { - - private static $mustache; - - public static function setUpBeforeClass() - { - self::$mustache = new Mustache_Engine; - } - /** * For some reason data providers can't mark tests skipped, so this test exists * simply to provide a 'skipped' test if the `spec` submodule isn't initialized. @@ -126,50 +118,4 @@ class Mustache_Test_Functional_MustacheSpecTest extends PHPUnit_Framework_TestCa { return $this->loadSpec('sections'); } - - /** - * 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/ObjectSectionTest.php b/test/Mustache/Test/Functional/ObjectSectionTest.php index 126a683..c611ab2 100644 --- a/test/Mustache/Test/Functional/ObjectSectionTest.php +++ b/test/Mustache/Test/Functional/ObjectSectionTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/FunctionalTestCase.php b/test/Mustache/Test/FunctionalTestCase.php new file mode 100644 index 0000000..4d243e0 --- /dev/null +++ b/test/Mustache/Test/FunctionalTestCase.php @@ -0,0 +1,47 @@ +log(Mustache_Logger::CRITICAL, 'message'); $this->assertEquals("CRITICAL: message\n", file_get_contents($name)); } - public function testAcceptsResource() + public function acceptsStreamData() { - $name = tempnam(sys_get_temp_dir(), 'mustache-test'); - $file = fopen($name, 'a'); - $logger = new Mustache_Logger_StreamLogger($file); - $logger->log(Mustache_Logger::CRITICAL, 'message'); + $one = tempnam(sys_get_temp_dir(), 'mustache-test'); + $two = tempnam(sys_get_temp_dir(), 'mustache-test'); - $this->assertEquals("CRITICAL: message\n", file_get_contents($name)); + return array( + array($one, $one), + array($two, fopen($two, 'a')), + ); } /** diff --git a/test/Mustache/Test/ParserTest.php b/test/Mustache/Test/ParserTest.php index 57e418a..d51b0a6 100644 --- a/test/Mustache/Test/ParserTest.php +++ b/test/Mustache/Test/ParserTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/SpecTestCase.php b/test/Mustache/Test/SpecTestCase.php new file mode 100644 index 0000000..d4dc2c9 --- /dev/null +++ b/test/Mustache/Test/SpecTestCase.php @@ -0,0 +1,67 @@ +setPartials($partials); + + return self::$mustache->loadTemplate($source); + } + + /** + * Data provider for the mustache spec test. + * + * Loads YAML files from the spec and converts them to PHPisms. + * + * @param string $name + * + * @return array + */ + protected 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; + } +} diff --git a/test/Mustache/Test/TemplateTest.php b/test/Mustache/Test/TemplateTest.php index bafdefb..6033e53 100644 --- a/test/Mustache/Test/TemplateTest.php +++ b/test/Mustache/Test/TemplateTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/Mustache/Test/TokenizerTest.php b/test/Mustache/Test/TokenizerTest.php index 63a94d4..42df8e1 100644 --- a/test/Mustache/Test/TokenizerTest.php +++ b/test/Mustache/Test/TokenizerTest.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/bootstrap.php b/test/bootstrap.php index cf84d28..03a119f 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. @@ -11,5 +11,6 @@ require dirname(__FILE__).'/../src/Mustache/Autoloader.php'; Mustache_Autoloader::register(); +Mustache_Autoloader::register(dirname(__FILE__).'/../test'); require dirname(__FILE__).'/../vendor/yaml/lib/sfYamlParser.php'; diff --git a/test/fixtures/autoloader/Mustache/Bar.php b/test/fixtures/autoloader/Mustache/Bar.php index f731f5f..a04da7a 100644 --- a/test/fixtures/autoloader/Mustache/Bar.php +++ b/test/fixtures/autoloader/Mustache/Bar.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/fixtures/autoloader/Mustache/Foo.php b/test/fixtures/autoloader/Mustache/Foo.php index 5f16022..3114188 100644 --- a/test/fixtures/autoloader/Mustache/Foo.php +++ b/test/fixtures/autoloader/Mustache/Foo.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. diff --git a/test/fixtures/autoloader/NonMustacheClass.php b/test/fixtures/autoloader/NonMustacheClass.php index 82febb9..dbe844d 100644 --- a/test/fixtures/autoloader/NonMustacheClass.php +++ b/test/fixtures/autoloader/NonMustacheClass.php @@ -3,7 +3,7 @@ /* * This file is part of Mustache.php. * - * (c) 2013 Justin Hileman + * (c) 2010-2014 Justin Hileman * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code.