ExpectDeprecationTrait.php 4.72 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Tests\Traits;

use Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListener as LegacySymfonyTestsListener;
use Symfony\Bridge\PhpUnit\SymfonyTestsListener;
7
use PHPUnit\Framework\TestCase;
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

/**
 * Adds the ability to dynamically set expected deprecation messages in tests.
 *
 * @internal
 *   This class should only be used by Drupal core and will be removed once
 *   https://github.com/symfony/symfony/pull/25757 is resolved.
 *
 * @todo Remove once https://github.com/symfony/symfony/pull/25757 is resolved.
 */
trait ExpectDeprecationTrait {

  /**
   * Sets an expected deprecation message.
   *
23
   * @param string $message
24 25
   *   The expected deprecation message.
   */
26 27 28 29 30 31 32 33 34 35 36
  protected function expectDeprecation($message) {
    $this->expectedDeprecations([$message]);
  }

  /**
   * Sets expected deprecation messages.
   *
   * @param string[] $messages
   *   The expected deprecation messages.
   */
  public function expectedDeprecations(array $messages) {
37 38 39 40 41 42 43 44
    if (class_exists('PHPUnit_Util_Test', FALSE)) {
      $test_util = 'PHPUnit_Util_Test';
      $assertion_failed_error = 'PHPUnit_Framework_AssertionFailedError';
    }
    else {
      $test_util = 'PHPUnit\Util\Test';
      $assertion_failed_error = 'PHPUnit\Framework\AssertionFailedError';
    }
45 46 47 48 49 50
    if ($this instanceof \PHPUnit_Framework_TestCase || $this instanceof TestCase) {
      // Ensure the class or method is in the legacy group.
      $groups = $test_util::getGroups(get_class($this), $this->getName(FALSE));
      if (!in_array('legacy', $groups, TRUE)) {
        throw new $assertion_failed_error('Only tests with the `@group legacy` annotation can call `setExpectedDeprecation()`.');
      }
51 52 53

      // If setting an expected deprecation there is no need to be strict about
      // testing nothing as this is an assertion.
54 55
      $this->getTestResultObject()
        ->beStrictAboutTestsThatDoNotTestAnything(FALSE);
56

57 58 59 60 61 62 63 64
      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);
65

66 67 68 69 70 71 72 73 74
        // 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;
75 76
      }
    }
77 78 79 80 81 82 83 84 85 86 87 88 89

    // 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;
90
    }
91 92

    throw new $assertion_failed_error('Can not set an expected deprecation message because the Symfony\Bridge\PhpUnit\SymfonyTestsListener is not registered as a PHPUnit test listener.');
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  }

  /**
   * Gets the SymfonyTestsListenerTrait.
   *
   * @return \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait|null
   *   The SymfonyTestsListenerTrait object or NULL is a Symfony test listener
   *   is not present.
   */
  private function getSymfonyTestListenerTrait() {
    $test_result_object = $this->getTestResultObject();
    $reflection_class = new \ReflectionClass($test_result_object);
    $reflection_property = $reflection_class->getProperty('listeners');
    $reflection_property->setAccessible(TRUE);
    $listeners = $reflection_property->getValue($test_result_object);
    foreach ($listeners as $listener) {
      if ($listener instanceof SymfonyTestsListener || $listener instanceof LegacySymfonyTestsListener) {
        $reflection_class = new \ReflectionClass($listener);
        $reflection_property = $reflection_class->getProperty('trait');
        $reflection_property->setAccessible(TRUE);
        return $reflection_property->getValue($listener);
      }
    }
  }

}