DrupalListener.php in Drupal 9
File
core/tests/Drupal/Tests/Listeners/DrupalListener.php
View source
<?php
namespace Drupal\Tests\Listeners;
use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestSuite;
use PHPUnit\Util\Test as UtilTest;
use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait;
use Symfony\Bridge\PhpUnit\SymfonyTestsListener;
class DrupalListener implements TestListener {
use TestListenerDefaultImplementation;
use DeprecationListenerTrait;
use DrupalComponentTestListenerTrait;
use DrupalStandardsListenerTrait;
protected $methodsWithVoidReturn = [
'setUpBeforeClass',
'setUp',
'assertPreConditions',
'assertPostConditions',
'tearDown',
'tearDownAfterClass',
'onNotSuccessfulTest',
];
private $symfonyListener;
public function __construct() {
$this->symfonyListener = new SymfonyTestsListener();
}
public function startTestSuite(TestSuite $suite) : void {
$this->symfonyListener
->startTestSuite($suite);
$this
->registerErrorHandler();
}
public function addSkippedTest(Test $test, \Throwable $t, float $time) : void {
$this->symfonyListener
->addSkippedTest($test, $t, $time);
}
public function startTest(Test $test) : void {
$annotations = UtilTest::parseTestMethodAnnotations(get_class($test), $test
->getName(FALSE));
if (isset($annotations['method']['expectedDeprecation'])) {
@trigger_error('The @expectedDeprecation annotation on ' . get_class($test) . '::' . $test
->getName(FALSE) . '() is deprecated in drupal:9.1.0 and is removed from drupal:10.0.0. Use the expectDeprecation() method instead. See https://www.drupal.org/node/3176667', E_USER_DEPRECATED);
}
$this
->registerErrorHandler();
$this->symfonyListener
->startTest($test);
$class = new \ReflectionClass($test);
foreach ($this->methodsWithVoidReturn as $method) {
if ($class
->hasMethod($method)) {
$reflected_method = $class
->getMethod($method);
if ($reflected_method
->getDeclaringClass()
->getName() === get_class($test)) {
if (!$reflected_method
->hasReturnType() || $reflected_method
->getReturnType()
->getName() !== 'void') {
@trigger_error("Declaring ::{$method} without a void return typehint in " . get_class($test) . " is deprecated in drupal:9.0.0. Typehinting will be required before drupal:10.0.0. See https://www.drupal.org/node/3114724", E_USER_DEPRECATED);
}
}
}
}
if ($class
->hasProperty('modules') && !$class
->getProperty('modules')
->isProtected()) {
@trigger_error('The ' . get_class($test) . '::$modules property must be declared protected. See https://www.drupal.org/node/2909426', E_USER_DEPRECATED);
}
}
public function endTest(Test $test, float $time) : void {
if (!SymfonyTestsListenerTrait::$previousErrorHandler) {
$className = get_class($test);
$groups = UtilTest::getGroups($className, $test
->getName(FALSE));
if (in_array('legacy', $groups, TRUE)) {
$symfony_error_handler = set_error_handler([
SymfonyTestsListenerTrait::class,
'handleError',
]);
}
}
$this
->deprecationEndTest($test, $time);
$this->symfonyListener
->endTest($test, $time);
$this
->componentEndTest($test, $time);
$this
->standardsEndTest($test, $time);
if (isset($symfony_error_handler)) {
restore_error_handler();
}
$this
->removeErrorHandler();
}
}