TemplateTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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, $useExt = false)
  16. {
  17. $template = new Twig_TemplateTest(
  18. new Twig_Environment(),
  19. $useExt
  20. );
  21. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  22. }
  23. /**
  24. * @dataProvider getGetAttributeTests
  25. */
  26. public function testGetAttributeStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false)
  27. {
  28. $template = new Twig_TemplateTest(
  29. new Twig_Environment(null, array('strict_variables' => true)),
  30. $useExt
  31. );
  32. if ($defined) {
  33. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  34. } else {
  35. try {
  36. $this->assertEquals($value, $template->getAttribute($object, $item, $arguments, $type));
  37. throw new Exception('Expected Twig_Error_Runtime exception.');
  38. } catch (Twig_Error_Runtime $e) { }
  39. }
  40. }
  41. /**
  42. * @dataProvider getGetAttributeTests
  43. */
  44. public function testGetAttributeDefined($defined, $value, $object, $item, $arguments, $type, $useExt = false)
  45. {
  46. $template = new Twig_TemplateTest(
  47. new Twig_Environment(),
  48. $useExt
  49. );
  50. $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
  51. }
  52. /**
  53. * @dataProvider getGetAttributeTests
  54. */
  55. public function testGetAttributeDefinedStrict($defined, $value, $object, $item, $arguments, $type, $useExt = false)
  56. {
  57. $template = new Twig_TemplateTest(
  58. new Twig_Environment(null, array('strict_variables' => true)),
  59. $useExt
  60. );
  61. $this->assertEquals($defined, $template->getAttribute($object, $item, $arguments, $type, true));
  62. }
  63. public function getGetAttributeTests()
  64. {
  65. $array = array(
  66. 'defined' => 'defined',
  67. 'zero' => 0,
  68. 'null' => null,
  69. '1' => 1,
  70. );
  71. $objectArray = new Twig_TemplateArrayAccessObject();
  72. $stdObject = (object) $array;
  73. $magicPropertyObject = new Twig_TemplateMagicPropertyObject();
  74. $propertyObject = new Twig_TemplatePropertyObject();
  75. $propertyObject1 = new Twig_TemplatePropertyObjectAndIterator();
  76. $methodObject = new Twig_TemplateMethodObject();
  77. $magicMethodObject = new Twig_TemplateMagicMethodObject();
  78. $anyType = Twig_TemplateInterface::ANY_CALL;
  79. $methodType = Twig_TemplateInterface::METHOD_CALL;
  80. $arrayType = Twig_TemplateInterface::ARRAY_CALL;
  81. $basicTests = array(
  82. // array(defined, value, property to fetch)
  83. array(true, 'defined', 'defined'),
  84. array(false, null, 'undefined'),
  85. array(false, null, 'protected'),
  86. array(true, 0, 'zero'),
  87. array(true, 1, 1),
  88. array(true, 1, 1.0),
  89. array(true, null, 'null'),
  90. );
  91. $testObjects = array(
  92. // array(object, type of fetch)
  93. array($array, $arrayType),
  94. array($objectArray, $arrayType),
  95. array($stdObject, $anyType),
  96. array($magicPropertyObject, $anyType),
  97. array($methodObject, $methodType),
  98. array($methodObject, $anyType),
  99. array($propertyObject, $anyType),
  100. array($propertyObject1, $anyType),
  101. );
  102. $tests = array();
  103. foreach ($testObjects as $testObject) {
  104. foreach ($basicTests as $test) {
  105. // properties cannot be numbers
  106. if (($testObject[0] instanceof stdClass || $testObject[0] instanceof Twig_TemplatePropertyObject) && is_numeric($test[2])) {
  107. continue;
  108. }
  109. $tests[] = array($test[0], $test[1], $testObject[0], $test[2], array(), $testObject[1]);
  110. }
  111. }
  112. // additional method tests
  113. $tests = array_merge($tests, array(
  114. array(true, 'defined', $methodObject, 'defined', array(), $methodType),
  115. array(true, 'defined', $methodObject, 'DEFINED', array(), $methodType),
  116. array(true, 'defined', $methodObject, 'getDefined', array(), $methodType),
  117. array(true, 'defined', $methodObject, 'GETDEFINED', array(), $methodType),
  118. array(true, 'static', $methodObject, 'static', array(), $methodType),
  119. array(true, 'static', $methodObject, 'getStatic', array(), $methodType),
  120. array(true, '__call_undefined', $magicMethodObject, 'undefined', array(), $methodType),
  121. array(true, '__call_UNDEFINED', $magicMethodObject, 'UNDEFINED', array(), $methodType),
  122. ));
  123. // add the same tests for the any type
  124. foreach ($tests as $test) {
  125. if ($anyType !== $test[5]) {
  126. $test[5] = $anyType;
  127. $tests[] = $test;
  128. }
  129. }
  130. $methodAndPropObject = new Twig_TemplateMethodAndPropObject;
  131. // additional method tests
  132. $tests = array_merge($tests, array(
  133. array(true, 'a', $methodAndPropObject, 'a', array(), $anyType),
  134. array(true, 'a', $methodAndPropObject, 'a', array(), $methodType),
  135. array(false, null, $methodAndPropObject, 'a', array(), $arrayType),
  136. array(true, 'b_prop', $methodAndPropObject, 'b', array(), $anyType),
  137. array(true, 'b', $methodAndPropObject, 'B', array(), $anyType),
  138. array(true, 'b', $methodAndPropObject, 'b', array(), $methodType),
  139. array(true, 'b', $methodAndPropObject, 'B', array(), $methodType),
  140. array(false, null, $methodAndPropObject, 'b', array(), $arrayType),
  141. array(false, null, $methodAndPropObject, 'c', array(), $anyType),
  142. array(false, null, $methodAndPropObject, 'c', array(), $methodType),
  143. array(false, null, $methodAndPropObject, 'c', array(), $arrayType),
  144. ));
  145. // add twig_template_get_attributes tests
  146. if (function_exists('twig_template_get_attributes')) {
  147. foreach(array_slice($tests, 0) as $test) {
  148. $test[] = true;
  149. $tests[] = $test;
  150. }
  151. }
  152. return $tests;
  153. }
  154. public function useExtGetAttribute()
  155. {
  156. return false;
  157. }
  158. }
  159. class Twig_TemplateTest extends Twig_Template
  160. {
  161. protected $useExtGetAttribute = false;
  162. public function __construct(Twig_Environment $env, $useExtGetAttribute = false)
  163. {
  164. parent::__construct($env);
  165. $this->useExtGetAttribute = $useExtGetAttribute;
  166. Twig_Template::clearCache();
  167. }
  168. public function getTemplateName()
  169. {
  170. }
  171. protected function doGetParent(array $context)
  172. {
  173. }
  174. protected function doDisplay(array $context, array $blocks = array())
  175. {
  176. }
  177. public function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false, $ignoreStrictCheck = false)
  178. {
  179. if ($this->useExtGetAttribute) {
  180. return twig_template_get_attributes($this, $object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
  181. } else {
  182. return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
  183. }
  184. }
  185. }
  186. class Twig_TemplateArrayAccessObject implements ArrayAccess
  187. {
  188. protected $protected = 'protected';
  189. public $attributes = array(
  190. 'defined' => 'defined',
  191. 'zero' => 0,
  192. 'null' => null,
  193. '1' => 1,
  194. );
  195. public function offsetExists($name)
  196. {
  197. return array_key_exists($name, $this->attributes);
  198. }
  199. public function offsetGet($name)
  200. {
  201. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
  202. }
  203. public function offsetSet($name, $value)
  204. {
  205. }
  206. public function offsetUnset($name)
  207. {
  208. }
  209. }
  210. class Twig_TemplateMagicPropertyObject
  211. {
  212. public $defined = 'defined';
  213. public $attributes = array(
  214. 'zero' => 0,
  215. 'null' => null,
  216. '1' => 1,
  217. );
  218. protected $protected = 'protected';
  219. public function __isset($name)
  220. {
  221. return array_key_exists($name, $this->attributes);
  222. }
  223. public function __get($name)
  224. {
  225. return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : null;
  226. }
  227. }
  228. class Twig_TemplatePropertyObject
  229. {
  230. public $defined = 'defined';
  231. public $zero = 0;
  232. public $null = null;
  233. protected $protected = 'protected';
  234. }
  235. class Twig_TemplatePropertyObjectAndIterator extends Twig_TemplatePropertyObject implements IteratorAggregate
  236. {
  237. public function getIterator()
  238. {
  239. return new ArrayIterator(array('foo', 'bar'));
  240. }
  241. }
  242. class Twig_TemplateMethodObject
  243. {
  244. public function getDefined()
  245. {
  246. return 'defined';
  247. }
  248. public function get1()
  249. {
  250. return 1;
  251. }
  252. public function getZero()
  253. {
  254. return 0;
  255. }
  256. public function getNull()
  257. {
  258. return null;
  259. }
  260. protected function getProtected()
  261. {
  262. return 'protected';
  263. }
  264. static public function getStatic()
  265. {
  266. return 'static';
  267. }
  268. }
  269. class Twig_TemplateMethodAndPropObject
  270. {
  271. private $a = 'a_prop';
  272. public function getA() {
  273. return 'a';
  274. }
  275. public $b = 'b_prop';
  276. public function getB() {
  277. return 'b';
  278. }
  279. private $c = 'c_prop';
  280. private function getC() {
  281. return 'c';
  282. }
  283. }
  284. class Twig_TemplateMagicMethodObject
  285. {
  286. public function __call($method, $arguments) {
  287. return '__call_'.$method;
  288. }
  289. }