MustachePragmaImplicitIteratorTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /**
  42. * @dataProvider recursiveSectionData
  43. */
  44. public function testRecursiveSections($template, $view, $result) {
  45. $m = new Mustache();
  46. $this->assertEquals($result, $m->render($template, $view));
  47. }
  48. public function recursiveSectionData() {
  49. return array(
  50. array(
  51. '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{.}}{{/.}}{{/items}}',
  52. array('items' => array(array('a', 'b', 'c'), array('d', 'e', 'f'))),
  53. 'abcdef'
  54. ),
  55. array(
  56. '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#.}}{{.}}{{/.}}{{/.}}{{/items}}',
  57. array('items' => array(array(array('a', 'b'), array('c')), array(array('d'), array('e', 'f')))),
  58. 'abcdef'
  59. ),
  60. array(
  61. '{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#items}}{{.}}{{/items}}{{/.}}{{/items}}',
  62. array('items' => array(
  63. array('items' => array('a', 'b', 'c')),
  64. array('items' => array('d', 'e', 'f')),
  65. )),
  66. 'abcdef'
  67. ),
  68. );
  69. }
  70. }