ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IntegrationTestCase.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
20 abstract class Twig_Test_IntegrationTestCase extends TestCase
21 {
25  abstract protected function getFixturesDir();
26 
30  protected function getRuntimeLoaders()
31  {
32  return array();
33  }
34 
38  protected function getExtensions()
39  {
40  return array();
41  }
42 
46  protected function getTwigFilters()
47  {
48  return array();
49  }
50 
54  protected function getTwigFunctions()
55  {
56  return array();
57  }
58 
62  protected function getTwigTests()
63  {
64  return array();
65  }
66 
70  public function testIntegration($file, $message, $condition, $templates, $exception, $outputs)
71  {
72  $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
73  }
74 
79  public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
80  {
81  $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
82  }
83 
84  public function getTests($name, $legacyTests = false)
85  {
86  $fixturesDir = realpath($this->getFixturesDir());
87  $tests = array();
88 
89  foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
90  if (!preg_match('/\.test$/', $file)) {
91  continue;
92  }
93 
94  if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
95  continue;
96  }
97 
98  $test = file_get_contents($file->getRealpath());
99 
100  if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
101  $message = $match[1];
102  $condition = $match[2];
103  $templates = self::parseTemplates($match[3]);
104  $exception = $match[5];
105  $outputs = array(array(null, $match[4], null, ''));
106  } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
107  $message = $match[1];
108  $condition = $match[2];
109  $templates = self::parseTemplates($match[3]);
110  $exception = false;
111  preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, PREG_SET_ORDER);
112  } else {
113  throw new InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
114  }
115 
116  $tests[] = array(str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs);
117  }
118 
119  if ($legacyTests && empty($tests)) {
120  // add a dummy test to avoid a PHPUnit message
121  return array(array('not', '-', '', array(), '', array()));
122  }
123 
124  return $tests;
125  }
126 
127  public function getLegacyTests()
128  {
129  return $this->getTests('testLegacyIntegration', true);
130  }
131 
132  protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
133  {
134  if (!$outputs) {
135  $this->markTestSkipped('no legacy tests to run');
136  }
137 
138  if ($condition) {
139  eval('$ret = '.$condition.';');
140  if (!$ret) {
141  $this->markTestSkipped($condition);
142  }
143  }
144 
145  $loader = new Twig_Loader_Array($templates);
146 
147  foreach ($outputs as $i => $match) {
148  $config = array_merge(array(
149  'cache' => false,
150  'strict_variables' => true,
151  ), $match[2] ? eval($match[2].';') : array());
152  $twig = new Twig_Environment($loader, $config);
153  $twig->addGlobal('global', 'global');
154  foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
155  $twig->addRuntimeLoader($runtimeLoader);
156  }
157 
158  foreach ($this->getExtensions() as $extension) {
159  $twig->addExtension($extension);
160  }
161 
162  foreach ($this->getTwigFilters() as $filter) {
163  $twig->addFilter($filter);
164  }
165 
166  foreach ($this->getTwigTests() as $test) {
167  $twig->addTest($test);
168  }
169 
170  foreach ($this->getTwigFunctions() as $function) {
171  $twig->addFunction($function);
172  }
173 
174  // avoid using the same PHP class name for different cases
175  // only for PHP 5.2+
176  if (PHP_VERSION_ID >= 50300) {
177  $p = new ReflectionProperty($twig, 'templateClassPrefix');
178  $p->setAccessible(true);
179  $p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_');
180  }
181 
182  try {
183  $template = $twig->loadTemplate('index.twig');
184  } catch (Exception $e) {
185  if (false !== $exception) {
186  $message = $e->getMessage();
187  $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $message)));
188  $last = substr($message, strlen($message) - 1);
189  $this->assertTrue('.' === $last || '?' === $last, $message, 'Exception message must end with a dot or a question mark.');
190 
191  return;
192  }
193 
194  throw new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
195  }
196 
197  try {
198  $output = trim($template->render(eval($match[1].';')), "\n ");
199  } catch (Exception $e) {
200  if (false !== $exception) {
201  $this->assertSame(trim($exception), trim(sprintf('%s: %s', get_class($e), $e->getMessage())));
202 
203  return;
204  }
205 
206  $e = new Twig_Error(sprintf('%s: %s', get_class($e), $e->getMessage()), -1, $file, $e);
207 
208  $output = trim(sprintf('%s: %s', get_class($e), $e->getMessage()));
209  }
210 
211  if (false !== $exception) {
212  list($class) = explode(':', $exception);
213  $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
214  $this->assertThat(null, new $constraintClass($class));
215  }
216 
217  $expected = trim($match[3], "\n ");
218 
219  if ($expected !== $output) {
220  printf("Compiled templates that failed on case %d:\n", $i + 1);
221 
222  foreach (array_keys($templates) as $name) {
223  echo "Template: $name\n";
224  $loader = $twig->getLoader();
225  if (!$loader instanceof Twig_SourceContextLoaderInterface) {
226  $source = new Twig_Source($loader->getSource($name), $name);
227  } else {
228  $source = $loader->getSourceContext($name);
229  }
230  echo $twig->compile($twig->parse($twig->tokenize($source)));
231  }
232  }
233  $this->assertEquals($expected, $output, $message.' (in '.$file.')');
234  }
235  }
236 
237  protected static function parseTemplates($test)
238  {
239  $templates = array();
240  preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, PREG_SET_ORDER);
241  foreach ($matches as $match) {
242  $templates[($match[1] ? $match[1] : 'index.twig')] = $match[2];
243  }
244 
245  return $templates;
246  }
247 }
248 
249 class_alias('Twig_Test_IntegrationTestCase', 'Twig\Test\IntegrationTestCase', false);
$config
Definition: bootstrap.php:15
$template
testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs)
getLegacyTests legacy
Twig base exception.
Definition: Error.php:34
Integration test helper.
testIntegration($file, $message, $condition, $templates, $exception, $outputs)
getTests
catch(Exception $e) $message
$tests
Definition: bench.php:104
Adds a getSourceContext() method for loaders.
doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
$ret
Definition: parser.php:6
$i
Definition: disco.tpl.php:19
Holds information about a non-compiled Twig template.
Definition: Source.php:19
$source
Definition: linkback.php:22
Stores the Twig configuration.
Definition: Environment.php:17
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
Loads a template from an array.
Definition: Array.php:26
getTests($name, $legacyTests=false)
$test
Definition: Utf8Test.php:84