MustachePragmaImplicitIteratorTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * @group pragmas
  5. */
  6. class MustachePragmaImplicitIteratorTest extends PHPUnit_Framework_TestCase {
  7. public function testEnablePragma() {
  8. $m = $this->getMock('Mustache', array('_renderPragma'), array('{{%IMPLICIT-ITERATOR}}'));
  9. $m->expects($this->exactly(1))
  10. ->method('_renderPragma')
  11. ->with(array('{{%IMPLICIT-ITERATOR}}', 'IMPLICIT-ITERATOR', null));
  12. $m->render();
  13. }
  14. public function testImplicitIterator() {
  15. $m1 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array('a', 'b', 'c')));
  16. $this->assertEquals('abc', $m1->render());
  17. $m2 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array(1, 2, 3)));
  18. $this->assertEquals('123', $m2->render());
  19. }
  20. public function testDotNotationCollision() {
  21. $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
  22. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{%DOT-NOTATION}}{{#items}}{{.}}{{/items}}'));
  23. $this->assertEquals('foobarbaz', $m->render('{{%DOT-NOTATION}}{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
  24. }
  25. public function testCustomIterator() {
  26. $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
  27. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
  28. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}}'));
  29. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=items}}{{#items}}{{items}}{{/items}}'));
  30. }
  31. public function testDotNotationContext() {
  32. $m = new Mustache(null, array('items' => array(
  33. array('index' => 1, 'name' => 'foo'),
  34. array('index' => 2, 'name' => 'bar'),
  35. array('index' => 3, 'name' => 'baz'),
  36. )));
  37. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{name}}{{/.}}{{/items}}'));
  38. $this->assertEquals('123', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.index}}{{/items}}'));
  39. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.name}}{{/items}}'));
  40. }
  41. }