DrupalComponentTestListenerTrait.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Drupal\Tests\Listeners;
  3. use Drupal\KernelTests\KernelTestBase;;
  4. use Drupal\Tests\BrowserTestBase;;
  5. use Drupal\Tests\UnitTestCase;
  6. use PHPUnit\Framework\AssertionFailedError;
  7. /**
  8. * Ensures that no component tests are extending a core test base class.
  9. *
  10. * @internal
  11. */
  12. trait DrupalComponentTestListenerTrait {
  13. /**
  14. * Reacts to the end of a test.
  15. *
  16. * @param \PHPUnit\Framework\Test|\PHPUnit_Framework_Test $test
  17. * The test object that has ended its test run.
  18. * @param float $time
  19. * The time the test took.
  20. */
  21. protected function componentEndTest($test, $time) {
  22. /** @var \PHPUnit\Framework\Test $test */
  23. if (substr($test->toString(), 0, 22) == 'Drupal\Tests\Component') {
  24. if ($test instanceof BrowserTestBase || $test instanceof KernelTestBase || $test instanceof UnitTestCase) {
  25. $error = new AssertionFailedError('Component tests should not extend a core test base class.');
  26. $test->getTestResultObject()->addFailure($test, $error, $time);
  27. }
  28. }
  29. }
  30. }