-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTestListenerTrait.php
163 lines (142 loc) · 6.98 KB
/
TestListenerTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Polyfill\Util;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Util\Test;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\Stub;
/**
* @author Nicolas Grekas <[email protected]>
*/
class TestListenerTrait
{
public static $enabledPolyfills;
public function startTestSuite($mainSuite)
{
$warnings = [];
foreach ($mainSuite->tests() as $suite) {
$testClass = $suite->getName();
if (!$tests = $suite->tests()) {
continue;
}
if (\in_array('class-polyfill', Test::getGroups($testClass), true)) {
// TODO: check signatures for all polyfilled methods on PHP >= 8
continue;
}
$testedClass = new \ReflectionClass($testClass);
if (preg_match('{^ \* @requires PHP (.*)}mi', $testedClass->getDocComment(), $m) && version_compare($m[1], \PHP_VERSION, '>')) {
continue;
}
if (!preg_match('/^(.+)\\\\Tests(\\\\.*)Test$/', $testClass, $m)) {
$mainSuite->addTest(TestListener::warning('Unknown naming convention for '.$testClass));
continue;
}
if (!class_exists($m[1].$m[2])) {
continue;
}
$testedClass = new \ReflectionClass($m[1].$m[2]);
$bootstrap = \dirname($testedClass->getFileName()).'/bootstrap';
$bootstrap = new \SplFileObject($bootstrap.(\PHP_VERSION_ID >= 80000 && file_exists($bootstrap.'80.php') ? '80' : '').'.php');
$newWarnings = 0;
$defLine = null;
foreach (new \RegexIterator($bootstrap, '/define\(\'/') as $defLine) {
preg_match("/define\('(?P<name>[^']++)'/", $defLine, $matches);
if (\defined($matches['name'])) {
continue;
}
try {
eval($defLine);
} catch (\PHPUnit\Framework\Exception $ex) {
$warnings[] = TestListener::warning($ex->getMessage());
++$newWarnings;
}
}
$bootstrap->rewind();
foreach (new \RegexIterator($bootstrap, '/return p\\\\'.$testedClass->getShortName().'::/') as $defLine) {
if (!preg_match('/^\s*function (?P<name>[^\(]++)(?P<signature>\(.*\)(?: ?: [^ ]++)?) \{ (?<return>return p\\\\'.$testedClass->getShortName().'::[^\(]++)(?P<args>\([^\n]*?\)); \}$/', $defLine, $f)) {
$warnings[] = TestListener::warning('Invalid line in '.$bootstrap->getPathname().': '.trim($defLine));
++$newWarnings;
continue;
}
$testNamespace = substr($testClass, 0, strrpos($testClass, '\\'));
if (\function_exists($testNamespace.'\\'.$f['name'])) {
continue;
}
try {
$r = new \ReflectionFunction($f['name']);
if ($r->isUserDefined()) {
throw new \ReflectionException();
}
if ('idn_to_ascii' === $f['name'] || 'idn_to_utf8' === $f['name']) {
$defLine = sprintf('return PHP_VERSION_ID < 80000 && INTL_IDNA_VARIANT_2003 === $variant ? \\%s($domain, $options, $variant) : \\%1$s%s', $f['name'], $f['args']);
} elseif (false !== strpos($f['signature'], '&') && 'idn_to_ascii' !== $f['name'] && 'idn_to_utf8' !== $f['name']) {
$defLine = sprintf('return \\%s%s', $f['name'], $f['args']);
} else {
$defLine = sprintf("return \\call_user_func_array('%s', \\func_get_args())", $f['name']);
}
} catch (\ReflectionException $e) {
$r = null;
$defLine = sprintf("throw new \\%s('Internal function not found: %s')", SkippedTestError::class, $f['name']);
}
eval(<<<EOPHP
namespace {$testNamespace};
use Symfony\Polyfill\Util\TestListenerTrait;
use {$testedClass->getNamespaceName()} as p;
function {$f['name']}{$f['signature']}
{
if ('{$testClass} with polyfills enabled' === TestListenerTrait::\$enabledPolyfills) {
{$f['return']}{$f['args']};
}
{$defLine};
}
EOPHP
);
if (\PHP_VERSION_ID >= 80000 && $r && false === strpos($bootstrap->getPath(), 'Php7') && false === strpos($bootstrap->getPath(), 'Php80')) {
$originalSignature = ReflectionCaster::getSignature(ReflectionCaster::castFunctionAbstract($r, [], new Stub(), true));
$polyfillSignature = ReflectionCaster::castFunctionAbstract(new \ReflectionFunction($testNamespace.'\\'.$f['name']), [], new Stub(), true);
$polyfillSignature = ReflectionCaster::getSignature($polyfillSignature);
if ('mb_get_info' === $r->name && false === strpos($originalSignature, '|null') && false !== strpos($polyfillSignature, '|null')) {
// Added to PHP 8.2.14/8.3.1
$originalSignature .= '|null';
}
if (false === strpos($bootstrap->getPath(), '80.php')) {
// mixed return type cannot be used before PHP 8
$originalSignature = str_replace(': mixed', '', $originalSignature);
}
$map = [
'?' => '',
'array|string|null $string' => 'array|string $string',
'array|string|null $from_encoding = null' => 'array|string|null $from_encoding = null',
'array|string|null $from_encoding' => 'array|string $from_encoding',
];
if (strtr($polyfillSignature, $map) !== str_replace('?', '', $originalSignature)) {
$warnings[] = TestListener::warning("Incompatible signature for PHP >= 8:\n- {$f['name']}$originalSignature\n+ {$f['name']}$polyfillSignature");
}
}
}
if (!$newWarnings && null === $defLine) {
$warnings[] = TestListener::warning('No polyfills found in bootstrap.php for '.$testClass);
} else {
$mainSuite->addTest(new TestListener($suite));
}
}
foreach ($warnings as $w) {
$mainSuite->addTest($w);
}
}
public function addError($test, \Exception $e, $time)
{
if (false !== self::$enabledPolyfills) {
$r = new \ReflectionProperty('Exception', 'message');
$r->setAccessible(true);
$r->setValue($e, 'Polyfills enabled, '.$r->getValue($e));
}
}
}