ObjectSectionTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * @group sections
  12. * @group functional
  13. */
  14. class Mustache_Test_Functional_ObjectSectionTest extends PHPUnit_Framework_TestCase
  15. {
  16. private $mustache;
  17. public function setUp()
  18. {
  19. $this->mustache = new Mustache_Engine();
  20. }
  21. public function testBasicObject()
  22. {
  23. $tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
  24. $this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Alpha()));
  25. }
  26. /**
  27. * @group magic_methods
  28. */
  29. public function testObjectWithGet()
  30. {
  31. $tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
  32. $this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Beta()));
  33. }
  34. /**
  35. * @group magic_methods
  36. */
  37. public function testSectionObjectWithGet()
  38. {
  39. $tpl = $this->mustache->loadTemplate('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}');
  40. $this->assertEquals('Foo', $tpl->render(new Mustache_Test_Functional_Gamma()));
  41. }
  42. public function testSectionObjectWithFunction()
  43. {
  44. $tpl = $this->mustache->loadTemplate('{{#foo}}{{name}}{{/foo}}');
  45. $alpha = new Mustache_Test_Functional_Alpha();
  46. $alpha->foo = new Mustache_Test_Functional_Delta();
  47. $this->assertEquals('Foo', $tpl->render($alpha));
  48. }
  49. }
  50. class Mustache_Test_Functional_Alpha
  51. {
  52. public $foo;
  53. public function __construct()
  54. {
  55. $this->foo = new StdClass();
  56. $this->foo->name = 'Foo';
  57. $this->foo->number = 1;
  58. }
  59. }
  60. class Mustache_Test_Functional_Beta
  61. {
  62. protected $_data = array();
  63. public function __construct()
  64. {
  65. $this->_data['foo'] = new StdClass();
  66. $this->_data['foo']->name = 'Foo';
  67. $this->_data['foo']->number = 1;
  68. }
  69. public function __isset($name)
  70. {
  71. return array_key_exists($name, $this->_data);
  72. }
  73. public function __get($name)
  74. {
  75. return $this->_data[$name];
  76. }
  77. }
  78. class Mustache_Test_Functional_Gamma
  79. {
  80. public $bar;
  81. public function __construct()
  82. {
  83. $this->bar = new Mustache_Test_Functional_Beta();
  84. }
  85. }
  86. class Mustache_Test_Functional_Delta
  87. {
  88. protected $_name = 'Foo';
  89. public function name()
  90. {
  91. return $this->_name;
  92. }
  93. }