View source
<?php
namespace Drupal\Tests\Listeners;
use Drupal\Tests\Traits\ExpectDeprecationTrait;
use PHPUnit\Framework\TestCase;
use PHPUnit\Util\Test;
trait DeprecationListenerTrait {
use ExpectDeprecationTrait;
private $previousHandler;
protected function deprecationStartTest($test) {
if ($test instanceof TestCase) {
if ('disabled' !== getenv('SYMFONY_DEPRECATIONS_HELPER')) {
$this
->registerErrorHandler($test);
}
if ($this
->willBeIsolated($test)) {
putenv('DRUPAL_EXPECTED_DEPRECATIONS_SERIALIZE=' . tempnam(sys_get_temp_dir(), 'exdep'));
}
}
}
protected function deprecationEndTest($test, $time) {
if ($file = getenv('DRUPAL_EXPECTED_DEPRECATIONS_SERIALIZE')) {
putenv('DRUPAL_EXPECTED_DEPRECATIONS_SERIALIZE');
$expected_deprecations = file_get_contents($file);
if ($expected_deprecations) {
$test
->expectedDeprecations(unserialize($expected_deprecations));
}
}
if ($file = getenv('SYMFONY_DEPRECATIONS_SERIALIZE')) {
$method = $test
->getName(FALSE);
if (strpos($method, 'testLegacy') === 0 || strpos($method, 'provideLegacy') === 0 || strpos($method, 'getLegacy') === 0 || strpos(get_class($test), '\\Legacy') || in_array('legacy', Test::getGroups(get_class($test), $method), TRUE)) {
return;
}
$deprecations = file_get_contents($file);
$deprecations = $deprecations ? unserialize($deprecations) : [];
$resave = FALSE;
foreach ($deprecations as $key => $deprecation) {
if (in_array($deprecation[1], static::getSkippedDeprecations())) {
unset($deprecations[$key]);
$resave = TRUE;
}
}
if ($resave) {
file_put_contents($file, serialize($deprecations));
}
}
}
private function willBeIsolated($test) {
if ($test
->isInIsolation()) {
return FALSE;
}
$r = new \ReflectionProperty($test, 'runTestInSeparateProcess');
$r
->setAccessible(TRUE);
return $r
->getValue($test);
}
public static function getSkippedDeprecations() {
return [
'MigrateCckField is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \\Drupal\\migrate_drupal\\Annotation\\MigrateField instead.',
'MigrateCckFieldPluginManager is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \\Drupal\\migrate_drupal\\Annotation\\MigrateFieldPluginManager instead.',
'MigrateCckFieldPluginManagerInterface is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.x. Use \\Drupal\\migrate_drupal\\Annotation\\MigrateFieldPluginManagerInterface instead.',
'The "plugin.manager.migrate.cckfield" service is deprecated. You should use the \'plugin.manager.migrate.field\' service instead. See https://www.drupal.org/node/2751897',
'The Symfony\\Component\\ClassLoader\\ApcClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.',
'The Symfony\\Component\\ClassLoader\\WinCacheClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use `composer install --apcu-autoloader` instead.',
'\\Drupal\\Tests\\SkippedDeprecationTest deprecation',
'The "Symfony\\Bridge\\PsrHttpMessage\\Factory\\DiactorosFactory" class is deprecated since symfony/psr-http-message-bridge 1.2, use PsrHttpFactory instead.',
'The "psr7.http_message_factory" service relies on the deprecated "Symfony\\Bridge\\PsrHttpMessage\\Factory\\DiactorosFactory" class. It should either be deprecated or its implementation upgraded.',
'The "Symfony\\Component\\BrowserKit\\Response::getStatus()" method is deprecated since Symfony 4.3, use getStatusCode() instead.',
'The "core/jquery.ui.checkboxradio" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3067969',
'The "core/jquery.ui.controlgroup" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3067969',
'The "core/html5shiv" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3086383',
'The "core/matchmedia" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3086653',
'The "core/matchmedia.addListener" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3086653',
'The "core/classList" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use the native browser implementation instead. See https://www.drupal.org/node/3089511',
'The "core/jquery.ui.datepicker" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3081864',
'The "locale/drupal.locale.datepicker" asset library is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. See https://www.drupal.org/node/3081864',
'The "https://www.drupal.org/link-relations/create" string as a RestResource plugin annotation URI path key is deprecated in Drupal 8.4.0, now a valid link relation type name must be specified, so "create" must be specified instead before Drupal 9.0.0. See https://www.drupal.org/node/2737401.',
];
}
protected function registerErrorHandler($test) {
$deprecation_handler = function ($type, $msg, $file, $line, $context = []) {
if ($type === E_USER_DEPRECATED && in_array($msg, self::getSkippedDeprecations(), TRUE)) {
return;
}
return call_user_func($this->previousHandler, $type, $msg, $file, $line, $context);
};
if ($this->previousHandler) {
set_error_handler($deprecation_handler);
return;
}
$this->previousHandler = set_error_handler($deprecation_handler);
$test_result_object = $test
->getTestResultObject();
if ($test_result_object) {
$reflection_class = new \ReflectionClass($test_result_object);
$reflection_property = $reflection_class
->getProperty('listeners');
$reflection_property
->setAccessible(TRUE);
$listeners = $reflection_property
->getValue($test_result_object);
$listeners[] = new AfterSymfonyListener();
$reflection_property
->setValue($test_result_object, $listeners);
}
}
}