Verified Commit f7eadfbe authored by godotislate's avatar godotislate
Browse files

task: #3517430 Add an attribute for skipping PHPUnit tests

By: smustgrave
By: mstrelan
By: mondrake
By: dcam
By: catch
By: godotislate
parent 77cf7918
Loading
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase;
use Drupal\TestTools\Attribute\Skip;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\Attributes\TestWith;
@@ -129,10 +130,10 @@ public function testInvalidFormat(): void {
  /**
   * {@inheritdoc}
   */
  #[Skip]
  public function testLabelValidation(): void {
    // @todo Remove this override in https://www.drupal.org/i/3231354. The label of Editor entities is dynamically computed: it's retrieved from the associated FilterFormat entity. That issue will change this.
    // @see \Drupal\editor\Entity\Editor::label()
    $this->markTestSkipped();
  }

  /**
+19 −0
Original line number Diff line number Diff line
@@ -4,9 +4,11 @@

namespace Drupal\Tests;

use Drupal\TestTools\Attribute\Skip;
use Drupal\TestTools\ErrorHandler\BootstrapErrorHandler;
use Drupal\TestTools\Extension\DeprecationBridge\Configuration as DeprecationHandlerConfiguration;
use Drupal\TestTools\Extension\Dump\DebugDump;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\BeforeClass;
@@ -24,6 +26,23 @@ trait DrupalTestCaseTrait {
   */
  protected string $root;

  /**
   * Supports skipping tests with the Skip attribute.
   *
   * This is run with a high priority to prevent any setUp() functions from
   * executing, avoiding any Drupal bootstrap.
   *
   * @internal
   */
  #[Before(200)]
  final protected function skipTestWithAttribute(): void {
    $reflection = new \ReflectionMethod(static::class, $this->name());
    $attribute = $reflection->getAttributes(Skip::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? NULL;
    if ($attribute) {
      Assert::markTestSkipped($attribute->newInstance()->message);
    }
  }

  /**
   * Ensure that the $root property is set initially.
   *