integrationTest.php 8.0KB

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