/usr/share/php/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php is in php-symfony-phpunit-bridge 3.4.6+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\Legacy;
use Doctrine\Common\Annotations\AnnotationRegistry;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Util\Blacklist;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\DnsMock;
/**
* PHP 5.3 compatible trait-like shared implementation.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class SymfonyTestsListenerTrait
{
private static $globallyEnabled = false;
private $state = -1;
private $skippedFile = false;
private $wasSkipped = array();
private $isSkipped = array();
private $expectedDeprecations = array();
private $gatheredDeprecations = array();
private $previousErrorHandler;
private $testsWithWarnings;
private $reportUselessTests;
private $error;
private $runsInSeparateProcess = false;
/**
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
*/
public function __construct(array $mockedNamespaces = array())
{
if (class_exists('PHPUnit_Util_Blacklist')) {
\PHPUnit_Util_Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
} else {
Blacklist::$blacklistedClassNames['\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait'] = 2;
}
$warn = false;
foreach ($mockedNamespaces as $type => $namespaces) {
if (!is_array($namespaces)) {
$namespaces = array($namespaces);
}
if (is_int($type)) {
// @deprecated BC with v2.8 to v3.0
$type = 'time-sensitive';
$warn = true;
}
if ('time-sensitive' === $type) {
foreach ($namespaces as $ns) {
ClockMock::register($ns.'\DummyClass');
}
}
if ('dns-sensitive' === $type) {
foreach ($namespaces as $ns) {
DnsMock::register($ns.'\DummyClass');
}
}
}
if (self::$globallyEnabled) {
$this->state = -2;
} else {
self::$globallyEnabled = true;
if ($warn) {
echo "Clock-mocked namespaces for SymfonyTestsListener need to be nested in a \"time-sensitive\" key. This will be enforced in Symfony 4.0.\n";
}
}
}
public function __destruct()
{
if (0 < $this->state) {
file_put_contents($this->skippedFile, '<?php return '.var_export($this->isSkipped, true).';');
}
}
public function globalListenerDisabled()
{
self::$globallyEnabled = false;
$this->state = -1;
}
public function startTestSuite($suite)
{
if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
} else {
$Test = 'PHPUnit\Util\Test';
}
$suiteName = $suite->getName();
$this->testsWithWarnings = array();
foreach ($suite->tests() as $test) {
if (!($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)) {
continue;
}
if (null === $Test::getPreserveGlobalStateSettings(get_class($test), $test->getName(false))) {
$test->setPreserveGlobalState(false);
}
}
if (-1 === $this->state) {
echo "Testing $suiteName\n";
$this->state = 0;
if (!class_exists('Doctrine\Common\Annotations\AnnotationRegistry', false) && class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
if (method_exists('Doctrine\Common\Annotations\AnnotationRegistry', 'registerUniqueLoader')) {
AnnotationRegistry::registerUniqueLoader('class_exists');
} else {
AnnotationRegistry::registerLoader('class_exists');
}
}
if ($this->skippedFile = getenv('SYMFONY_PHPUNIT_SKIPPED_TESTS')) {
$this->state = 1;
if (file_exists($this->skippedFile)) {
$this->state = 2;
if (!$this->wasSkipped = require $this->skippedFile) {
echo "All tests already ran successfully.\n";
$suite->setTests(array());
}
}
}
$testSuites = array($suite);
for ($i = 0; isset($testSuites[$i]); ++$i) {
foreach ($testSuites[$i]->tests() as $test) {
if ($test instanceof \PHPUnit_Framework_TestSuite || $test instanceof TestSuite) {
if (!class_exists($test->getName(), false)) {
$testSuites[] = $test;
continue;
}
$groups = $Test::getGroups($test->getName());
if (in_array('time-sensitive', $groups, true)) {
ClockMock::register($test->getName());
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::register($test->getName());
}
}
}
}
} elseif (2 === $this->state) {
$skipped = array();
foreach ($suite->tests() as $test) {
if (!($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)
|| isset($this->wasSkipped[$suiteName]['*'])
|| isset($this->wasSkipped[$suiteName][$test->getName()])) {
$skipped[] = $test;
}
}
$suite->setTests($skipped);
}
}
public function addSkippedTest($test, \Exception $e, $time)
{
if (0 < $this->state) {
if ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase) {
$class = get_class($test);
$method = $test->getName();
} else {
$class = $test->getName();
$method = '*';
}
$this->isSkipped[$class][$method] = 1;
}
}
public function startTest($test)
{
if (-2 < $this->state && ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)) {
if (null !== $test->getTestResultObject()) {
$this->reportUselessTests = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything();
}
// This event is triggered before the test is re-run in isolation
if ($this->willBeIsolated($test)) {
$this->runsInSeparateProcess = tempnam(sys_get_temp_dir(), 'deprec');
putenv('SYMFONY_DEPRECATIONS_SERIALIZE='.$this->runsInSeparateProcess);
}
if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
$AssertionFailedError = 'PHPUnit_Framework_AssertionFailedError';
} else {
$Test = 'PHPUnit\Util\Test';
$AssertionFailedError = 'PHPUnit\Framework\AssertionFailedError';
}
$groups = $Test::getGroups(get_class($test), $test->getName(false));
if (!$this->runsInSeparateProcess) {
if (in_array('time-sensitive', $groups, true)) {
ClockMock::register(get_class($test));
ClockMock::withClockMock(true);
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::register(get_class($test));
}
}
$annotations = $Test::parseTestMethodAnnotations(get_class($test), $test->getName(false));
if (isset($annotations['class']['expectedDeprecation'])) {
$test->getTestResultObject()->addError($test, new $AssertionFailedError('`@expectedDeprecation` annotations are not allowed at the class level.'), 0);
}
if (isset($annotations['method']['expectedDeprecation'])) {
if (!in_array('legacy', $groups, true)) {
$this->error = new $AssertionFailedError('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`.');
}
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
$this->expectedDeprecations = $annotations['method']['expectedDeprecation'];
$this->previousErrorHandler = set_error_handler(array($this, 'handleError'));
}
}
}
public function addWarning($test, $e, $time)
{
if ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase) {
$this->testsWithWarnings[$test->getName()] = true;
}
}
public function endTest($test, $time)
{
if (class_exists('PHPUnit_Util_Blacklist', false)) {
$Test = 'PHPUnit_Util_Test';
$BaseTestRunner = 'PHPUnit_Runner_BaseTestRunner';
$Warning = 'PHPUnit_Framework_Warning';
} else {
$Test = 'PHPUnit\Util\Test';
$BaseTestRunner = 'PHPUnit\Runner\BaseTestRunner';
$Warning = 'PHPUnit\Framework\Warning';
}
$className = get_class($test);
$classGroups = $Test::getGroups($className);
$groups = $Test::getGroups($className, $test->getName(false));
if (null !== $this->reportUselessTests) {
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything($this->reportUselessTests);
$this->reportUselessTests = null;
}
if ($errored = null !== $this->error) {
$test->getTestResultObject()->addError($test, $this->error, 0);
$this->error = null;
}
if ($this->runsInSeparateProcess) {
$deprecations = file_get_contents($this->runsInSeparateProcess);
unlink($this->runsInSeparateProcess);
putenv('SYMFONY_DEPRECATIONS_SERIALIZE');
foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) {
$error = serialize(array('deprecation' => $deprecation[1], 'class' => $className, 'method' => $test->getName(false), 'triggering_file' => isset($deprecation[2]) ? $deprecation[2] : null));
if ($deprecation[0]) {
trigger_error($error, E_USER_DEPRECATED);
} else {
@trigger_error($error, E_USER_DEPRECATED);
}
}
$this->runsInSeparateProcess = false;
}
if ($this->expectedDeprecations) {
if (!in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE), true)) {
$test->addToAssertionCount(count($this->expectedDeprecations));
}
restore_error_handler();
if (!$errored && !in_array($test->getStatus(), array($BaseTestRunner::STATUS_SKIPPED, $BaseTestRunner::STATUS_INCOMPLETE, $BaseTestRunner::STATUS_FAILURE, $BaseTestRunner::STATUS_ERROR), true)) {
try {
$prefix = "@expectedDeprecation:\n";
$test->assertStringMatchesFormat($prefix.'%A '.implode("\n%A ", $this->expectedDeprecations)."\n%A", $prefix.' '.implode("\n ", $this->gatheredDeprecations)."\n");
} catch (AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
$test->getTestResultObject()->addFailure($test, $e, $time);
}
}
$this->expectedDeprecations = $this->gatheredDeprecations = array();
$this->previousErrorHandler = null;
}
if (!$this->runsInSeparateProcess && -2 < $this->state && ($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase)) {
if (in_array('time-sensitive', $groups, true)) {
ClockMock::withClockMock(false);
}
if (in_array('dns-sensitive', $groups, true)) {
DnsMock::withMockedHosts(array());
}
}
if (($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase) && 0 === strpos($test->getName(), 'testLegacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $groups, true)) {
$result = $test->getTestResultObject();
if (method_exists($result, 'addWarning')) {
$result->addWarning($test, new $Warning('Using the "testLegacy" prefix to mark tests as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time);
}
}
if (($test instanceof \PHPUnit_Framework_TestCase || $test instanceof TestCase) && strpos($className, '\Legacy') && !isset($this->testsWithWarnings[$test->getName()]) && !in_array('legacy', $classGroups, true)) {
$result = $test->getTestResultObject();
if (method_exists($result, 'addWarning')) {
$result->addWarning($test, new $Warning('Using the "Legacy" prefix to mark all tests of a class as legacy is deprecated since version 3.3 and will be removed in 4.0. Use the "@group legacy" notation instead to add the test to the legacy group.'), $time);
}
}
}
public function handleError($type, $msg, $file, $line, $context = array())
{
if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
$h = $this->previousErrorHandler;
return $h ? $h($type, $msg, $file, $line, $context) : false;
}
// If the message is serialized we need to extract the message. This occurs when the error is triggered by
// by the isolated test path in \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest().
$parsedMsg = @unserialize($msg);
if (is_array($parsedMsg)) {
$msg = $parsedMsg['deprecation'];
}
if (error_reporting()) {
$msg = 'Unsilenced deprecation: '.$msg;
}
$this->gatheredDeprecations[] = $msg;
}
/**
* @param TestCase $test
*
* @return bool
*/
private function willBeIsolated($test)
{
if ($test->isInIsolation()) {
return false;
}
$r = new \ReflectionProperty($test, 'runTestInSeparateProcess');
$r->setAccessible(true);
return $r->getValue($test);
}
}
|