Verified Commit f4a46dd0 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 7db0a117
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();
  }

  /**
+28 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\TestTools\Attribute;

/**
 * Defines an attribute to mark tests as skipped.
 *
 * Using this attribute to skip tests is preferred instead of
 * Assert::markTestSkipped(). DrupalTestCase::skipTestWithAttribute() will mark
 * tests with this attribute as skipped before Drupal is bootstrapped.
 *
 * @see \PHPUnit\Framework\Assert::markTestSkipped()
 * @see \Drupal\Tests\DrupalTestCase::skipTestWithAttribute()
 */
#[\Attribute(\Attribute::TARGET_METHOD)]
class Skip {

  /**
   * Constructs a Skip object.
   *
   * @param string $message
   *   (optional) Information about why the test is skipped.
   */
  public function __construct(public readonly string $message = '') {}

}
+2 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
use Drupal\Core\Database\Database;
use Drupal\Core\Site\Settings;
use Drupal\Tests\UnitTestCase;
use Drupal\TestTools\Attribute\Skip;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
@@ -326,9 +327,8 @@ public function testGetDeprecationWhenConfiguredAndNoReplacement(): void {
   */
  #[DataProvider('providerTestRealDeprecatedSettings')]
  #[IgnoreDeprecations]
  #[Skip('No settings to test real deprecated settings with.')]
  public function testRealDeprecatedSettings(string $legacy_setting, string $expected_deprecation): void {
    $this->markTestSkipped('No settings to test real deprecated settings with.');

    $settings_file_content = "<?php\n\$settings['$legacy_setting'] = 'foo';\n";
    $class_loader = NULL;
    $vfs_root = vfsStream::setup('root');
+18 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Tests;

use Drupal\TestTools\Attribute\Skip;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;

@@ -21,6 +22,23 @@ abstract class DrupalTestCase extends TestCase {
   */
  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) {
      static::markTestSkipped($attribute->newInstance()->message);
    }
  }

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