Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

IntegrationTestCase.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 8.83 KiB


001  <?php
002   
003  /*
004   * This file is part of Twig.
005   *
006   * (c) Fabien Potencier
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Twig\Test;
013   
014  use PHPUnit\Framework\TestCase;
015  use Twig\Environment;
016  use Twig\Error\Error;
017  use Twig\Extension\ExtensionInterface;
018  use Twig\Loader\ArrayLoader;
019  use Twig\RuntimeLoader\RuntimeLoaderInterface;
020  use Twig\TwigFilter;
021  use Twig\TwigFunction;
022  use Twig\TwigTest;
023   
024  /**
025   * Integration test helper.
026   *
027   * @author Fabien Potencier <fabien@symfony.com>
028   * @author Karma Dordrak <drak@zikula.org>
029   */
030  abstract class IntegrationTestCase extends TestCase
031  {
032      /**
033       * @return string
034       */
035      abstract protected function getFixturesDir();
036   
037      /**
038       * @return RuntimeLoaderInterface[]
039       */
040      protected function getRuntimeLoaders()
041      {
042          return [];
043      }
044   
045      /**
046       * @return ExtensionInterface[]
047       */
048      protected function getExtensions()
049      {
050          return [];
051      }
052   
053      /**
054       * @return TwigFilter[]
055       */
056      protected function getTwigFilters()
057      {
058          return [];
059      }
060   
061      /**
062       * @return TwigFunction[]
063       */
064      protected function getTwigFunctions()
065      {
066          return [];
067      }
068   
069      /**
070       * @return TwigTest[]
071       */
072      protected function getTwigTests()
073      {
074          return [];
075      }
076   
077      /**
078       * @dataProvider getTests
079       */
080      public function testIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
081      {
082          $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
083      }
084   
085      /**
086       * @dataProvider getLegacyTests
087       * @group legacy
088       */
089      public function testLegacyIntegration($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
090      {
091          $this->doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation);
092      }
093   
094      public function getTests($name, $legacyTests = false)
095      {
096          $fixturesDir = realpath($this->getFixturesDir());
097          $tests = [];
098   
099          foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($fixturesDir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
100              if (!preg_match('/\.test$/', $file)) {
101                  continue;
102              }
103   
104              if ($legacyTests xor false !== strpos($file->getRealpath(), '.legacy.test')) {
105                  continue;
106              }
107   
108              $test = file_get_contents($file->getRealpath());
109   
110              if (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)\s*(?:--DATA--\s*(.*))?\s*--EXCEPTION--\s*(.*)/sx', $test, $match)) {
111                  $message = $match[1];
112                  $condition = $match[2];
113                  $deprecation = $match[3];
114                  $templates = self::parseTemplates($match[4]);
115                  $exception = $match[6];
116                  $outputs = [[null, $match[5], null, '']];
117              } elseif (preg_match('/--TEST--\s*(.*?)\s*(?:--CONDITION--\s*(.*))?\s*(?:--DEPRECATION--\s*(.*?))?\s*((?:--TEMPLATE(?:\(.*?\))?--(?:.*?))+)--DATA--.*?--EXPECT--.*/s', $test, $match)) {
118                  $message = $match[1];
119                  $condition = $match[2];
120                  $deprecation = $match[3];
121                  $templates = self::parseTemplates($match[4]);
122                  $exception = false;
123                  preg_match_all('/--DATA--(.*?)(?:--CONFIG--(.*?))?--EXPECT--(.*?)(?=\-\-DATA\-\-|$)/s', $test, $outputs, \PREG_SET_ORDER);
124              } else {
125                  throw new \InvalidArgumentException(sprintf('Test "%s" is not valid.', str_replace($fixturesDir.'/', '', $file)));
126              }
127   
128              $tests[] = [str_replace($fixturesDir.'/', '', $file), $message, $condition, $templates, $exception, $outputs, $deprecation];
129          }
130   
131          if ($legacyTests && empty($tests)) {
132              // add a dummy test to avoid a PHPUnit message
133              return [['not', '-', '', [], '', []]];
134          }
135   
136          return $tests;
137      }
138   
139      public function getLegacyTests()
140      {
141          return $this->getTests('testLegacyIntegration', true);
142      }
143   
144      protected function doIntegrationTest($file, $message, $condition, $templates, $exception, $outputs, $deprecation = '')
145      {
146          if (!$outputs) {
147              $this->markTestSkipped('no tests to run');
148          }
149   
150          if ($condition) {
151              eval('$ret = '.$condition.';');
152              if (!$ret) {
153                  $this->markTestSkipped($condition);
154              }
155          }
156   
157          $loader = new ArrayLoader($templates);
158   
159          foreach ($outputs as $i => $match) {
160              $config = array_merge([
161                  'cache' => false,
162                  'strict_variables' => true,
163              ], $match[2] ? eval($match[2].';') : []);
164              $twig = new Environment($loader, $config);
165              $twig->addGlobal('global', 'global');
166              foreach ($this->getRuntimeLoaders() as $runtimeLoader) {
167                  $twig->addRuntimeLoader($runtimeLoader);
168              }
169   
170              foreach ($this->getExtensions() as $extension) {
171                  $twig->addExtension($extension);
172              }
173   
174              foreach ($this->getTwigFilters() as $filter) {
175                  $twig->addFilter($filter);
176              }
177   
178              foreach ($this->getTwigTests() as $test) {
179                  $twig->addTest($test);
180              }
181   
182              foreach ($this->getTwigFunctions() as $function) {
183                  $twig->addFunction($function);
184              }
185   
186              // avoid using the same PHP class name for different cases
187              $p = new \ReflectionProperty($twig, 'templateClassPrefix');
188              $p->setAccessible(true);
189              $p->setValue($twig, '__TwigTemplate_'.hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', uniqid(mt_rand(), true), false).'_');
190   
191              $deprecations = [];
192              try {
193                  $prevHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$deprecations, &$prevHandler) {
194                      if (\E_USER_DEPRECATED === $type) {
195                          $deprecations[] = $msg;
196   
197                          return true;
198                      }
199   
200                      return $prevHandler ? $prevHandler($type, $msg, $file, $line, $context) : false;
201                  });
202   
203                  $template = $twig->load('index.twig');
204              } catch (\Exception $e) {
205                  if (false !== $exception) {
206                      $message = $e->getMessage();
207                      $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $message)));
208                      $last = substr($message, \strlen($message) - 1);
209                      $this->assertTrue('.' === $last || '?' === $last, 'Exception message must end with a dot or a question mark.');
210   
211                      return;
212                  }
213   
214                  throw new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
215              } finally {
216                  restore_error_handler();
217              }
218   
219              $this->assertSame($deprecation, implode("\n", $deprecations));
220   
221              try {
222                  $output = trim($template->render(eval($match[1].';')), "\n ");
223              } catch (\Exception $e) {
224                  if (false !== $exception) {
225                      $this->assertSame(trim($exception), trim(sprintf('%s: %s', \get_class($e), $e->getMessage())));
226   
227                      return;
228                  }
229   
230                  $e = new Error(sprintf('%s: %s', \get_class($e), $e->getMessage()), -1, null, $e);
231   
232                  $output = trim(sprintf('%s: %s', \get_class($e), $e->getMessage()));
233              }
234   
235              if (false !== $exception) {
236                  list($class) = explode(':', $exception);
237                  $constraintClass = class_exists('PHPUnit\Framework\Constraint\Exception') ? 'PHPUnit\Framework\Constraint\Exception' : 'PHPUnit_Framework_Constraint_Exception';
238                  $this->assertThat(null, new $constraintClass($class));
239              }
240   
241              $expected = trim($match[3], "\n ");
242   
243              if ($expected !== $output) {
244                  printf("Compiled templates that failed on case %d:\n", $i + 1);
245   
246                  foreach (array_keys($templates) as $name) {
247                      echo "Template: $name\n";
248                      echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext($name))));
249                  }
250              }
251              $this->assertEquals($expected, $output, $message.' (in '.$file.')');
252          }
253      }
254   
255      protected static function parseTemplates($test)
256      {
257          $templates = [];
258          preg_match_all('/--TEMPLATE(?:\((.*?)\))?--(.*?)(?=\-\-TEMPLATE|$)/s', $test, $matches, \PREG_SET_ORDER);
259          foreach ($matches as $match) {
260              $templates[($match[1] ?: 'index.twig')] = $match[2];
261          }
262   
263          return $templates;
264      }
265  }
266   
267  class_alias('Twig\Test\IntegrationTestCase', 'Twig_Test_IntegrationTestCase');
268