fp4php/psalm-toolkit

psalm 插件的测试工具

v3.6.1 2023-01-30 09:26 UTC

README

插件编写和静态测试工具的辅助工具。

安装

必须手动安装包 fp4php/functional

$ composer require --dev fp4php/functional fp4php/psalm-toolkit
$ vendor/bin/psalm-plugin enable fp4php/psalm-toolkit

使用方法

目前您可以使用两种方法进行静态断言

  • seePsalmIssue: 检查来自 haveCode 的代码块是否存在特定问题。
  • seeReturnType: 验证来自 haveCode 块的返回类型。

以下为使用示例

<?php

namespace Fp\Decode\Test\Static;

use Fp\PsalmToolkit\StaticTest\PsalmTest;
use Fp\PsalmToolkit\StaticTest\StaticTestCase;
use Fp\PsalmToolkit\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,
            );
    }
}