ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_Test_IntegrationTestCase Class Reference

Integration test helper. More...

+ Inheritance diagram for Twig_Test_IntegrationTestCase:
+ Collaboration diagram for Twig_Test_IntegrationTestCase:

Public Member Functions

 testIntegration ($file, $message, $condition, $templates, $exception, $outputs)
 getTests More...
 
 testLegacyIntegration ($file, $message, $condition, $templates, $exception, $outputs)
 getLegacyTests legacy More...
 
 getTests ($name, $legacyTests=false)
 
 getLegacyTests ()
 

Protected Member Functions

 getFixturesDir ()
 
 getRuntimeLoaders ()
 
 getExtensions ()
 
 getTwigFilters ()
 
 getTwigFunctions ()
 
 getTwigTests ()
 
 doIntegrationTest ($file, $message, $condition, $templates, $exception, $outputs)
 

Static Protected Member Functions

static parseTemplates ($test)
 

Detailed Description

Integration test helper.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com
Karma Dordrak drak@.nosp@m.ziku.nosp@m.la.or.nosp@m.g

Definition at line 20 of file IntegrationTestCase.php.

Member Function Documentation

◆ doIntegrationTest()

Twig_Test_IntegrationTestCase::doIntegrationTest (   $file,
  $message,
  $condition,
  $templates,
  $exception,
  $outputs 
)
protected

Definition at line 132 of file IntegrationTestCase.php.

References $config, $i, $message, $name, Sabre\VObject\$output, $ret, $source, $template, $test, getExtensions(), getRuntimeLoaders(), getTwigFilters(), getTwigFunctions(), getTwigTests(), and GuzzleHttp\Psr7\hash().

Referenced by testIntegration(), and testLegacyIntegration().

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  }
$config
Definition: bootstrap.php:15
$template
Twig base exception.
Definition: Error.php:34
catch(Exception $e) $message
Adds a getSourceContext() method for loaders.
$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
$test
Definition: Utf8Test.php:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getExtensions()

Twig_Test_IntegrationTestCase::getExtensions ( )
protected
Returns
Twig_ExtensionInterface[]

Definition at line 38 of file IntegrationTestCase.php.

Referenced by doIntegrationTest().

39  {
40  return array();
41  }
+ Here is the caller graph for this function:

◆ getFixturesDir()

Twig_Test_IntegrationTestCase::getFixturesDir ( )
abstractprotected
Returns
string

Referenced by getTests().

+ Here is the caller graph for this function:

◆ getLegacyTests()

Twig_Test_IntegrationTestCase::getLegacyTests ( )

Definition at line 127 of file IntegrationTestCase.php.

References getTests().

128  {
129  return $this->getTests('testLegacyIntegration', true);
130  }
getTests($name, $legacyTests=false)
+ Here is the call graph for this function:

◆ getRuntimeLoaders()

Twig_Test_IntegrationTestCase::getRuntimeLoaders ( )
protected
Returns
Twig_RuntimeLoaderInterface[]

Definition at line 30 of file IntegrationTestCase.php.

Referenced by doIntegrationTest().

31  {
32  return array();
33  }
+ Here is the caller graph for this function:

◆ getTests()

Twig_Test_IntegrationTestCase::getTests (   $name,
  $legacyTests = false 
)

Definition at line 84 of file IntegrationTestCase.php.

References $message, $test, $tests, and getFixturesDir().

Referenced by getLegacyTests().

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  }
catch(Exception $e) $message
$tests
Definition: bench.php:104
$test
Definition: Utf8Test.php:84
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getTwigFilters()

Twig_Test_IntegrationTestCase::getTwigFilters ( )
protected
Returns
Twig_SimpleFilter[]

Definition at line 46 of file IntegrationTestCase.php.

Referenced by doIntegrationTest().

47  {
48  return array();
49  }
+ Here is the caller graph for this function:

◆ getTwigFunctions()

Twig_Test_IntegrationTestCase::getTwigFunctions ( )
protected
Returns
Twig_SimpleFunction[]

Definition at line 54 of file IntegrationTestCase.php.

Referenced by doIntegrationTest().

55  {
56  return array();
57  }
+ Here is the caller graph for this function:

◆ getTwigTests()

Twig_Test_IntegrationTestCase::getTwigTests ( )
protected
Returns
Twig_SimpleTest[]

Definition at line 62 of file IntegrationTestCase.php.

Referenced by doIntegrationTest().

63  {
64  return array();
65  }
+ Here is the caller graph for this function:

◆ parseTemplates()

static Twig_Test_IntegrationTestCase::parseTemplates (   $test)
staticprotected

Definition at line 237 of file IntegrationTestCase.php.

References $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  }
$test
Definition: Utf8Test.php:84

◆ testIntegration()

Twig_Test_IntegrationTestCase::testIntegration (   $file,
  $message,
  $condition,
  $templates,
  $exception,
  $outputs 
)

getTests

Definition at line 70 of file IntegrationTestCase.php.

References $message, and doIntegrationTest().

71  {
72  $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
73  }
catch(Exception $e) $message
doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
+ Here is the call graph for this function:

◆ testLegacyIntegration()

Twig_Test_IntegrationTestCase::testLegacyIntegration (   $file,
  $message,
  $condition,
  $templates,
  $exception,
  $outputs 
)

getLegacyTests legacy

Definition at line 79 of file IntegrationTestCase.php.

References $message, and doIntegrationTest().

80  {
81  $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs);
82  }
catch(Exception $e) $message
doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs)
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: