diff --git a/README.md b/README.md index 11e7545..1b304b8 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ public function testTypo(int $cents): void {} Tests, providers and `TestCase` classes are not unused code. Properties set in `setUp()` or a `#[Before]` method are initialized (no `MissingConstructor`). Exceptions declared by PHPUnit's API are not `MissingThrowsDocblock`. -The code of the test suites of `phpunit.xml` and the test cases are [internal](https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-internal), so they aren't part of a library's API with [`markAllNonInternalCodeAsApi`](https://psalm.dev/docs/running_psalm/configuration/#markallnoninternalcodeasapi). +The code of the test suites of `phpunit.xml` and the test cases are [internal](https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-internal), so they aren't part of a library's API with [`markAllNonInternalCodeAsApi`](https://psalm.dev/docs/running_psalm/configuration/#markallnoninternalcodeasapi). Mark a test case shipped for the users of a library to extend with [`@api`](https://psalm.dev/docs/annotating_code/supported_annotations/#api-psalm-api) to keep it in the API. ```php namespace Lib; diff --git a/src/Hooks/TestCaseHandler.php b/src/Hooks/TestCaseHandler.php index e2e0d79..2ff381d 100644 --- a/src/Hooks/TestCaseHandler.php +++ b/src/Hooks/TestCaseHandler.php @@ -183,7 +183,12 @@ private static function markTestCodeInternal(Codebase $codebase): void $namespaces = []; foreach ($codebase->classlike_storage_provider->getAll() as $name => $storage) { $file_path = $storage->location?->file_path; - if ($file_path === null || $storage->internal || !$codebase->config->isInProjectDirs($file_path)) { + // code marked @api is meant for the users of the project, even in a test suite + if ($file_path === null + || $storage->internal + || $storage->public_api + || !$codebase->config->isInProjectDirs($file_path) + ) { continue; } diff --git a/tests/acceptance/DeadCode.feature b/tests/acceptance/DeadCode.feature index cac6ae7..079821a 100644 --- a/tests/acceptance/DeadCode.feature +++ b/tests/acceptance/DeadCode.feature @@ -712,3 +712,16 @@ Feature: Dead code | UnusedClass | Class NS\Support\Helper is never used | | UnusedClass | Class NS\Billing\Tests\Fixture is never used | And I see no other errors + + Scenario: Test cases marked @api stay part of the API of a library + Given the config marks all non-internal code as API + And I have the following code + """ + /** @api */ + class LogTestCase extends TestCase + { + protected function getRecord(): void {} + } + """ + When I run Psalm with dead code detection + Then I see no errors diff --git a/tests/acceptance/Internal.feature b/tests/acceptance/Internal.feature index cc31e5c..8fbc12d 100644 --- a/tests/acceptance/Internal.feature +++ b/tests/acceptance/Internal.feature @@ -129,3 +129,61 @@ Feature: Internal test code """ When I run Psalm Then I see no errors + + Scenario: Library code may use a test case marked @api outside the test suites + Given I have the following code in "testing/LogTestCase.php" + """ + + + + tests + + + + """ + And I have the following code in "tests/Helper.php" + """ +