integrationTest.php 8.1KB

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