integrationTest.php 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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_IntegrationTest extends PHPUnit_Framework_TestCase
  11. {
  12. public function getTests()
  13. {
  14. $fixturesDir = realpath(dirname(__FILE__).'/Fixtures/');
  15. $tests = array();
  16. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  17. if (!preg_match('/\.test$/', $file)) {
  18. continue;
  19. }
  20. $test = file_get_contents($file->getRealpath());
  21. if (preg_match('/
  22. --TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*))+)\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
  23. $message = $match[1];
  24. $condition = $match[2];
  25. $templates = $this->parseTemplates($match[3]);
  26. $exception = $match[4];
  27. $outputs = array(null, array(), null, '');
  28. } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
  29. $message = $match[1];
  30. $condition = $match[2];
  31. $templates = $this->parseTemplates($match[3]);
  32. $exception = false;
  33. preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
  34. } else {
  35. throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
  36. }
  37. $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
  38. }
  39. return $tests;
  40. }
  41. /**
  42. * @dataProvider getTests
  43. */
  44. public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
  45. {
  46. if ($condition) {
  47. eval('$ret = '.$condition.';');
  48. if (!$ret) {
  49. $this->markTestSkipped($condition);
  50. }
  51. }
  52. $loader = new Twig_Loader_Array($templates);
  53. foreach ($outputs as $match) {
  54. $config = array_merge(array(
  55. 'cache' => false,
  56. 'strict_variables' => true,
  57. ), $match[2] ? eval($match[2].';') : array());
  58. $twig = new Twig_Environment($loader, $config);
  59. $twig->addExtension(new TestExtension());
  60. $twig->addExtension(new Twig_Extension_Debug());
  61. $policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(), array(), array());
  62. $twig->addExtension(new Twig_Extension_Sandbox($policy, false));
  63. $twig->addGlobal('global', 'global');
  64. try {
  65. $template = $twig->loadTemplate('index.twig');
  66. } catch (Exception $e) {
  67. if (false !== $exception) {
  68. $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
  69. return;
  70. }
  71. if ($e instanceof Twig_Error_Syntax) {
  72. $e->setTemplateFile($file);
  73. throw $e;
  74. }
  75. throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
  76. }
  77. try {
  78. $output = trim($template->render(eval($match[1].';')), "\n ");
  79. } catch (Exception $e) {
  80. if (false !== $exception) {
  81. $this->assertEquals(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
  82. return;
  83. }
  84. if ($e instanceof Twig_Error_Syntax) {
  85. $e->setTemplateFile($file);
  86. } else {
  87. $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
  88. }
  89. $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
  90. }
  91. $expected = trim($match[3], "\n ");
  92. if ($expected != $output) {
  93. echo 'Compiled template that failed:';
  94. foreach (array_keys($templates) as $name) {
  95. echo "Template: $name\n";
  96. $source = $loader->getSource($name);
  97. echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
  98. }
  99. }
  100. $this->assertEquals($expected, $output, $message.' (in '.$file.')');
  101. }
  102. }
  103. protected function parseTemplates($test)
  104. {
  105. $templates = array();
  106. preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
  107. foreach ($matches as $match) {
  108. $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
  109. }
  110. return $templates;
  111. }
  112. }
  113. function test_foo($value = 'foo')
  114. {
  115. return $value;
  116. }
  117. class Foo implements Iterator
  118. {
  119. const BAR_NAME = 'bar';
  120. public $position = 0;
  121. public $array = array(1, 2);
  122. public function bar($param1 = null, $param2 = null)
  123. {
  124. return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
  125. }
  126. public function getFoo()
  127. {
  128. return 'foo';
  129. }
  130. public function getSelf()
  131. {
  132. return $this;
  133. }
  134. public function is()
  135. {
  136. return 'is';
  137. }
  138. public function in()
  139. {
  140. return 'in';
  141. }
  142. public function not()
  143. {
  144. return 'not';
  145. }
  146. public function strToLower($value)
  147. {
  148. return strtolower($value);
  149. }
  150. public function rewind()
  151. {
  152. $this->position = 0;
  153. }
  154. public function current()
  155. {
  156. return $this->array[$this->position];
  157. }
  158. public function key()
  159. {
  160. return 'a';
  161. }
  162. public function next()
  163. {
  164. ++$this->position;
  165. }
  166. public function valid()
  167. {
  168. return isset($this->array[$this->position]);
  169. }
  170. }
  171. class TestTokenParser_☃ extends Twig_TokenParser
  172. {
  173. public function parse(Twig_Token $token)
  174. {
  175. $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
  176. return new Twig_Node_Print(new Twig_Node_Expression_Constant('☃', -1), -1);
  177. }
  178. public function getTag()
  179. {
  180. return '☃';
  181. }
  182. }
  183. class TestExtension extends Twig_Extension
  184. {
  185. public function getTokenParsers()
  186. {
  187. return array(
  188. new TestTokenParser_☃(),
  189. );
  190. }
  191. public function getFilters()
  192. {
  193. return array(
  194. '☃' => new Twig_Filter_Method($this, '☃Filter'),
  195. 'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))),
  196. 'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),
  197. 'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))),
  198. 'preserves_safety' => new Twig_Filter_Method($this, 'preserves_safety', array('preserves_safety' => array('html'))),
  199. '*_path' => new Twig_Filter_Method($this, 'dynamic_path'),
  200. '*_foo_*_bar' => new Twig_Filter_Method($this, 'dynamic_foo'),
  201. );
  202. }
  203. public function getFunctions()
  204. {
  205. return array(
  206. '☃' => new Twig_Function_Method($this, '☃Function'),
  207. 'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))),
  208. 'unsafe_br' => new Twig_Function_Method($this, 'br'),
  209. '*_path' => new Twig_Function_Method($this, 'dynamic_path'),
  210. '*_foo_*_bar' => new Twig_Function_Method($this, 'dynamic_foo'),
  211. );
  212. }
  213. public function ☃Filter($value)
  214. {
  215. return "☃{$value}☃";
  216. }
  217. public function ☃Function($value)
  218. {
  219. return "☃{$value}☃";
  220. }
  221. /**
  222. * nl2br which also escapes, for testing escaper filters
  223. */
  224. public function escape_and_nl2br($env, $value, $sep = '<br />')
  225. {
  226. return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep);
  227. }
  228. /**
  229. * nl2br only, for testing filters with pre_escape
  230. */
  231. public function nl2br($value, $sep = '<br />')
  232. {
  233. // not secure if $value contains html tags (not only entities)
  234. // don't use
  235. return str_replace("\n", "$sep\n", $value);
  236. }
  237. public function dynamic_path($element, $item)
  238. {
  239. return $element.'/'.$item;
  240. }
  241. public function dynamic_foo($foo, $bar, $item)
  242. {
  243. return $foo.'/'.$bar.'/'.$item;
  244. }
  245. public function escape_something($value)
  246. {
  247. return strtoupper($value);
  248. }
  249. public function preserves_safety($value)
  250. {
  251. return strtoupper($value);
  252. }
  253. public function br()
  254. {
  255. return '<br />';
  256. }
  257. public function getName()
  258. {
  259. return 'test';
  260. }
  261. }