Verified Commit 89e64c1f authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2928249 by alexpott, mondrake: Introduce a PHPUnit 6+ compatibility...

Issue #2928249 by alexpott, mondrake: Introduce a PHPUnit 6+ compatibility layer for Drupal\Tests\Listeners classes
parent f320141d
Loading
Loading
Loading
Loading
+2 −2
Changes for core/modules/simpletest/src/WebTestBase.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\Tests\EntityViewTrait;
use Drupal\Tests\block\Traits\BlockCreationTrait as BaseBlockCreationTrait;
use Drupal\Tests\Listeners\DeprecationListener;
use Drupal\Tests\Listeners\DeprecationListenerTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\Traits\Core\CronRunTrait;
@@ -698,7 +698,7 @@ protected function curlHeaderCallback($curlHandler, $header) {
      if ($parameters[1] === 'User deprecated function') {
        if (getenv('SYMFONY_DEPRECATIONS_HELPER') !== 'disabled') {
          $message = (string) $parameters[0];
          if (!in_array($message, DeprecationListener::getSkippedDeprecations())) {
          if (!in_array($message, DeprecationListenerTrait::getSkippedDeprecations())) {
            call_user_func_array([&$this, 'error'], $parameters);
          }
        }
+2 −7
Changes for core/phpunit.xml.dist: 2 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -49,16 +49,11 @@
    </testsuite>
  </testsuites>
  <listeners>
    <listener class="\Drupal\Tests\Listeners\DeprecationListener">
    <listener class="\Drupal\Tests\Listeners\DrupalListener">
    </listener>
    <!-- The Symfony deprecation listener has to come after the Drupal
    deprecation listener -->
    <!-- The Symfony deprecation listener has to come after the Drupal listener -->
    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
    </listener>
    <listener class="\Drupal\Tests\Listeners\DrupalStandardsListener">
    </listener>
    <listener class="\Drupal\Tests\Listeners\DrupalComponentTestListener">
    </listener>
  </listeners>
  <!-- Filter for coverage reports. -->
  <filter>
+9 −3
Changes for core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -9,12 +9,18 @@
 *   This class will be removed once all the deprecation notices have been
 *   fixed.
 */
class DeprecationListener extends \PHPUnit_Framework_BaseTestListener {
trait DeprecationListenerTrait {

  /**
   * {@inheritdoc}
   * Reacts to the end of a test.
   *
   * @param \PHPUnit\Framework\Test|\PHPUnit_Framework_Test $test
   *   The test object that has ended its test run.
   * @param float $time
   *   The time the test took.
   */
  public function endTest(\PHPUnit_Framework_Test $test, $time) {
  protected function deprecationEndTest($test, $time) {
    /** @var \PHPUnit\Framework\Test $test */
    // Need to edit the file of deprecations.
    if ($file = getenv('SYMFONY_DEPRECATIONS_SERIALIZE')) {
      $deprecations = file_get_contents($file);
+13 −5
Changes for core/tests/Drupal/Tests/Listeners/DrupalComponentTestListenerTrait.php: 13 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -5,20 +5,28 @@
use Drupal\KernelTests\KernelTestBase;;
use Drupal\Tests\BrowserTestBase;;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\AssertionFailedError;

/**
 * Ensures that no component tests are extending a core test base class.
 *
 * @internal
 */
class DrupalComponentTestListener extends BaseTestListener {
trait DrupalComponentTestListenerTrait {

  /**
   * {@inheritdoc}
   * Reacts to the end of a test.
   *
   * @param \PHPUnit\Framework\Test|\PHPUnit_Framework_Test $test
   *   The test object that has ended its test run.
   * @param float $time
   *   The time the test took.
   */
  public function endTest(\PHPUnit_Framework_Test $test, $time) {
  protected function componentEndTest($test, $time) {
    /** @var \PHPUnit\Framework\Test $test */
    if (substr($test->toString(), 0, 22) == 'Drupal\Tests\Component') {
      if ($test instanceof BrowserTestBase || $test instanceof KernelTestBase || $test instanceof UnitTestCase) {
        $error = new \PHPUnit_Framework_AssertionFailedError('Component tests should not extend a core test base class.');
        $error = new AssertionFailedError('Component tests should not extend a core test base class.');
        $test->getTestResultObject()->addFailure($test, $error, $time);
      }
    }
+36 −0
Changes for core/tests/Drupal/Tests/Listeners/DrupalListener.php: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\Listeners;

use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
  class_alias('Drupal\Tests\Listeners\Legacy\DrupalListener', 'Drupal\Tests\Listeners\DrupalListener');
  // Using an early return instead of a else does not work when using the
  // PHPUnit phar due to some weird PHP behavior (the class gets defined without
  // executing the code before it and so the definition is not properly
  // conditional).
}
else {
  /**
   * Listens to PHPUnit test runs.
   *
   * @internal
   */
  class DrupalListener extends BaseTestListener {
    use DeprecationListenerTrait;
    use DrupalComponentTestListenerTrait;
    use DrupalStandardsListenerTrait;

    /**
     * {@inheritdoc}
     */
    public function endTest(Test $test, $time) {
      $this->deprecationEndTest($test, $time);
      $this->componentEndTest($test, $time);
      $this->standardsEndTest($test, $time);
    }

  }
}
Loading