Commit 90dd2d45 authored by catch's avatar catch
Browse files

fix: #3556315 Current approach to ensuring RunTestsInSeparateProcesses is added does not work

By: alexpott
By: mondrake
By: longwave
(cherry picked from commit 8e1a45a4)
parent 9ba7f47e
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\KernelTests\KernelTestBase;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

// cspell:ignore nyan

@@ -15,6 +16,7 @@
 */
#[IgnoreDeprecations]
#[Group('Theme')]
#[RunTestsInSeparateProcesses]
class ThemeEngineTest extends KernelTestBase {

  /**
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ parameters:
    - phpstan-tmp/*
    # Skip Drupal's own PHPStan rules test fixtures.
    - tests/PHPStan/fixtures/*
    # Skip Drupal's own PHPStan vendor directory
    - tests/PHPStan/vendor/*
    # Skip Drupal 6 & 7 code.
    - scripts/dump-database-d?.sh
    - scripts/generate-d?-content.sh
+2 −0
Original line number Diff line number Diff line
@@ -102,6 +102,8 @@ public function testTearDownWithoutSetUp(): void {

/**
 * A class extending BrowserTestBase for testing purposes.
 *
 * @phpstan-ignore testClass.missingAttribute.Group, testClass.missingAttribute.RunInSeparateProcesses
 */
class BrowserTestBaseMockableClassTest extends BrowserTestBase {

+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ public function testCapabilities($expected, ?string $mink_driver_args_webdriver,
    $this->putEnv("MINK_DRIVER_ARGS_WEBDRIVER", $mink_driver_args_webdriver);
    $this->putEnv("MINK_DRIVER_ARGS", $mink_driver_args);

    // @phpstan-ignore testClass.missingAttribute.Group, testClass.missingAttribute.RunInSeparateProcesses
    $object = new class('test') extends WebDriverTestBase {
    };
    $method = new \ReflectionMethod($object, 'getMinkDriverArgs');
+36 −0
Original line number Diff line number Diff line
@@ -6,12 +6,16 @@

// cspell:ignore analyse testdox

use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\BrowserTestBase;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\InClassNode;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

/**
@@ -128,6 +132,38 @@ public function processNode(Node $node, Scope $scope): array {
          }
        }
      }

      // Concrete test classes should have the Group attribute and in some cases
      // the RunTestsInSeparateProcesses attribute.
      $has_group_attribute = FALSE;
      $has_run_in_separate_process_attribute = FALSE;
      foreach ($class->getAttributes() as $attribute) {
        switch ($attribute->getName()) {
          case Group::class:
            $has_group_attribute = TRUE;
            break;

          case RunTestsInSeparateProcesses::class:
            $has_run_in_separate_process_attribute = TRUE;
            break;
        }
      }
      $should_run_in_separate_process =
        $class->isSubclassOfClass($this->reflectionProvider->getClass(BrowserTestBase::class)) ||
        $class->isSubclassOfClass($this->reflectionProvider->getClass(KernelTestBase::class));
      if ($should_run_in_separate_process && !$has_run_in_separate_process_attribute) {
        $fails[] = RuleErrorBuilder::message("Test class {$class->getName()} must have attribute \PHPUnit\Framework\Attributes\RunInSeparateProcesses.")
          ->identifier('testClass.missingAttribute.RunInSeparateProcesses')
          ->line($node->getStartLine())
          ->build();
      }
      if (!$has_group_attribute) {
        $fails[] = RuleErrorBuilder::message("Test class {$class->getName()} must have attribute \PHPUnit\Framework\Attributes\Group.")
          ->identifier('testClass.missingAttribute.Group')
          ->line($node->getStartLine())
          ->build();
      }

    }

    return $fails;
Loading