[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/polyfill-util/ -> TestListenerTrait.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Polyfill\Util;
  13  
  14  /**
  15   * @author Nicolas Grekas <p@tchwork.com>
  16   */
  17  class TestListenerTrait
  18  {
  19      public static $enabledPolyfills;
  20  
  21      public function startTestSuite($mainSuite)
  22      {
  23          if (null !== self::$enabledPolyfills) {
  24              return;
  25          }
  26          self::$enabledPolyfills = false;
  27          $SkippedTestError = class_exists('PHPUnit\Framework\SkippedTestError') ? 'PHPUnit\Framework\SkippedTestError' : 'PHPUnit_Framework_SkippedTestError';
  28  
  29          foreach ($mainSuite->tests() as $suite) {
  30              $testClass = $suite->getName();
  31              if (!$tests = $suite->tests()) {
  32                  continue;
  33              }
  34              $testedClass = new \ReflectionClass($testClass);
  35              if (preg_match('{^ \* @requires PHP (.*)}mi', $testedClass->getDocComment(), $m) && version_compare($m[1], \PHP_VERSION, '>')) {
  36                  continue;
  37              }
  38              if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) {
  39                  $mainSuite->addTest(TestListener::warning('Unknown naming convention for '.$testClass));
  40                  continue;
  41              }
  42              if (!class_exists($m[1].$m[2])) {
  43                  continue;
  44              }
  45              $testedClass = new \ReflectionClass($m[1].$m[2]);
  46              $bootstrap = new \SplFileObject(\dirname($testedClass->getFileName()).'/bootstrap.php');
  47              $warnings = array();
  48              $defLine = null;
  49  
  50              foreach (new \RegexIterator($bootstrap, '/define\(\'/') as $defLine) {
  51                  preg_match('/define\(\'(?P<name>.+)\'/', $defLine, $matches);
  52                  if (\defined($matches['name'])) {
  53                      continue;
  54                  }
  55  
  56                  try {
  57                      eval($defLine);
  58                  } catch (\PHPUnit_Framework_Exception $ex){
  59                      $warnings[] = TestListener::warning($ex->getMessage());
  60                  } catch (\PHPUnit\Framework\Exception $ex) {
  61                      $warnings[] = TestListener::warning($ex->getMessage());
  62                  }
  63              }
  64  
  65              $bootstrap->rewind();
  66  
  67              foreach (new \RegexIterator($bootstrap, '/return p\\\\'.$testedClass->getShortName().'::/') as $defLine) {
  68                  if (!preg_match('/^\s*function (?P<name>[^\(]++)(?P<signature>\(.*\)(?: ?: [^ ]++)?) \{ (?<return>return p\\\\'.$testedClass->getShortName().'::[^\(]++)(?P<args>\([^\)]*+\)); \}$/', $defLine, $f)) {
  69                      $warnings[] = TestListener::warning('Invalid line in bootstrap.php: '.trim($defLine));
  70                      continue;
  71                  }
  72                  $testNamespace = substr($testClass, 0, strrpos($testClass, '\\'));
  73                  if (\function_exists($testNamespace.'\\'.$f['name'])) {
  74                      continue;
  75                  }
  76  
  77                  try {
  78                      $r = new \ReflectionFunction($f['name']);
  79                      if ($r->isUserDefined()) {
  80                          throw new \ReflectionException();
  81                      }
  82                      if ('idn_to_ascii' === $f['name'] || 'idn_to_utf8' === $f['name']) {
  83                          $defLine = sprintf('return INTL_IDNA_VARIANT_2003 === $variant ? \\%s($domain, $options, $variant) : \\%1$s%s', $f['name'], $f['args']);
  84                      } elseif (false !== strpos($f['signature'], '&') && 'idn_to_ascii' !== $f['name'] && 'idn_to_utf8' !== $f['name']) {
  85                          $defLine = sprintf('return \\%s%s', $f['name'], $f['args']);
  86                      } else {
  87                          $defLine = sprintf("return \\call_user_func_array('%s', \\func_get_args())", $f['name']);
  88                      }
  89                  } catch (\ReflectionException $e) {
  90                      $defLine = sprintf("throw new \\{$SkippedTestError}('Internal function not found: %s')", $f['name']);
  91                  }
  92  
  93                  eval(<<<EOPHP
  94  namespace {$testNamespace};
  95  
  96  use Symfony\Polyfill\Util\TestListenerTrait;
  97  use {$testedClass->getNamespaceName()} as p;
  98  
  99  function {$f['name']}{$f['signature']}
 100  {
 101      if ('{$testClass}' === TestListenerTrait::\$enabledPolyfills) {
 102          {$f['return']}{$f['args']};
 103      }
 104  
 105      {$defLine};
 106  }
 107  EOPHP
 108                  );
 109              }
 110              if (!$warnings && null === $defLine) {
 111                  $warnings[] = new $SkippedTestError('No Polyfills found in bootstrap.php for '.$testClass);
 112              } else {
 113                  $mainSuite->addTest(new TestListener($suite));
 114              }
 115          }
 116          foreach ($warnings as $w) {
 117              $mainSuite->addTest($w);
 118          }
 119      }
 120  
 121      public function addError($test, \Exception $e, $time)
 122      {
 123          if (false !== self::$enabledPolyfills) {
 124              $r = new \ReflectionProperty('Exception', 'message');
 125              $r->setAccessible(true);
 126              $r->setValue($e, 'Polyfills enabled, '.$r->getValue($e));
 127          }
 128      }
 129  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1