PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/piwik/vendor/twig/twig/lib/Twig/Test/IntegrationTestCase.php

https://bitbucket.org/jteppa/access_test
PHP | 237 lines | 158 code | 40 blank | 39 comment | 17 complexity | f9e2a2b0c5b7d3bbb29d5cf57090dada MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, BSD-2-Clause, LGPL-2.1, GPL-3.0
  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. use PHPUnit\Framework\TestCase;
  11. /**
  12. * Integration test helper.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Karma Dordrak <drak@zikula.org>
  16. */
  17. abstract class Twig_Test_IntegrationTestCase extends TestCase
  18. {
  19. /**
  20. * @return string
  21. */
  22. abstract protected function getFixturesDir();
  23. /**
  24. * @return Twig_ExtensionInterface[]
  25. */
  26. protected function getExtensions()
  27. {
  28. return array();
  29. }
  30. /**
  31. * @return Twig_SimpleFilter[]
  32. */
  33. protected function getTwigFilters()
  34. {
  35. return array();
  36. }
  37. /**
  38. * @return Twig_SimpleFunction[]
  39. */
  40. protected function getTwigFunctions()
  41. {
  42. return array();
  43. }
  44. /**
  45. * @return Twig_SimpleTest[]
  46. */
  47. protected function getTwigTests()
  48. {
  49. return array();
  50. }
  51. /**
  52. * @dataProvider getTests
  53. */
  54. public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
  55. {
  56. $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
  57. }
  58. /**
  59. * @dataProvider getLegacyTests
  60. * @group legacy
  61. */
  62. public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
  63. {
  64. $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
  65. }
  66. public function getTests($name, $legacyTests = false)
  67. {
  68. $fixturesDir = realpath($this->getFixturesDir());
  69. $tests = array();
  70. foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  71. if (!preg_match('/\.test$/', $file)) {
  72. continue;
  73. }
  74. if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
  75. continue;
  76. }
  77. $test = file_get_contents($file->getRealpath());
  78. if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
  79. $message = $match[1];
  80. $condition = $match[2];
  81. $templates = self::parseTemplates($match[3]);
  82. $exception = $match[5];
  83. $outputs = array(array(null, $match[4], null, ''));
  84. } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
  85. $message = $match[1];
  86. $condition = $match[2];
  87. $templates = self::parseTemplates($match[3]);
  88. $exception = false;
  89. preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
  90. } else {
  91. throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
  92. }
  93. $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
  94. }
  95. if ($legacyTests && empty($tests)) {
  96. // add a dummy test to avoid a PHPUnit message
  97. return array(array('not', '-', '', array(), '', array()));
  98. }
  99. return $tests;
  100. }
  101. public function getLegacyTests()
  102. {
  103. return $this->getTests('testLegacyIntegration', true);
  104. }
  105. protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
  106. {
  107. if (!$outputs) {
  108. $this->markTestSkipped('no legacy tests to run');
  109. }
  110. if ($condition) {
  111. eval('$ret = '.$condition.';');
  112. if (!$ret) {
  113. $this->markTestSkipped($condition);
  114. }
  115. }
  116. $loader = new Twig_Loader_Array($templates);
  117. foreach ($outputs as $i => $match) {
  118. $config = array_merge(array(
  119. 'cache' => false,
  120. 'strict_variables' => true,
  121. ), $match[2] ? eval($match[2].';') : array());
  122. $twig = new Twig_Environment($loader, $config);
  123. $twig->addGlobal('global', 'global');
  124. foreach ($this->getExtensions() as $extension) {
  125. $twig->addExtension($extension);
  126. }
  127. foreach ($this->getTwigFilters() as $filter) {
  128. $twig->addFilter($filter);
  129. }
  130. foreach ($this->getTwigTests() as $test) {
  131. $twig->addTest($test);
  132. }
  133. foreach ($this->getTwigFunctions() as $function) {
  134. $twig->addFunction($function);
  135. }
  136. // avoid using the same PHP class name for different cases
  137. // only for PHP 5.2+
  138. if (PHP_VERSION_ID >= 50300) {
  139. $p = new ReflectionProperty($twig, 'templateClassPrefix');
  140. $p->setAccessible(true);
  141. $p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
  142. }
  143. try {
  144. $template = $twig->loadTemplate('index.twig');
  145. } catch (Exception $e) {
  146. if (false !== $exception) {
  147. $message = $e->getMessage();
  148. $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
  149. $last = substr($message, strlen($message) - 1);
  150. $this->assertTrue('.' === $last || '?' === $last, $message, 'Exception message must end with a dot or a question mark.');
  151. return;
  152. }
  153. throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
  154. }
  155. try {
  156. $output = trim($template->render(eval($match[1].';')), "\n ");
  157. } catch (Exception $e) {
  158. if (false !== $exception) {
  159. $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
  160. return;
  161. }
  162. $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
  163. $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
  164. }
  165. if (false !== $exception) {
  166. list($class) = explode(':', $exception);
  167. $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
  168. $this->assertThat(null, new $constraintClass($class));
  169. }
  170. $expected = trim($match[3], "\n ");
  171. if ($expected !== $output) {
  172. printf("Compiled templates that failed on case %d:\n", $i + 1);
  173. foreach (array_keys($templates) as $name) {
  174. echo "Template: $name\n";
  175. $loader = $twig->getLoader();
  176. if (!$loader instanceof Twig_SourceContextLoaderInterface) {
  177. $source = new Twig_Source($loader->getSource($name), $name);
  178. } else {
  179. $source = $loader->getSourceContext($name);
  180. }
  181. echo $twig->compile($twig->parse($twig->tokenize($source)));
  182. }
  183. }
  184. $this->assertEquals($expected, $output, $message.' (in '.$file.')');
  185. }
  186. }
  187. protected static function parseTemplates($test)
  188. {
  189. $templates = array();
  190. preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
  191. foreach ($matches as $match) {
  192. $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
  193. }
  194. return $templates;
  195. }
  196. }
  197. class_alias('Twig_Test_IntegrationTestCase', 'Twig\Test\IntegrationTestCase', false);