/vendor/symfony/polyfill-util/TestListener.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 154 lines · 121 code · 22 blank · 11 comment · 15 complexity · 27860f3150c11989a3bf15a349399dbe MD5 · raw file

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