MustacheObjectSectionTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * @group sections
  5. */
  6. class MustacheObjectSectionTest extends PHPUnit_Framework_TestCase {
  7. public function testBasicObject() {
  8. $alpha = new Alpha();
  9. $this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
  10. }
  11. public function testObjectWithGet() {
  12. $beta = new Beta();
  13. $this->assertEquals('Foo', $beta->render('{{#foo}}{{name}}{{/foo}}'));
  14. }
  15. public function testSectionObjectWithGet() {
  16. $gamma = new Gamma();
  17. $this->assertEquals('Foo', $gamma->render('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}'));
  18. }
  19. public function testSectionObjectWithFunction() {
  20. $alpha = new Alpha();
  21. $alpha->foo = new Delta();
  22. $this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}'));
  23. }
  24. }
  25. class Alpha extends Mustache {
  26. public $foo;
  27. public function __construct() {
  28. $this->foo = new StdClass();
  29. $this->foo->name = 'Foo';
  30. $this->foo->number = 1;
  31. }
  32. }
  33. class Beta extends Mustache {
  34. protected $_data = array();
  35. public function __construct() {
  36. $this->_data['foo'] = new StdClass();
  37. $this->_data['foo']->name = 'Foo';
  38. $this->_data['foo']->number = 1;
  39. }
  40. public function __isset($name) {
  41. return array_key_exists($name, $this->_data);
  42. }
  43. public function __get($name) {
  44. return $this->_data[$name];
  45. }
  46. }
  47. class Gamma extends Mustache {
  48. public $bar;
  49. public function __construct() {
  50. $this->bar = new Beta();
  51. }
  52. }
  53. class Delta extends Mustache {
  54. protected $_name = 'Foo';
  55. public function name() {
  56. return $this->_name;
  57. }
  58. }