klmick / psalm-test
v1.2.3
2022-01-12 16:02 UTC
Requires
- php: ^8.0
- vimeo/psalm: ^4.13
- whsv26/functional: ^4.10
README
psalm插件的静态测试工具。
安装
$ composer require --dev klimick/psalm-test
$ vendor/bin/psalm-plugin enable klimick/psalm-test
使用方法
目前您可以使用两种方法进行静态断言
seePsalmIssue
:检查来自haveCode
的代码块是否存在特定的错误。seeReturnType
:验证来自haveCode
块的返回类型。
以下为使用示例
<?php namespace Klimick\Decode\Test\Static; use Klimick\PsalmTest\PsalmTest; use Klimick\PsalmTest\StaticTestCase; use Klimick\PsalmTest\StaticType\StaticTypes as t; final class ExampleTest extends PsalmTest { public function __invoke(): void { StaticTestCase::describe('See InvalidScalarArgument issue') ->haveCode(function() { $plus = fn(int $a, int $b): int => $a + $b; $plus(10, 10.00); }) ->seePsalmIssue( type: 'InvalidScalarArgument', message: 'Argument 2 expects int, float(10) provided', ); StaticTestCase::describe('See return type (invariant type compare)') ->haveCode(function() { return [ 'twenty' => 10 + 10, 'message' => 'Hello world!' ]; }) ->seeReturnType( is: t::shape([ 'twenty' => t::literal(20), 'message' => t::literal('Hello world!'), ]), ); StaticTestCase::describe('See return type (covariant type compare)') ->haveCode(function() { return [ 'twenty' => 10 + 10, 'message' => 'Hello world!' ]; }) ->seeReturnType( is: t::shape([ 'twenty' => t::int(), 'message' => t::string(), ]), invariant: false, ); } }