Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/Hooks/TestCaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/acceptance/DeadCode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 58 additions & 0 deletions tests/acceptance/Internal.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"""
<?php
namespace Testing;

/** @api */
class LogTestCase extends \PHPUnit\Framework\TestCase
{
public static function getRecord(): void {}
}
"""
And I have the following code
"""
/** @psalm-cli-only */
final class Client
{
public function send(): void
{
\Testing\LogTestCase::getRecord();
}
}
"""
When I run Psalm
Then I see no errors

Scenario: Library code may use the code of the test suites marked @api
Given I have the following code in "phpunit.xml"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "tests/Helper.php"
"""
<?php
namespace Tests;

/** @api */
final class Helper {}
"""
And I have the following code
"""
final class Client
{
public function send(): void
{
new \Tests\Helper();
}
}
"""
When I run Psalm
Then I see no errors
Loading