[ Index ]

PHP Cross Reference of phpBB-3.3.0-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              if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) {
  35                  $mainSuite->addTest(TestListener::warning('Unknown naming convention for '.$testClass));
  36                  continue;
  37              }
  38              if (!class_exists($m[1].$m[2])) {
  39                  continue;
  40              }
  41              $testedClass = new \ReflectionClass($m[1].$m[2]);
  42              $bootstrap = new \SplFileObject(\dirname($testedClass->getFileName()).'/bootstrap.php');
  43              $warnings = array();
  44              $defLine = null;
  45  
  46              foreach (new \RegexIterator($bootstrap, '/define\(\'/') as $defLine) {
  47                  preg_match('/define\(\'(?P<name>.+)\'/', $defLine, $matches);
  48                  if (\defined($matches['name'])) {
  49                      continue;
  50                  }
  51  
  52                  try {
  53                      eval($defLine);
  54                  } catch (\PHPUnit_Framework_Exception $ex){
  55                      $warnings[] = TestListener::warning($ex->getMessage());
  56                  } catch (\PHPUnit\Framework\Exception $ex) {
  57                      $warnings[] = TestListener::warning($ex->getMessage());
  58                  }
  59              }
  60  
  61              $bootstrap->rewind();
  62  
  63              foreach (new \RegexIterator($bootstrap, '/return p\\\\'.$testedClass->getShortName().'::/') as $defLine) {
  64                  if (!preg_match('/^\s*function (?P<name>[^\(]++)(?P<signature>\(.*\)) \{ (?<return>return p\\\\'.$testedClass->getShortName().'::[^\(]++)(?P<args>\([^\)]*+\)); \}$/', $defLine, $f)) {
  65                      $warnings[] = TestListener::warning('Invalid line in bootstrap.php: '.trim($defLine));
  66                      continue;
  67                  }
  68                  $testNamespace = substr($testClass, 0, strrpos($testClass, '\\'));
  69                  if (\function_exists($testNamespace.'\\'.$f['name'])) {
  70                      continue;
  71                  }
  72  
  73                  try {
  74                      $r = new \ReflectionFunction($f['name']);
  75                      if ($r->isUserDefined()) {
  76                          throw new \ReflectionException();
  77                      }
  78                      if ('idn_to_ascii' === $f['name'] || 'idn_to_utf8' === $f['name']) {
  79                          $defLine = sprintf('return INTL_IDNA_VARIANT_2003 === $variant ? \\%s($domain, $options, $variant) : \\%1$s%s', $f['name'], $f['args']);
  80                      } elseif (false !== strpos($f['signature'], '&') && 'idn_to_ascii' !== $f['name'] && 'idn_to_utf8' !== $f['name']) {
  81                          $defLine = sprintf('return \\%s%s', $f['name'], $f['args']);
  82                      } else {
  83                          $defLine = sprintf("return \\call_user_func_array('%s', \\func_get_args())", $f['name']);
  84                      }
  85                  } catch (\ReflectionException $e) {
  86                      $defLine = sprintf("throw new \\{$SkippedTestError}('Internal function not found: %s')", $f['name']);
  87                  }
  88  
  89                  eval(<<<EOPHP
  90  namespace {$testNamespace};
  91  
  92  use Symfony\Polyfill\Util\TestListenerTrait;
  93  use {$testedClass->getNamespaceName()} as p;
  94  
  95  function {$f['name']}{$f['signature']}
  96  {
  97      if ('{$testClass}' === TestListenerTrait::\$enabledPolyfills) {
  98          {$f['return']}{$f['args']};
  99      }
 100  
 101      {$defLine};
 102  }
 103  EOPHP
 104                  );
 105              }
 106              if (!$warnings && null === $defLine) {
 107                  $warnings[] = new $SkippedTestError('No Polyfills found in bootstrap.php for '.$testClass);
 108              } else {
 109                  $mainSuite->addTest(new TestListener($suite));
 110              }
 111          }
 112          foreach ($warnings as $w) {
 113              $mainSuite->addTest($w);
 114          }
 115      }
 116  
 117      public function addError($test, \Exception $e, $time)
 118      {
 119          if (false !== self::$enabledPolyfills) {
 120              $r = new \ReflectionProperty('Exception', 'message');
 121              $r->setAccessible(true);
 122              $r->setValue($e, 'Polyfills enabled, '.$r->getValue($e));
 123          }
 124      }
 125  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1