PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/kohana-twig/vendor/Twig-1.0/test/Twig/Tests/integrationTest.php

https://bitbucket.org/sapphiriq/assets-example
PHP | 178 lines | 129 code | 30 blank | 19 comment | 4 complexity | 41c30d4e9789d894d239903fd84457be MD5 | raw file
Possible License(s): BSD-3-Clause
  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('/--TEST--\s*(.*?)\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
  22. throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
  23. }
  24. $message = $match[1];
  25. $templates = array();
  26. preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $match[2], $matches, PREG_SET_ORDER);
  27. foreach ($matches as $match) {
  28. $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
  29. }
  30. $tests[] = array(str_replace($fixturesDir.'/', '', $file), $test, $message, $templates);
  31. }
  32. return $tests;
  33. }
  34. /**
  35. * @dataProvider getTests
  36. */
  37. public function testIntegration($file, $test, $message, $templates)
  38. {
  39. $loader = new Twig_Loader_Array($templates);
  40. $twig = new Twig_Environment($loader, array('cache' => false));
  41. $twig->addExtension(new Twig_Extension_Escaper());
  42. $twig->addExtension(new TestExtension());
  43. try {
  44. $template = $twig->loadTemplate('index.twig');
  45. } catch (Twig_Error_Syntax $e) {
  46. $e->setFilename($file);
  47. throw $e;
  48. } catch (Exception $e) {
  49. throw new Twig_Error($e->getMessage().' (in '.$file.')');
  50. }
  51. preg_match_all('/--DATA--(.*?)--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $matches, PREG_SET_ORDER);
  52. foreach ($matches as $match) {
  53. $output = trim($template->render(eval($match[1].';')), "\n ");
  54. $expected = trim($match[2], "\n ");
  55. if ($expected != $output) {
  56. echo 'Compiled template that failed:';
  57. foreach (array_keys($templates) as $name) {
  58. echo "Template: $name\n";
  59. $source = $loader->getSource($name);
  60. echo $twig->compile($twig->parse($twig->tokenize($source, $name)));
  61. }
  62. }
  63. $this->assertEquals($expected, $output, $message.' (in '.$file.')');
  64. }
  65. }
  66. }
  67. function test_foo($value = 'foo')
  68. {
  69. return $value;
  70. }
  71. class Foo
  72. {
  73. const BAR_NAME = 'bar';
  74. public function bar($param1 = null, $param2 = null)
  75. {
  76. return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
  77. }
  78. public function getFoo()
  79. {
  80. return 'foo';
  81. }
  82. public function getSelf()
  83. {
  84. return $this;
  85. }
  86. public function is()
  87. {
  88. return 'is';
  89. }
  90. public function in()
  91. {
  92. return 'in';
  93. }
  94. public function not()
  95. {
  96. return 'not';
  97. }
  98. public function strToLower($value)
  99. {
  100. return strtolower($value);
  101. }
  102. }
  103. class TestExtension extends Twig_Extension
  104. {
  105. public function getFilters()
  106. {
  107. return array(
  108. 'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))),
  109. 'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),
  110. 'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))),
  111. );
  112. }
  113. public function getFunctions()
  114. {
  115. return array(
  116. 'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))),
  117. 'unsafe_br' => new Twig_Function_Method($this, 'br'),
  118. );
  119. }
  120. /**
  121. * nl2br which also escapes, for testing escaper filters
  122. */
  123. public function escape_and_nl2br($env, $value, $sep = '<br />')
  124. {
  125. return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep);
  126. }
  127. /**
  128. * nl2br only, for testing filters with pre_escape
  129. */
  130. public function nl2br($value, $sep = '<br />')
  131. {
  132. // not secure if $value contains html tags (not only entities)
  133. // don't use
  134. return str_replace("\n", "$sep\n", $value);
  135. }
  136. public function escape_something($value)
  137. {
  138. return strtoupper($value);
  139. }
  140. public function br()
  141. {
  142. return '<br />';
  143. }
  144. public function getName()
  145. {
  146. return 'test';
  147. }
  148. }