Rearrange legacy tests.
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
class MustacheCallTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testCallEatsContext() {
|
||||
$foo = new ClassWithCall();
|
||||
$foo->name = 'Bob';
|
||||
|
||||
$template = '{{# foo }}{{ label }}: {{ name }}{{/ foo }}';
|
||||
$data = array('label' => 'name', 'foo' => $foo);
|
||||
$m = new Mustache($template, $data);
|
||||
|
||||
$this->assertEquals('name: Bob', $m->render());
|
||||
}
|
||||
}
|
||||
|
||||
class ClassWithCall {
|
||||
public $name;
|
||||
public function __call($method, $args) {
|
||||
return 'unknown value';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace Mustache\Test\Functional;
|
||||
|
||||
use Mustache\Mustache;
|
||||
|
||||
class ExamplesTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* Test everything in the `examples` directory.
|
||||
*
|
||||
* @group examples
|
||||
* @dataProvider getExamples
|
||||
*
|
||||
* @param string $context
|
||||
* @param string $source
|
||||
* @param array $partials
|
||||
* @param string $expected
|
||||
*/
|
||||
public function testExamples($context, $source, $partials, $expected) {
|
||||
$mustache = new Mustache(array(
|
||||
'partials' => $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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
class MustacheHigherOrderSectionsTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function setUp() {
|
||||
$this->foo = new Foo();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
$this->foo->wrapper = function($text) {
|
||||
return sprintf('<div class="anonymous">%s</div>', $text);
|
||||
};
|
||||
|
||||
$this->assertEquals(
|
||||
sprintf('<div class="anonymous">%s</div>', $this->foo->name),
|
||||
$this->foo->render('{{#wrapper}}{{name}}{{/wrapper}}')
|
||||
);
|
||||
}
|
||||
|
||||
public function testSectionCallback() {
|
||||
$this->assertEquals(sprintf('%s', $this->foo->name), $this->foo->render('{{name}}'));
|
||||
$this->assertEquals(sprintf('<em>%s</em>', $this->foo->name), $this->foo->render('{{#wrap}}{{name}}{{/wrap}}'));
|
||||
}
|
||||
|
||||
public function testRuntimeSectionCallback() {
|
||||
$this->foo->double_wrap = array($this->foo, 'wrapWithBoth');
|
||||
$this->assertEquals(
|
||||
sprintf('<strong><em>%s</em></strong>', $this->foo->name),
|
||||
$this->foo->render('{{#double_wrap}}{{name}}{{/double_wrap}}')
|
||||
);
|
||||
}
|
||||
|
||||
public function testStaticSectionCallback() {
|
||||
$this->foo->trimmer = array(get_class($this->foo), 'staticTrim');
|
||||
$this->assertEquals($this->foo->name, $this->foo->render('{{#trimmer}} {{name}} {{/trimmer}}'));
|
||||
}
|
||||
|
||||
public function testViewArraySectionCallback() {
|
||||
$data = array(
|
||||
'name' => 'Bob',
|
||||
'trim' => array(get_class($this->foo), 'staticTrim'),
|
||||
);
|
||||
$this->assertEquals($data['name'], $this->foo->render('{{#trim}} {{name}} {{/trim}}', $data));
|
||||
}
|
||||
|
||||
public function testViewArrayAnonymousSectionCallback() {
|
||||
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
|
||||
$this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
|
||||
return;
|
||||
}
|
||||
$data = array(
|
||||
'name' => 'Bob',
|
||||
'wrap' => function($text) {
|
||||
return sprintf('[[%s]]', $text);
|
||||
}
|
||||
);
|
||||
$this->assertEquals(
|
||||
sprintf('[[%s]]', $data['name']),
|
||||
$this->foo->render('{{#wrap}}{{name}}{{/wrap}}', $data)
|
||||
);
|
||||
}
|
||||
|
||||
public function testMonsters() {
|
||||
$frank = new Monster();
|
||||
$frank->title = 'Dr.';
|
||||
$frank->name = 'Frankenstein';
|
||||
$this->assertEquals('Dr. Frankenstein', $frank->render());
|
||||
|
||||
$dracula = new Monster();
|
||||
$dracula->title = 'Count';
|
||||
$dracula->name = 'Dracula';
|
||||
$this->assertEquals('Count Dracula', $dracula->render());
|
||||
}
|
||||
}
|
||||
|
||||
class Foo extends Mustache {
|
||||
public $name = 'Justin';
|
||||
public $lorem = 'Lorem ipsum dolor sit amet,';
|
||||
public $wrap;
|
||||
|
||||
public function __construct($template = null, $view = null, $partials = null) {
|
||||
$this->wrap = array($this, 'wrapWithEm');
|
||||
parent::__construct($template, $view, $partials);
|
||||
}
|
||||
|
||||
public function wrapWithEm($text) {
|
||||
return sprintf('<em>%s</em>', $text);
|
||||
}
|
||||
|
||||
public function wrapWithStrong($text) {
|
||||
return sprintf('<strong>%s</strong>', $text);
|
||||
}
|
||||
|
||||
public function wrapWithBoth($text) {
|
||||
return self::wrapWithStrong(self::wrapWithEm($text));
|
||||
}
|
||||
|
||||
public static function staticTrim($text) {
|
||||
return trim($text);
|
||||
}
|
||||
}
|
||||
|
||||
class Monster extends Mustache {
|
||||
public $_template = '{{#title}}{{title}} {{/title}}{{name}}';
|
||||
public $title;
|
||||
public $name;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
/**
|
||||
* @group mustache_injection
|
||||
*/
|
||||
class MustacheInjectionSectionTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
// interpolation
|
||||
|
||||
public function testInterpolationInjection() {
|
||||
$data = array(
|
||||
'a' => '{{ b }}',
|
||||
'b' => 'FAIL'
|
||||
);
|
||||
$template = '{{ a }}';
|
||||
$output = '{{ b }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
public function testUnescapedInterpolationInjection() {
|
||||
$data = array(
|
||||
'a' => '{{ b }}',
|
||||
'b' => 'FAIL'
|
||||
);
|
||||
$template = '{{{ a }}}';
|
||||
$output = '{{ b }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
|
||||
// sections
|
||||
|
||||
public function testSectionInjection() {
|
||||
$data = array(
|
||||
'a' => true,
|
||||
'b' => '{{ c }}',
|
||||
'c' => 'FAIL'
|
||||
);
|
||||
$template = '{{# a }}{{ b }}{{/ a }}';
|
||||
$output = '{{ c }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
public function testUnescapedSectionInjection() {
|
||||
$data = array(
|
||||
'a' => true,
|
||||
'b' => '{{ c }}',
|
||||
'c' => 'FAIL'
|
||||
);
|
||||
$template = '{{# a }}{{{ b }}}{{/ a }}';
|
||||
$output = '{{ c }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
|
||||
// partials
|
||||
|
||||
public function testPartialInjection() {
|
||||
$data = array(
|
||||
'a' => '{{ b }}',
|
||||
'b' => 'FAIL'
|
||||
);
|
||||
$template = '{{> partial }}';
|
||||
$partials = array(
|
||||
'partial' => '{{ a }}',
|
||||
);
|
||||
$output = '{{ b }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data, $partials));
|
||||
}
|
||||
|
||||
public function testPartialUnescapedInjection() {
|
||||
$data = array(
|
||||
'a' => '{{ b }}',
|
||||
'b' => 'FAIL'
|
||||
);
|
||||
$template = '{{> partial }}';
|
||||
$partials = array(
|
||||
'partial' => '{{{ a }}}',
|
||||
);
|
||||
$output = '{{ b }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data, $partials));
|
||||
}
|
||||
|
||||
|
||||
// lambdas
|
||||
|
||||
public function testLambdaInterpolationInjection() {
|
||||
$data = array(
|
||||
'a' => array($this, 'interpolationLambda'),
|
||||
'b' => '{{ c }}',
|
||||
'c' => 'FAIL'
|
||||
);
|
||||
$template = '{{ a }}';
|
||||
$output = '{{ c }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
public function interpolationLambda() {
|
||||
return '{{ b }}';
|
||||
}
|
||||
|
||||
public function testLambdaSectionInjection() {
|
||||
$data = array(
|
||||
'a' => array($this, 'sectionLambda'),
|
||||
'b' => '{{ c }}',
|
||||
'c' => 'FAIL'
|
||||
);
|
||||
$template = '{{# a }}b{{/ a }}';
|
||||
$output = '{{ c }}';
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($output, $m->render($template, $data));
|
||||
}
|
||||
|
||||
public function sectionLambda($content) {
|
||||
return '{{ ' . $content . ' }}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
require_once './lib/yaml/lib/sfYamlParser.php';
|
||||
|
||||
/**
|
||||
* A PHPUnit test case wrapping the Mustache Spec
|
||||
*
|
||||
* @group mustache-spec
|
||||
*/
|
||||
class MustacheSpecTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public function testSpecInitialized() {
|
||||
$spec_dir = dirname(__FILE__) . '/spec/specs/';
|
||||
if (!file_exists($spec_dir)) {
|
||||
$this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @group comments
|
||||
* @dataProvider loadCommentSpec
|
||||
*/
|
||||
public function testCommentSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group delimiters
|
||||
* @dataProvider loadDelimitersSpec
|
||||
*/
|
||||
public function testDelimitersSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group interpolation
|
||||
* @dataProvider loadInterpolationSpec
|
||||
*/
|
||||
public function testInterpolationSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group inverted-sections
|
||||
* @dataProvider loadInvertedSpec
|
||||
*/
|
||||
public function testInvertedSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group lambdas
|
||||
* @dataProvider loadLambdasSpec
|
||||
*/
|
||||
public function testLambdasSpec($desc, $template, $data, $partials, $expected) {
|
||||
if (!version_compare(PHP_VERSION, '5.3.0', '>=')) {
|
||||
$this->markTestSkipped('Unable to test Lambdas spec with PHP < 5.3.');
|
||||
}
|
||||
|
||||
$data = $this->prepareLambdasSpec($data);
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract and lambdafy any 'lambda' values found in the $data array.
|
||||
*/
|
||||
protected 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
|
||||
*/
|
||||
public function testPartialsSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group sections
|
||||
* @dataProvider loadSectionsSpec
|
||||
*/
|
||||
public function testSectionsSpec($desc, $template, $data, $partials, $expected) {
|
||||
$m = new Mustache($template, $data, $partials);
|
||||
$this->assertEquals($expected, $m->render(), $desc);
|
||||
}
|
||||
|
||||
public function loadCommentSpec() {
|
||||
return $this->loadSpec('comments');
|
||||
}
|
||||
|
||||
public function loadDelimitersSpec() {
|
||||
return $this->loadSpec('delimiters');
|
||||
}
|
||||
|
||||
public function loadInterpolationSpec() {
|
||||
return $this->loadSpec('interpolation');
|
||||
}
|
||||
|
||||
public function loadInvertedSpec() {
|
||||
return $this->loadSpec('inverted');
|
||||
}
|
||||
|
||||
public function loadLambdasSpec() {
|
||||
return $this->loadSpec('~lambdas');
|
||||
}
|
||||
|
||||
public function loadPartialsSpec() {
|
||||
return $this->loadSpec('partials');
|
||||
}
|
||||
|
||||
public function loadSectionsSpec() {
|
||||
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
|
||||
*/
|
||||
protected function loadSpec($name) {
|
||||
$filename = dirname(__FILE__) . '/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'],
|
||||
$test['data'],
|
||||
isset($test['partials']) ? $test['partials'] : array(),
|
||||
$test['expected'],
|
||||
);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
/**
|
||||
* @group sections
|
||||
*/
|
||||
class MustacheObjectSectionTest extends PHPUnit_Framework_TestCase {
|
||||
public function testBasicObject() {
|
||||
$alpha = new Alpha();
|
||||
$this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
|
||||
}
|
||||
|
||||
public function testObjectWithGet() {
|
||||
$beta = new Beta();
|
||||
$this->assertEquals('Foo', $beta->render('{{#foo}}{{name}}{{/foo}}'));
|
||||
}
|
||||
|
||||
public function testSectionObjectWithGet() {
|
||||
$gamma = new Gamma();
|
||||
$this->assertEquals('Foo', $gamma->render('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}'));
|
||||
}
|
||||
|
||||
public function testSectionObjectWithFunction() {
|
||||
$alpha = new Alpha();
|
||||
$alpha->foo = new Delta();
|
||||
$this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
|
||||
}
|
||||
}
|
||||
|
||||
class Alpha extends Mustache {
|
||||
public $foo;
|
||||
|
||||
public function __construct() {
|
||||
$this->foo = new StdClass();
|
||||
$this->foo->name = 'Foo';
|
||||
$this->foo->number = 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Beta extends Mustache {
|
||||
protected $_data = array();
|
||||
|
||||
public function __construct() {
|
||||
$this->_data['foo'] = new StdClass();
|
||||
$this->_data['foo']->name = 'Foo';
|
||||
$this->_data['foo']->number = 1;
|
||||
}
|
||||
|
||||
public function __isset($name) {
|
||||
return array_key_exists($name, $this->_data);
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
return $this->_data[$name];
|
||||
}
|
||||
}
|
||||
|
||||
class Gamma extends Mustache {
|
||||
public $bar;
|
||||
|
||||
public function __construct() {
|
||||
$this->bar = new Beta();
|
||||
}
|
||||
}
|
||||
|
||||
class Delta extends Mustache {
|
||||
protected $_name = 'Foo';
|
||||
|
||||
public function name() {
|
||||
return $this->_name;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user