Skip to content

Tests

The analyzer checks Drupal's test classes. Assertions, mocks and Prophecy doubles are typed by friendsofmago/mago-phpunit, which is installed with this package and runs in its worker. The phpunit/ analyzer checks and linter rules come from that package too, and are described in its README.


drupal/test-modules-visibility

  • Level: error
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's TestClassesProtectedPropertyModulesRule

phpstan-drupal reports a test class whose $modules property is public. Drupal's test bases declare it protected, see the change record.

Reports:

  • a public $modules property, static or not, typed or not, declared in a class that descends from PHPUnit's TestCase.

Not reported:

  • a $modules that the class inherits from a parent or gets from a trait. The class that declares it is the one to fix.
Compared with phpstan-drupal

phpstan-drupal also reports a $modules declared with var, which PHP makes public too, and a public $modules declared by a trait, once for each test class that uses the trait.


drupal/browser-test-default-theme

  • Level: error
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's BrowserTestBaseDefaultThemeRule

phpstan-drupal reports a functional test that doesn't set $defaultTheme. When the install profile of the test doesn't install a theme, Drupal throws while it sets up the test.

Reports:

  • a browser test on a themeless profile that doesn't set $defaultTheme. The test is a concrete BrowserTestBase descendant whose name ends in Test, and the property counts when it is set on the class, on a base class, or by a trait that either of them uses.

The check finds the test's $profile the same way, on the class, a base class or a trait. A profile is themeless when it doesn't ship a system.theme config in config/sync or config/install, which is checked by Drupal when the test runs. Profiles are read from core, from the site's profiles directory, and from the tests/profiles directories of core's modules and of extensions. A profile not found there counts as themeless when it is one of core's themeless test profiles, such as testing.

Not reported:

  • a class that overrides installDefaultThemeFromClassProperty(), itself, through a base class or through a trait, since the override may set the theme at runtime;
  • a class that assigns $this->defaultTheme itself, such as in setUp();
  • update path tests, which install from a database dump;
  • a test on a NULL or FALSE profile, which installs from existing configuration and takes its theme;
  • a profile that isn't a string literal, such as one read from a constant.
Example
modules/example/tests/src/Functional/ExampleTest.php
// Reported.
namespace Drupal\Tests\example\Functional;

use Drupal\Tests\BrowserTestBase;

final class ExampleTest extends BrowserTestBase {

  public function testPage(): void {}

}

// Fine.
namespace Drupal\Tests\example\Functional;

use Drupal\Tests\BrowserTestBase;

final class ExampleTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  public function testPage(): void {}

}
Compared with phpstan-drupal
  • phpstan-drupal checks a class only in a Functional or FunctionalJavascript namespace, as the fourth part of a module test's namespace, or in core's FunctionalTests and FunctionalJavascriptTests. The check covers every BrowserTestBase descendant.
  • phpstan-drupal counts only core's seven themeless test profiles as themeless, and leaves a test on any other profile alone. The check reads the config of each profile from disk.
  • phpstan-drupal skips the two installer test bases that install from existing configuration by class name. The check skips a test on a NULL profile, which is what those bases set, so it also skips other tests on a NULL profile, which are reported by phpstan-drupal.
  • phpstan-drupal reports a class that sets the theme in setUp() or that overrides installDefaultThemeFromClassProperty(). The check leaves both alone.

drupal/component-test-core-base

  • Level: error
  • Source: Drupal core's PHPStan rules
  • Ports: core's ComponentTestDoesNotExtendCoreTest rule

Core's component tests run without Drupal, so they can't extend a test base that boots Drupal or needs its classes. Core's own PHPStan rules report a component test that does.

Reports:

  • a class in the Drupal\Tests\Component namespace that extends UnitTestCase, KernelTestBase, BuildTestBase or BrowserTestBase, directly or through its parents.

WebDriverTestBase counts, since it extends BrowserTestBase. The report is on the class whose own parent lies outside the component tests. So a component test base that extends a core one is reported once, and not again on the tests below it.

Compared with core's rule

Core's rule reports every component test below such a base too, not only the base.


drupal/test-class-name

  • Level: error
  • Source: phpstan-drupal
  • Ports: phpstan-drupal's TestClassSuffixNameRule, the part that needs the class's ancestry

PHPUnit runs only the files named *Test.php, and so do phpunit -c core and run-tests.sh, so a test class whose name doesn't end in Test never runs. The lint rule drupal/test-class-suffix reports most of them from the parent's name, with no types. The check reports the ones that the lint rule can't tell apart from other classes.

Reports:

  • a concrete class that descends from PHPUnit's TestCase, whose name doesn't end in Test, and that drupal/test-class-suffix doesn't take for a test. That is a class outside the lint rule's test directories, such as one directly in tests/src or in src, a class whose parent's name doesn't end in Test, TestBase or TestCase, and a class whose parent is declared in the same file without such a parent of its own.

Not reported:

  • a class that the lint rule reports, so a class gets one report when both run;
  • an abstract class and an anonymous class.
Example
modules/example/tests/src/Unit/ExampleUnitBase.php
namespace Drupal\Tests\example\Unit;

use Drupal\Tests\UnitTestCase;

abstract class ExampleUnitBase extends UnitTestCase {
}
modules/example/tests/src/Unit/ExampleChecks.php
// Reported.
namespace Drupal\Tests\example\Unit;

final class ExampleChecks extends ExampleUnitBase {
}

// Fine.
// The class and its file are named ExampleChecksTest.
namespace Drupal\Tests\example\Unit;

final class ExampleChecksTest extends ExampleUnitBase {
}
Compared with phpstan-drupal

phpstan-drupal reports every such class from one rule. The check leaves the classes that the lint rule reports to it, so a project that runs mago analyze without mago lint doesn't see those.