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
$modulesproperty, static or not, typed or not, declared in a class that descends from PHPUnit'sTestCase.
Not reported:
- a
$modulesthat 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 concreteBrowserTestBasedescendant whose name ends inTest, 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->defaultThemeitself, such as insetUp(); - update path tests, which install from a database dump;
- a test on a
NULLorFALSEprofile, 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
// 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
FunctionalorFunctionalJavascriptnamespace, as the fourth part of a module test's namespace, or in core'sFunctionalTestsandFunctionalJavascriptTests. The check covers everyBrowserTestBasedescendant. - 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
NULLprofile, which is what those bases set, so it also skips other tests on aNULLprofile, which are reported by phpstan-drupal. - phpstan-drupal reports a class that sets the theme in
setUp()or that overridesinstallDefaultThemeFromClassProperty(). The check leaves both alone.
drupal/component-test-core-base¶
- Level: error
- Source: Drupal core's PHPStan rules
- Ports: core's
ComponentTestDoesNotExtendCoreTestrule
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\Componentnamespace that extendsUnitTestCase,KernelTestBase,BuildTestBaseorBrowserTestBase, 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 inTest, and thatdrupal/test-class-suffixdoesn't take for a test. That is a class outside the lint rule's test directories, such as one directly intests/srcor insrc, a class whose parent's name doesn't end inTest,TestBaseorTestCase, 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
namespace Drupal\Tests\example\Unit;
use Drupal\Tests\UnitTestCase;
abstract class ExampleUnitBase extends UnitTestCase {
}
// 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.