MustachePragmaDotNotationTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * @group pragmas
  5. */
  6. class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
  7. public function testDotTraversal() {
  8. $m = new Mustache('', array('foo' => array('bar' => 'this worked')));
  9. $this->assertEquals($m->render('{{foo.bar}}'), '',
  10. 'Dot notation not enabled, variable should have been replaced with nothing');
  11. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked',
  12. 'Dot notation enabled, variable should have been replaced by "this worked"');
  13. }
  14. public function testDeepTraversal() {
  15. $data = array(
  16. 'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))),
  17. 'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))),
  18. 'one' => array(
  19. 'one' => 'one-one',
  20. 'two' => 'one-two',
  21. 'three' => 'one-three',
  22. ),
  23. );
  24. $m = new Mustache('', $data);
  25. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!');
  26. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs');
  27. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three');
  28. }
  29. public function testDotNotationContext() {
  30. $data = array('parent' => array('items' => array(
  31. array('item' => array('index' => 1)),
  32. array('item' => array('index' => 2)),
  33. array('item' => array('index' => 3)),
  34. array('item' => array('index' => 4)),
  35. array('item' => array('index' => 5)),
  36. )));
  37. $m = new Mustache('', $data);
  38. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}'));
  39. }
  40. public function testDotNotationSectionNames() {
  41. $data = array('parent' => array('items' => array(
  42. array('item' => array('index' => 1)),
  43. array('item' => array('index' => 2)),
  44. array('item' => array('index' => 3)),
  45. array('item' => array('index' => 4)),
  46. array('item' => array('index' => 5)),
  47. )));
  48. $m = new Mustache('', $data);
  49. $this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}'));
  50. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}'));
  51. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}'));
  52. }
  53. }