diff --git a/AGENTS.md b/AGENTS.md index a40e855..4ff333d 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,6 +11,10 @@ Layout: - `src/Plugin.php`: entry point. Registers `stubs/TestCase.phpstub` and `Hooks\TestCaseHandler`. - `src/Hooks/TestCaseHandler.php`: all the hooks (`AfterClassLikeVisit`, `AfterClassLikeAnalysis`, `AfterCodebasePopulated`, `AfterMethodCallAnalysis`). +- `src/Hooks/TestSuiteFileHandler.php`: the `BeforeFileAnalysis` hook that marks the files of the `phpunit.xml` + test suites CLI-only (their top-level code and closures, such as Pest tests bound to a `TestCase`), the + files `TestCaseHandler::getTestSuitePaths()` returns. The classes and functions they declare are not marked: + library code may use `@api` test helpers. - `src/Hooks/AssertSameSuggestionHandler.php` and `src/Issue/Unspecific*Assertion.php`: the `AfterMethodCallAnalysis` hook and plugin issues that suggest `assertNull()`, `assertTrue()` / `assertFalse()` and `assertCount()` over `assertSame()`, and `assertSame()` over `assertEquals()` diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index 2ff381d..2e96f3e 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -90,6 +90,9 @@ final class TestCaseHandler implements DependsExternalUsingShallowClone::class => true, ]; + /** @var array> test suite paths by base directory */ + private static array $test_suite_paths = []; + private const DATA_PROVIDER_CLOSURE = 'PHPUnit\\Framework\\Attributes\\DataProviderClosure'; private const EXPECT_EXCEPTION_METHODS = [ @@ -220,8 +223,16 @@ private static function markTestCodeInternal(Codebase $codebase): void * The directories and files of the test suites in the PHPUnit configuration PHPUnit would use. * * @return list + * + * @internal */ - private static function getTestSuitePaths(string $base_dir): array + public static function getTestSuitePaths(string $base_dir): array + { + return self::$test_suite_paths[$base_dir] ??= self::readTestSuitePaths($base_dir); + } + + /** @return list */ + private static function readTestSuitePaths(string $base_dir): array { $config = false; foreach (['phpunit.xml', 'phpunit.dist.xml', 'phpunit.xml.dist'] as $file_name) { @@ -271,8 +282,10 @@ private static function getTestSuitePaths(string $base_dir): array /** * @param list $paths + * + * @internal */ - private static function isInPaths(string $file_path, array $paths): bool + public static function isInPaths(string $file_path, array $paths): bool { if ($paths === []) { return false; diff --git a/src/Hooks/TestSuiteFileHandler.php b/src/Hooks/TestSuiteFileHandler.php new file mode 100644 index 0000000..9205466 --- /dev/null +++ b/src/Hooks/TestSuiteFileHandler.php @@ -0,0 +1,25 @@ +getCodebase()->config->base_dir); + + if (TestCaseHandler::isInPaths($event->getStatementsSource()->getFilePath(), $test_paths)) { + $event->markFileAsCliOnly(); + } + } +} diff --git a/src/Plugin.php b/src/Plugin.php index 71839f2..d84a21f 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -20,6 +20,9 @@ public function __invoke(RegistrationInterface $psalm, ?\SimpleXMLElement $confi class_exists(Hooks\TestCaseHandler::class, true); $psalm->registerHooksFromClass(Hooks\TestCaseHandler::class); + class_exists(Hooks\TestSuiteFileHandler::class, true); + $psalm->registerHooksFromClass(Hooks\TestSuiteFileHandler::class); + class_exists(Hooks\AssertSameSuggestionHandler::class, true); $psalm->registerHooksFromClass(Hooks\AssertSameSuggestionHandler::class); diff --git a/tests/acceptance/CliOnly.feature b/tests/acceptance/CliOnly.feature index 3c48dde..773eea8 100644 --- a/tests/acceptance/CliOnly.feature +++ b/tests/acceptance/CliOnly.feature @@ -81,3 +81,220 @@ Feature: CliOnly """ When I run Psalm Then I see no errors + + Scenario: A test suite file calls a test case at top level and in a closure + Given I have the following code in "phpunit.xml" + """ + + + + tests + + + + """ + And I have the following code in "tests/helpers.php" + """ + ExampleTest::applicationBasePath(); + $nested = static function (): string { + return (static fn (): string => ExampleTest::applicationBasePath())(); + }; + """ + And I have the following code + """ + final class ExampleTest extends TestCase + { + public static function applicationBasePath(): string + { + return __DIR__; + } + } + """ + When I run Psalm + Then I see no errors + + Scenario: A closure of a test suite file bound to a test case calls its methods + Given I have the following code in "phpunit.xml" + """ + + + + tests + + + + """ + And I have the following code in "tests/ExampleTest.php" + """ + render(); + $this->assertTrue(true); + }); + """ + And I have the following code + """ + final class ExampleTest extends TestCase + { + public function render(): string + { + return ''; + } + } + """ + When I run Psalm + Then I see no errors + + Scenario: A file outside the test suites calling a test case is reported + Given I have the following code in "phpunit.xml" + """ + + + + tests + + + + """ + And I have the following code in "src/helpers.php" + """ + ExampleTest::applicationBasePath(); + """ + And I have the following code + """ + final class ExampleTest extends TestCase + { + public static function applicationBasePath(): string + { + return __DIR__; + } + } + """ + When I run Psalm + Then I see these errors + | Type | Message | Line | + | CliOnlyCall | The method NS\\ExampleTest::applicationBasePath() only runs from the command line (@psalm-cli-only), but this code may run outside of it: mark it @psalm-cli-only too, or check that PHP_SAPI === 'cli' first | 4 | + | CliOnlyCall | The method NS\\ExampleTest::applicationBasePath() only runs from the command line (@psalm-cli-only), but this code may run outside of it: mark it @psalm-cli-only too, or check that PHP_SAPI === 'cli' first | 5 | + And I see no other errors + + Scenario: A test suite listing files marks only those files + Given I have the following code in "phpunit.xml" + """ + + + + tests/listed.php + + + + """ + And I have the following code in "tests/listed.php" + """ + + + + tests/*/Feature + + + + """ + And I have the following code in "tests/Admin/Feature/helpers.php" + """ +