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
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand Down
17 changes: 15 additions & 2 deletions src/Hooks/TestCaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ final class TestCaseHandler implements
DependsExternalUsingShallowClone::class => true,
];

/** @var array<string, list<string>> 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 = [
Expand Down Expand Up @@ -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<string>
*
* @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<string> */
private static function readTestSuitePaths(string $base_dir): array
{
$config = false;
foreach (['phpunit.xml', 'phpunit.dist.xml', 'phpunit.xml.dist'] as $file_name) {
Expand Down Expand Up @@ -271,8 +282,10 @@ private static function getTestSuitePaths(string $base_dir): array

/**
* @param list<string> $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;
Expand Down
25 changes: 25 additions & 0 deletions src/Hooks/TestSuiteFileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Psalm\PhpUnitPlugin\Hooks;

use Psalm\Plugin\EventHandler\BeforeFileAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\BeforeFileAnalysisEvent;

/**
* The top-level code and the closures of a test suite file only run under PHPUnit, from the command line. That
* includes Pest tests, which are closures bound to a test case.
*/
final class TestSuiteFileHandler implements BeforeFileAnalysisInterface
{
#[\Override]
public static function beforeAnalyzeFile(BeforeFileAnalysisEvent $event): void
{
$test_paths = TestCaseHandler::getTestSuitePaths($event->getCodebase()->config->base_dir);

if (TestCaseHandler::isInPaths($event->getStatementsSource()->getFilePath(), $test_paths)) {
$event->markFileAsCliOnly();
}
}
}
3 changes: 3 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
217 changes: 217 additions & 0 deletions tests/acceptance/CliOnly.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "tests/helpers.php"
"""
<?php
namespace NS;

$basePath = ExampleTest::applicationBasePath();
$render = static fn (): string => 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"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "tests/ExampleTest.php"
"""
<?php
namespace NS;

/** @param \Closure(): void $test */
function it(\Closure $test): void {}

/** @psalm-scope-this ExampleTest */
it(function (): void {
$this->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"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "src/helpers.php"
"""
<?php
namespace NS;

$basePath = ExampleTest::applicationBasePath();
$render = 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 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"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<file>tests/listed.php</file>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "tests/listed.php"
"""
<?php
namespace NS;

$basePath = ExampleTest::applicationBasePath();
"""
And I have the following code in "tests/other.php"
"""
<?php
namespace NS;

$name = 'other';
$basePath = 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 | 5 |
And I see no other errors

Scenario: A test suite directory with a wildcard marks the files of the matching directories
Given I have the following code in "phpunit.xml"
"""
<phpunit>
<testsuites>
<testsuite name="unit">
<directory>tests/*/Feature</directory>
</testsuite>
</testsuites>
</phpunit>
"""
And I have the following code in "tests/Admin/Feature/helpers.php"
"""
<?php
namespace NS;

$basePath = ExampleTest::applicationBasePath();
"""
And I have the following code in "tests/Admin/Unit/helpers.php"
"""
<?php
namespace NS;

$name = 'other';
$basePath = 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 | 5 |
And I see no other errors

Scenario: Without a phpunit.xml a file calling a test case is reported
Given I have the following code in "tests/helpers.php"
"""
<?php
namespace NS;

$basePath = 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 |
And I see no other errors