You are here

protected function DeprecationListenerTrait::registerErrorHandler in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php \Drupal\Tests\Listeners\DeprecationListenerTrait::registerErrorHandler()

Registers an error handler that wraps Symfony's DeprecationErrorHandler.

See also

\Symfony\Bridge\PhpUnit\DeprecationErrorHandler

\Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait

1 call to DeprecationListenerTrait::registerErrorHandler()
DeprecationListenerTrait::deprecationStartTest in core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php

File

core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php, line 158

Class

DeprecationListenerTrait
Removes deprecations that we are yet to fix.

Namespace

Drupal\Tests\Listeners

Code

protected function registerErrorHandler($test) {
  $deprecation_handler = function ($type, $msg, $file, $line, $context = []) {

    // Skip listed deprecations.
    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);

  // Register another listener so that we can remove the error handler before
  // Symfony's DeprecationErrorHandler checks that it is the currently
  // registered handler. Note this is done like this to ensure the error
  // handler is removed after SymfonyTestsListenerTrait::endTest() is called.
  // SymfonyTestsListenerTrait has its own error handler that needs to be
  // removed before this one.
  $test_result_object = $test
    ->getTestResultObject();

  // It's possible that a test does not have a result object. This can happen
  // when a test class does not have any test methods.
  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);
  }
}