trait ExpectDeprecationTrait

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/TestTools/Extension/DeprecationBridge/ExpectDeprecationTrait.php \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait
  2. 9 core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php \Drupal\Tests\Traits\ExpectDeprecationTrait
  3. 8.9.x core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php \Drupal\Tests\Traits\ExpectDeprecationTrait

A trait to include in Drupal tests to manage expected deprecations.

This code works in coordination with DeprecationHandler.

In the future this extension might be dropped if PHPUnit adds support for ignoring a specified list of deprecations.

@internal

Hierarchy

Deprecated

in drupal:11.4.0 and is removed from drupal:12.0.0. Use $this->expectUserDeprecationMessage() or $this->expectUserDeprecationMessageMatches() instead.

See also

https://www.drupal.org/node/3545276

4 files declare their use of ExpectDeprecationTrait
BrowserTestBase.php in core/tests/Drupal/Tests/BrowserTestBase.php
ExpectDeprecationTest.php in core/tests/Drupal/Tests/ExpectDeprecationTest.php
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php
UnitTestCase.php in core/tests/Drupal/Tests/UnitTestCase.php

File

core/tests/Drupal/TestTools/Extension/DeprecationBridge/ExpectDeprecationTrait.php, line 25

Namespace

Drupal\TestTools\Extension\DeprecationBridge
View source
trait ExpectDeprecationTrait {
  
  /**
   * Adds an expected deprecation.
   *
   * @param string $message
   *   The expected deprecation message.
   *
   * @deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use
   *   $this->expectUserDeprecationMessage() or
   *   $this->expectUserDeprecationMessageMatches() instead.
   *
   * @see https://www.drupal.org/node/3545276
   */
  public function expectDeprecation(string $message) : void {
    @trigger_error('expectDeprecation() is deprecated in drupal:11.4.0 and is removed from drupal:12.0.0. Use $this->expectUserDeprecationMessage() or $this->expectUserDeprecationMessageMatches() instead. See https://www.drupal.org/node/3545276', E_USER_DEPRECATED);
    $this->expectUserDeprecationMessageMatches($this->regularExpressionForFormatDescription('%A' . $message . '%A'));
  }
  private function regularExpressionForFormatDescription(string $string) : string {
    $string = strtr(preg_quote($string, '/'), [
      '%%' => '%',
      '%e' => preg_quote(\DIRECTORY_SEPARATOR, '/'),
      '%s' => '[^\\r\\n]+',
      '%S' => '[^\\r\\n]*',
      '%a' => '.+?',
      '%A' => '.*?',
      '%w' => '\\s*',
      '%i' => '[+-]?\\d+',
      '%d' => '\\d+',
      '%x' => '[0-9a-fA-F]+',
      '%f' => '[+-]?(?:\\d+|(?=\\.\\d))(?:\\.\\d+)?(?:[Ee][+-]?\\d+)?',
      '%c' => '.',
      '%0' => '\\x00',
    ]);
    return '/^' . $string . '$/s';
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.