Add test coverage for Mustache.php 2.0

Update legacy tests for new codebase.
This commit is contained in:
Justin Hileman
2012-02-29 19:12:22 -08:00
parent 8710e4b8a4
commit 0034bf657b
16 changed files with 1123 additions and 707 deletions
+12 -5
View File
@@ -1,18 +1,25 @@
<?php
require_once '../Mustache.php';
namespace Mustache\Test\Functional;
class MustacheCallTest extends PHPUnit_Framework_TestCase {
use Mustache\Mustache;
/**
* @group magic_methods
* @group functional
*/
class CallTest extends \PHPUnit_Framework_TestCase {
public function testCallEatsContext() {
$m = new Mustache;
$tpl = $m->loadTemplate('{{# foo }}{{ label }}: {{ name }}{{/ foo }}');
$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());
$this->assertEquals('name: Bob', $tpl->render($data));
}
}
@@ -4,12 +4,15 @@ namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group examples
* @group functional
*/
class ExamplesTest extends \PHPUnit_Framework_TestCase {
/**
* Test everything in the `examples` directory.
*
* @group examples
* @dataProvider getExamples
*
* @param string $context
@@ -1,93 +1,115 @@
<?php
require_once '../Mustache.php';
namespace Mustache\Test\Functional;
class MustacheHigherOrderSectionsTest extends PHPUnit_Framework_TestCase {
use Mustache\Mustache;
/**
* @group lambdas
* @group functional
*/
class HigherOrderSectionsTest extends \PHPUnit_Framework_TestCase {
private $mustache;
public function setUp() {
$this->foo = new Foo();
$this->mustache = new Mustache;
}
public function testAnonymousFunctionSectionCallback() {
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
$this->markTestSkipped('Unable to test anonymous function section callbacks in PHP < 5.3');
return;
}
$tpl = $this->mustache->loadTemplate('{{#wrapper}}{{name}}{{/wrapper}}');
$this->foo->wrapper = function($text) {
$foo = new Foo;
$foo->name = 'Mario';
$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}}')
);
$this->assertEquals(sprintf('<div class="anonymous">%s</div>', $foo->name), $tpl->render($foo));
}
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}}'));
$one = $this->mustache->loadTemplate('{{name}}');
$two = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
$foo = new Foo;
$foo->name = 'Luigi';
$this->assertEquals($foo->name, $one->render($foo));
$this->assertEquals(sprintf('<em>%s</em>', $foo->name), $two->render($foo));
}
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}}')
);
$tpl = $this->mustache->loadTemplate('{{#double_wrap}}{{name}}{{/double_wrap}}');
$foo = new Foo;
$foo->double_wrap = array($foo, 'wrapWithBoth');
$this->assertEquals(sprintf('<strong><em>%s</em></strong>', $foo->name), $tpl->render($foo));
}
public function testStaticSectionCallback() {
$this->foo->trimmer = array(get_class($this->foo), 'staticTrim');
$this->assertEquals($this->foo->name, $this->foo->render('{{#trimmer}} {{name}} {{/trimmer}}'));
$tpl = $this->mustache->loadTemplate('{{#trimmer}} {{name}} {{/trimmer}}');
$foo = new Foo;
$foo->trimmer = array(get_class($foo), 'staticTrim');
$this->assertEquals($foo->name, $tpl->render($foo));
}
public function testViewArraySectionCallback() {
$tpl = $this->mustache->loadTemplate('{{#trim}} {{name}} {{/trim}}');
$foo = new Foo;
$data = array(
'name' => 'Bob',
'trim' => array(get_class($this->foo), 'staticTrim'),
'trim' => array(get_class($foo), 'staticTrim'),
);
$this->assertEquals($data['name'], $this->foo->render('{{#trim}} {{name}} {{/trim}}', $data));
$this->assertEquals($data['name'], $tpl->render($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;
}
$tpl = $this->mustache->loadTemplate('{{#wrap}}{{name}}{{/wrap}}');
$data = array(
'name' => 'Bob',
'wrap' => function($text) {
return sprintf('[[%s]]', $text);
}
);
$this->assertEquals(
sprintf('[[%s]]', $data['name']),
$this->foo->render('{{#wrap}}{{name}}{{/wrap}}', $data)
$tpl->render($data)
);
}
public function testMonsters() {
$tpl = $this->mustache->loadTemplate('{{#title}}{{title}} {{/title}}{{name}}');
$frank = new Monster();
$frank->title = 'Dr.';
$frank->name = 'Frankenstein';
$this->assertEquals('Dr. Frankenstein', $frank->render());
$this->assertEquals('Dr. Frankenstein', $tpl->render($frank));
$dracula = new Monster();
$dracula->title = 'Count';
$dracula->name = 'Dracula';
$this->assertEquals('Count Dracula', $dracula->render());
$this->assertEquals('Count Dracula', $tpl->render($dracula));
}
}
class Foo extends Mustache {
class Foo {
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 __construct() {
$this->wrap = function($text) {
return sprintf('<em>%s</em>', $text);
};
}
public function wrapWithEm($text) {
@@ -107,8 +129,7 @@ class Foo extends Mustache {
}
}
class Monster extends Mustache {
public $_template = '{{#title}}{{title}} {{/title}}{{name}}';
class Monster {
public $title;
public $name;
}
}
@@ -1,127 +1,127 @@
<?php
require_once '../Mustache.php';
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group mustache_injection
* @group functional
*/
class MustacheInjectionSectionTest extends PHPUnit_Framework_TestCase {
class MustacheInjectionTest extends \PHPUnit_Framework_TestCase {
// interpolation
private $mustache;
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 setUp() {
$this->mustache = new Mustache;
}
public function testUnescapedInterpolationInjection() {
$data = array(
'a' => '{{ b }}',
'b' => 'FAIL'
);
$template = '{{{ a }}}';
$output = '{{ b }}';
$m = new Mustache();
$this->assertEquals($output, $m->render($template, $data));
}
// interpolation
public function testInterpolationInjection() {
$tpl = $this->mustache->loadTemplate('{{ a }}');
$data = 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
// 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 testSectionInjection() {
$tpl = $this->mustache->loadTemplate('{{# a }}{{ b }}{{/ a }}');
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));
}
$data = 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
// 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 testPartialInjection() {
$tpl = $this->mustache->loadTemplate('{{> partial }}');
$this->mustache->setPartials(array(
'partial' => '{{ a }}',
));
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));
}
$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
// 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 testLambdaInterpolationInjection() {
$tpl = $this->mustache->loadTemplate('{{ a }}');
public function interpolationLambda() {
return '{{ b }}';
}
$data = array(
'a' => function() { return '{{ b }}'; },
'b' => '{{ c }}',
'c' => 'FAIL'
);
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));
}
$this->assertEquals('{{ c }}', $tpl->render($data));
}
public function sectionLambda($content) {
return '{{ ' . $content . ' }}';
}
public function testLambdaSectionInjection() {
$tpl = $this->mustache->loadTemplate('{{# a }}b{{/ a }}');
}
$data = array(
'a' => function ($text) { return '{{ ' . $text . ' }}'; },
'b' => '{{ c }}',
'c' => 'FAIL'
);
$this->assertEquals('{{ c }}', $tpl->render($data));
}
}
@@ -1,22 +1,30 @@
<?php
require_once '../Mustache.php';
require_once './lib/yaml/lib/sfYamlParser.php';
namespace Mustache\Test\Functional;
use Mustache\Mustache;
use Mustache\Loader\StringLoader;
/**
* A PHPUnit test case wrapping the Mustache Spec
*
* @group mustache-spec
* @group functional
*/
class MustacheSpecTest extends PHPUnit_Framework_TestCase {
class MustacheSpecTest extends \PHPUnit_Framework_TestCase {
private static $mustache;
public static function setUpBeforeClass() {
self::$mustache = new Mustache;
}
/**
* 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)) {
if (!file_exists(__DIR__.'/../../../spec/specs/')) {
$this->markTestSkipped('Mustache spec submodule not initialized: run "git submodule update --init"');
}
}
@@ -25,56 +33,72 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
* @group comments
* @dataProvider loadCommentSpec
*/
public function testCommentSpec($desc, $template, $data, $partials, $expected) {
$m = new Mustache($template, $data, $partials);
$this->assertEquals($expected, $m->render(), $desc);
public function testCommentSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadCommentSpec() {
return $this->loadSpec('comments');
}
/**
* @group delimiters
* @dataProvider loadDelimitersSpec
*/
public function testDelimitersSpec($desc, $template, $data, $partials, $expected) {
$m = new Mustache($template, $data, $partials);
$this->assertEquals($expected, $m->render(), $desc);
public function testDelimitersSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadDelimitersSpec() {
return $this->loadSpec('delimiters');
}
/**
* @group interpolation
* @dataProvider loadInterpolationSpec
*/
public function testInterpolationSpec($desc, $template, $data, $partials, $expected) {
$m = new Mustache($template, $data, $partials);
$this->assertEquals($expected, $m->render(), $desc);
public function testInterpolationSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadInterpolationSpec() {
return $this->loadSpec('interpolation');
}
/**
* @group inverted
* @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);
public function testInvertedSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadInvertedSpec() {
return $this->loadSpec('inverted');
}
/**
* @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.');
}
public function testLambdasSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($this->prepareLambdasSpec($data)), $desc);
}
$data = $this->prepareLambdasSpec($data);
$m = new Mustache($template, $data, $partials);
$this->assertEquals($expected, $m->render(), $desc);
public function loadLambdasSpec() {
return $this->loadSpec('~lambdas');
}
/**
* Extract and lambdafy any 'lambda' values found in the $data array.
*/
protected function prepareLambdasSpec($data) {
private function prepareLambdasSpec($data) {
foreach ($data as $key => $val) {
if ($key === 'lambda') {
if (!isset($val['php'])) {
@@ -94,42 +118,22 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
* @group partials
* @dataProvider loadPartialsSpec
*/
public function testPartialsSpec($desc, $template, $data, $partials, $expected) {
$m = new Mustache($template, $data, $partials);
$this->assertEquals($expected, $m->render(), $desc);
public function testPartialsSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadPartialsSpec() {
return $this->loadSpec('partials');
}
/**
* @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 testSectionsSpec($desc, $source, $partials, $data, $expected) {
$template = self::loadTemplate($source, $partials);
$this->assertEquals($expected, $template($data), $desc);
}
public function loadSectionsSpec() {
@@ -144,14 +148,14 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
* @access public
* @return array
*/
protected function loadSpec($name) {
$filename = dirname(__FILE__) . '/spec/specs/' . $name . '.yml';
private function loadSpec($name) {
$filename = __DIR__ . '/../../../spec/specs/' . $name . '.yml';
if (!file_exists($filename)) {
return array();
}
$data = array();
$yaml = new sfYamlParser();
$yaml = new \sfYamlParser;
$file = file_get_contents($filename);
// @hack: pre-process the 'lambdas' spec so the Symfony YAML parser doesn't complain.
@@ -160,15 +164,23 @@ class MustacheSpecTest extends PHPUnit_Framework_TestCase {
}
$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['data'],
$test['expected'],
);
}
return $data;
}
}
private static function loadTemplate($source, $partials) {
self::$mustache->setPartials($partials);
return self::$mustache->loadTemplate($source);
}
}
@@ -1,48 +1,64 @@
<?php
require_once '../Mustache.php';
namespace Mustache\Test\Functional;
use Mustache\Mustache;
/**
* @group sections
* @group functional
*/
class MustacheObjectSectionTest extends PHPUnit_Framework_TestCase {
class ObjectSectionTest extends \PHPUnit_Framework_TestCase {
private $mustache;
public function setUp() {
$this->mustache = new Mustache;
}
public function testBasicObject() {
$alpha = new Alpha();
$this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Alpha));
}
/**
* @group magic_methods
*/
public function testObjectWithGet() {
$beta = new Beta();
$this->assertEquals('Foo', $beta->render('{{#foo}}{{name}}{{/foo}}'));
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$this->assertEquals('Foo', $tpl->render(new Beta));
}
/**
* @group magic_methods
*/
public function testSectionObjectWithGet() {
$gamma = new Gamma();
$this->assertEquals('Foo', $gamma->render('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}'));
$tpl = $this->mustache->loadTemplate('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}');
$this->assertEquals('Foo', $tpl->render(new Gamma));
}
public function testSectionObjectWithFunction() {
$alpha = new Alpha();
$alpha->foo = new Delta();
$this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
$tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
$alpha = new Alpha;
$alpha->foo = new Delta;
$this->assertEquals('Foo', $tpl->render($alpha));
}
}
class Alpha extends Mustache {
class Alpha {
public $foo;
public function __construct() {
$this->foo = new StdClass();
$this->foo = new \StdClass();
$this->foo->name = 'Foo';
$this->foo->number = 1;
}
}
class Beta extends Mustache {
class Beta {
protected $_data = array();
public function __construct() {
$this->_data['foo'] = new StdClass();
$this->_data['foo'] = new \StdClass();
$this->_data['foo']->name = 'Foo';
$this->_data['foo']->number = 1;
}
@@ -56,7 +72,7 @@ class Beta extends Mustache {
}
}
class Gamma extends Mustache {
class Gamma {
public $bar;
public function __construct() {
@@ -64,10 +80,10 @@ class Gamma extends Mustache {
}
}
class Delta extends Mustache {
class Delta {
protected $_name = 'Foo';
public function name() {
return $this->_name;
}
}
}