function ExpectDeprecationTrait::expectedDeprecations

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php \Drupal\Tests\Traits\ExpectDeprecationTrait::expectedDeprecations()

Sets expected deprecation messages.

Parameters

string[] $messages: The expected deprecation messages.

3 calls to ExpectDeprecationTrait::expectedDeprecations()
ExpectDeprecationTrait::addExpectedDeprecationMessage in core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
Sets an expected deprecation message.
RendererCallbackTest::testCallback in core/tests/Drupal/Tests/Core/Render/RendererCallbackTest.php
Tests the expected deprecations are triggered by Renderer::doCallback().
ReverseProxyMiddlewareTest::testReverseProxyEnabledLegacy in core/tests/Drupal/Tests/Core/StackMiddleware/ReverseProxyMiddlewareTest.php
Tests that subscriber sets trusted headers when reverse proxy is set.

File

core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php, line 56

Class

ExpectDeprecationTrait
Adds the ability to dynamically set expected deprecation messages in tests.

Namespace

Drupal\Tests\Traits

Code

public function expectedDeprecations(array $messages) {
    if ($this instanceof TestCase) {
        // Ensure the class or method is in the legacy group.
        $groups = Test::getGroups(get_class($this), $this->getName(FALSE));
        if (!in_array('legacy', $groups, TRUE)) {
            throw new AssertionFailedError('Only tests with the `@group legacy` annotation can call `setExpectedDeprecation()`.');
        }
        // If setting an expected deprecation there is no need to be strict about
        // testing nothing as this is an assertion.
        $this->getTestResultObject()
            ->beStrictAboutTestsThatDoNotTestAnything(FALSE);
        if ($trait = $this->getSymfonyTestListenerTrait()) {
            // Add the expected deprecation message to the class property.
            $reflection_class = new \ReflectionClass($trait);
            $expected_deprecations_property = $reflection_class->getProperty('expectedDeprecations');
            $expected_deprecations_property->setAccessible(TRUE);
            $expected_deprecations = $expected_deprecations_property->getValue($trait);
            $expected_deprecations = array_merge($expected_deprecations, $messages);
            $expected_deprecations_property->setValue($trait, $expected_deprecations);
            // Register the error handler if necessary.
            $previous_error_handler_property = $reflection_class->getProperty('previousErrorHandler');
            $previous_error_handler_property->setAccessible(TRUE);
            $previous_error_handler = $previous_error_handler_property->getValue($trait);
            if (!$previous_error_handler) {
                $previous_error_handler = set_error_handler([
                    $trait,
                    'handleError',
                ]);
                $previous_error_handler_property->setValue($trait, $previous_error_handler);
            }
            return;
        }
    }
    // Expected deprecations set by isolated tests need to be written to a file
    // so that the test running process can take account of them.
    if ($file = getenv('DRUPAL_EXPECTED_DEPRECATIONS_SERIALIZE')) {
        $expected_deprecations = file_get_contents($file);
        if ($expected_deprecations) {
            $expected_deprecations = array_merge(unserialize($expected_deprecations), $messages);
        }
        else {
            $expected_deprecations = $messages;
        }
        file_put_contents($file, serialize($expected_deprecations));
        return;
    }
    throw new AssertionFailedError('Can not set an expected deprecation message because the Symfony\\Bridge\\PhpUnit\\SymfonyTestsListener is not registered as a PHPUnit test listener.');
}

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