TemplateTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
  11. {
  12. /**
  13. * @dataProvider getGetAttributeTests
  14. */
  15. public function testGetAttribute($defined, $value, $object, $item, $arguments, $type)
  16. {
  17. $template = new Twig_TemplateTest(new Twig_Environment());
  18. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  19. }
  20. /**
  21. * @dataProvider getGetAttributeTests
  22. */
  23. public function testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type)
  24. {
  25. $template = new Twig_TemplateTest(
  26. new Twig_Environment(null, array('strict_variables' => true))
  27. );
  28. if ($defined) {
  29. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  30. } else {
  31. try {
  32. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  33. throw new Exception('Expected Twig_Error_Runtime exception.');
  34. } catch (Twig_Error_Runtime $e) { }
  35. }
  36. }
  37. /**
  38. * @dataProvider getGetAttributeTests
  39. */
  40. public function testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type)
  41. {
  42. $template = new Twig_TemplateTest(new Twig_Environment());
  43. $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
  44. }
  45. /**
  46. * @dataProvider getGetAttributeTests
  47. */
  48. public function testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type)
  49. {
  50. $template = new Twig_TemplateTest(
  51. new Twig_Environment(null, array('strict_variables' => true))
  52. );
  53. $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
  54. }
  55. public function getGetAttributeTests()
  56. {
  57. $array = array(
  58. 'defined' => 'defined',
  59. 'zero' => 0,
  60. 'null' => null,
  61. );
  62. $objectArray = new Twig_TemplateArrayAccessObject;
  63. $stdObject = (object) $array;
  64. $magicPropertyObject = new Twig_TemplateMagicPropertyObject;
  65. $methodObject = new Twig_TemplateMethodObject;
  66. $magicMethodObject = new Twig_TemplateMagicMethodObject;
  67. $anyType = Twig_TemplateInterface::ANY_CALL;
  68. $methodType = Twig_TemplateInterface::METHOD_CALL;
  69. $arrayType = Twig_TemplateInterface::ARRAY_CALL;
  70. $basicTests = array(
  71. // array(defined, value, property to fetch)
  72. array(true, 'defined', 'defined'),
  73. array(false, null, 'undefined'),
  74. array(true, 0, 'zero'),
  75. array(true, null, 'null'),
  76. );
  77. $testObjects = array(
  78. // array(object, type of fetch)
  79. array($array, $arrayType),
  80. array($objectArray, $arrayType),
  81. array($stdObject, $anyType),
  82. array($magicPropertyObject, $anyType),
  83. array($methodObject, $methodType),
  84. );
  85. $tests = array();
  86. foreach ($testObjects as $testObject) {
  87. foreach ($basicTests as $test) {
  88. $tests[] = array($test[0], $test[1], $testObject[0], $test[2], array(), $testObject[1]);
  89. }
  90. }
  91. // additional method tests
  92. $tests = array_merge($tests, array(
  93. array(true, 'defined', $methodObject, 'defined', array(), $methodType),
  94. array(true, 'defined', $methodObject, 'DEFINED', array(), $methodType),
  95. array(true, 'defined', $methodObject, 'getDefined', array(), $methodType),
  96. array(true, 'defined', $methodObject, 'GETDEFINED', array(), $methodType),
  97. array(true, 'static', $methodObject, 'static', array(), $methodType),
  98. array(true, 'static', $methodObject, 'getStatic', array(), $methodType),
  99. array(true, '__call_undefined', $magicMethodObject, 'undefined', array(), $methodType),
  100. array(true, '__call_UNDEFINED', $magicMethodObject, 'UNDEFINED', array(), $methodType),
  101. ));
  102. // add the same tests for the any type
  103. foreach ($tests as $test) {
  104. if ($anyType !== $test[5]) {
  105. $test[5] = $anyType;
  106. $tests[] = $test;
  107. }
  108. }
  109. return $tests;
  110. }
  111. }
  112. class Twig_TemplateTest extends Twig_Template
  113. {
  114. protected function doDisplay(array $context, array $blocks = array())
  115. {
  116. }
  117. public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false)
  118. {
  119. return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest);
  120. }
  121. }
  122. class Twig_TemplateArrayAccessObject implements ArrayAccess
  123. {
  124. public $attributes = array(
  125. 'defined' => 'defined',
  126. 'zero' => 0,
  127. 'null' => null,
  128. );
  129. public function offsetExists($name)
  130. {
  131. return array_key_exists($name, $this->attributes);
  132. }
  133. public function offsetGet($name)
  134. {
  135. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
  136. }
  137. public function offsetSet($name, $value)
  138. {
  139. }
  140. public function offsetUnset($name)
  141. {
  142. }
  143. }
  144. class Twig_TemplateMagicPropertyObject
  145. {
  146. public $attributes = array(
  147. 'defined' => 'defined',
  148. 'zero' => 0,
  149. 'null' => null,
  150. );
  151. public function __isset($name)
  152. {
  153. return array_key_exists($name, $this->attributes);
  154. }
  155. public function __get($name)
  156. {
  157. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
  158. }
  159. }
  160. class Twig_TemplateMethodObject
  161. {
  162. public function getDefined()
  163. {
  164. return 'defined';
  165. }
  166. public function getZero()
  167. {
  168. return 0;
  169. }
  170. public function getNull()
  171. {
  172. return null;
  173. }
  174. static public function getStatic()
  175. {
  176. return 'static';
  177. }
  178. }
  179. class Twig_TemplateMagicMethodObject
  180. {
  181. public function __call($method, $arguments) {
  182. return '__call_'.$method;
  183. }
  184. }