Test Psalm via phpt files!
composer require --dev phpyh/psalm-tester
tests/array_values.phpt
--FILE--
<?php
/** @psalm-trace $_list */
$_list = array_values(['a' => 1, 'b' => 2]);
--EXPECT--
Trace on line 9: $_list: non-empty-list<1|2>
To avoid hardcoding error details, you can use EXPECTF
:
--EXPECTF--
Trace on line %d: $_list: non-empty-list<%s>
tests/PsalmTest.php
<?php
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use PHPyh\PsalmTester\PsalmTester;
use PHPyh\PsalmTester\StaticAnalysisTest;
final class PsalmTest extends TestCase
{
private ?PsalmTester $psalmTester = null;
#[TestWith([__DIR__ . '/array_values.phpt'])]
public function testPhptFiles(string $phptFile): void
{
$this->psalmTester ??= PsalmTester::create();
$this->psalmTester->test(StaticAnalysisTest::fromPhptFile($phptFile));
}
}
By default PsalmTester
runs Psalm with --no-progress --no-diff --config=
psalm.xml.
You can change this at the PsalmTester
level:
use PHPyh\PsalmTester\PsalmTester;
PsalmTester::create(
defaultArguments: '--no-progress --no-cache --config=my_default_config.xml',
);
or for each test individually using --ARGS--
section:
--ARGS--
--no-progress --config=my_special_config.xml
--FILE--
...
--EXPECT--
...