|
11 | 11 |
|
12 | 12 | namespace Symfony\Polyfill\Util;
|
13 | 13 |
|
14 |
| -/** |
15 |
| - * @author Nicolas Grekas <[email protected]> |
16 |
| - */ |
17 |
| -class TestListener extends \PHPUnit_Framework_TestSuite implements \PHPUnit_Framework_TestListener |
18 |
| -{ |
19 |
| - public static $enabledPolyfills; |
20 |
| - private $suite; |
21 |
| - |
22 |
| - public function __construct(\PHPUnit_Framework_TestSuite $suite = null) |
| 14 | +use PHPUnit\Framework\AssertionFailedError; |
| 15 | +use PHPUnit\Framework\TestListener as TestListenerInterface; |
| 16 | +use PHPUnit\Framework\Test; |
| 17 | +use PHPUnit\Framework\TestSuite; |
| 18 | +use PHPUnit\Framework\Warning; |
| 19 | + |
| 20 | +if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) { |
| 21 | + class_alias('Symfony\Polyfill\Util\LegacyTestListener', 'Symfony\Polyfill\Util\TestListener'); |
| 22 | +// Using an early return instead of a else does not work when using the PHPUnit phar due to some weird PHP behavior (the class |
| 23 | +// gets defined without executing the code before it and so the definition is not properly conditional) |
| 24 | +} else { |
| 25 | + /** |
| 26 | + * @author Nicolas Grekas <[email protected]> |
| 27 | + */ |
| 28 | + class TestListener extends TestSuite implements TestListenerInterface |
23 | 29 | {
|
24 |
| - if ($suite) { |
25 |
| - $this->suite = $suite; |
26 |
| - $this->setName($suite->getName().' with polyfills enabled'); |
27 |
| - $this->addTest($suite); |
| 30 | + private $suite; |
| 31 | + private $trait; |
| 32 | + |
| 33 | + public function __construct(TestSuite $suite = null) |
| 34 | + { |
| 35 | + if ($suite) { |
| 36 | + $this->suite = $suite; |
| 37 | + $this->setName($suite->getName().' with polyfills enabled'); |
| 38 | + $this->addTest($suite); |
| 39 | + } |
| 40 | + $this->trait = new TestListenerTrait(); |
28 | 41 | }
|
29 |
| - } |
30 | 42 |
|
31 |
| - public function startTestSuite(\PHPUnit_Framework_TestSuite $mainSuite) |
32 |
| - { |
33 |
| - if (null !== self::$enabledPolyfills) { |
34 |
| - return; |
| 43 | + public function startTestSuite(TestSuite $suite) |
| 44 | + { |
| 45 | + $this->trait->startTestSuite($suite); |
35 | 46 | }
|
36 |
| - self::$enabledPolyfills = false; |
37 | 47 |
|
38 |
| - foreach ($mainSuite->tests() as $suite) { |
39 |
| - $testClass = $suite->getName(); |
40 |
| - if (!$tests = $suite->tests()) { |
41 |
| - continue; |
42 |
| - } |
43 |
| - if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) { |
44 |
| - $mainSuite->addTest(self::warning('Unknown naming convention for '.$testClass)); |
45 |
| - continue; |
46 |
| - } |
47 |
| - if (!class_exists($m[1].$m[2])) { |
48 |
| - continue; |
49 |
| - } |
50 |
| - $testedClass = new \ReflectionClass($m[1].$m[2]); |
51 |
| - $bootstrap = new \SplFileObject(dirname($testedClass->getFileName()).'/bootstrap.php'); |
52 |
| - $warnings = array(); |
53 |
| - $defLine = null; |
54 |
| - |
55 |
| - foreach (new \RegexIterator($bootstrap, '/return p\\\\'.$testedClass->getShortName().'::/') as $defLine) { |
56 |
| - if (!preg_match('/^\s*function (?P<name>[^\(]++)(?P<signature>\([^\)]*+\)) \{ (?<return>return p\\\\'.$testedClass->getShortName().'::[^\(]++)(?P<args>\([^\)]*+\)); \}$/', $defLine, $f)) { |
57 |
| - $warnings[] = self::warning('Invalid line in bootstrap.php: '.trim($defLine)); |
58 |
| - continue; |
59 |
| - } |
60 |
| - $testNamespace = substr($testClass, 0, strrpos($testClass, '\\')); |
61 |
| - if (function_exists($testNamespace.'\\'.$f['name'])) { |
62 |
| - continue; |
63 |
| - } |
64 |
| - |
65 |
| - try { |
66 |
| - $r = new \ReflectionFunction($f['name']); |
67 |
| - if ($r->isUserDefined()) { |
68 |
| - throw new \ReflectionException(); |
69 |
| - } |
70 |
| - if (false !== strpos($f['signature'], '&')) { |
71 |
| - $defLine = sprintf('return \\%s%s', $f['name'], $f['args']); |
72 |
| - } else { |
73 |
| - $defLine = sprintf("return \\call_user_func_array('%s', func_get_args())", $f['name']); |
74 |
| - } |
75 |
| - } catch (\ReflectionException $e) { |
76 |
| - $defLine = sprintf("throw new \PHPUnit_Framework_SkippedTestError('Internal function not found: %s')", $f['name']); |
77 |
| - } |
78 |
| - |
79 |
| - eval(<<<EOPHP |
80 |
| -namespace {$testNamespace}; |
81 |
| -
|
82 |
| -use Symfony\Polyfill\Util\TestListener; |
83 |
| -use {$testedClass->getNamespaceName()} as p; |
84 |
| -
|
85 |
| -function {$f['name']}{$f['signature']} |
86 |
| -{ |
87 |
| - if ('{$testClass}' === TestListener::\$enabledPolyfills) { |
88 |
| - {$f['return']}{$f['args']}; |
89 |
| - } |
90 |
| -
|
91 |
| - {$defLine}; |
92 |
| -} |
93 |
| -EOPHP |
94 |
| - ); |
95 |
| - } |
96 |
| - if (!$warnings && null === $defLine) { |
97 |
| - $warnings[] = new \PHPUnit_Framework_SkippedTestError('No Polyfills found in bootstrap.php for '.$testClass); |
98 |
| - } else { |
99 |
| - $mainSuite->addTest(new static($suite)); |
100 |
| - } |
101 |
| - } |
102 |
| - foreach ($warnings as $w) { |
103 |
| - $mainSuite->addTest($w); |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + TestListenerTrait::$enabledPolyfills = $this->suite->getName(); |
104 | 51 | }
|
105 |
| - } |
106 | 52 |
|
107 |
| - protected function setUp() |
108 |
| - { |
109 |
| - self::$enabledPolyfills = $this->suite->getName(); |
110 |
| - } |
| 53 | + protected function tearDown() |
| 54 | + { |
| 55 | + TestListenerTrait::$enabledPolyfills = false; |
| 56 | + } |
111 | 57 |
|
112 |
| - protected function tearDown() |
113 |
| - { |
114 |
| - self::$enabledPolyfills = false; |
115 |
| - } |
| 58 | + public function addError(Test $test, \Exception $e, $time) |
| 59 | + { |
| 60 | + $this->trait->addError($test, $e, $time); |
| 61 | + } |
116 | 62 |
|
117 |
| - public function addError(\PHPUnit_Framework_Test $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)); |
| 63 | + public function addWarning(Test $test, Warning $e, $time) |
| 64 | + { |
123 | 65 | }
|
124 |
| - } |
125 | 66 |
|
126 |
| - public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
127 |
| - { |
128 |
| - $this->addError($test, $e, $time); |
129 |
| - } |
| 67 | + public function addFailure(Test $test, AssertionFailedError $e, $time) |
| 68 | + { |
| 69 | + $this->trait->addError($test, $e, $time); |
| 70 | + } |
130 | 71 |
|
131 |
| - public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
132 |
| - { |
133 |
| - } |
| 72 | + public function addIncompleteTest(Test $test, \Exception $e, $time) |
| 73 | + { |
| 74 | + } |
134 | 75 |
|
135 |
| - public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
136 |
| - { |
137 |
| - } |
| 76 | + public function addRiskyTest(Test $test, \Exception $e, $time) |
| 77 | + { |
| 78 | + } |
138 | 79 |
|
139 |
| - public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
140 |
| - { |
141 |
| - } |
| 80 | + public function addSkippedTest(Test $test, \Exception $e, $time) |
| 81 | + { |
| 82 | + } |
142 | 83 |
|
143 |
| - public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
144 |
| - { |
145 |
| - } |
| 84 | + public function endTestSuite(TestSuite $suite) |
| 85 | + { |
| 86 | + } |
146 | 87 |
|
147 |
| - public function startTest(\PHPUnit_Framework_Test $test) |
148 |
| - { |
149 |
| - } |
| 88 | + public function startTest(Test $test) |
| 89 | + { |
| 90 | + } |
150 | 91 |
|
151 |
| - public function endTest(\PHPUnit_Framework_Test $test, $time) |
152 |
| - { |
| 92 | + public function endTest(Test $test, $time) |
| 93 | + { |
| 94 | + } |
153 | 95 | }
|
154 | 96 | }
|
0 commit comments