klimick/psalm-test

This package is abandoned and no longer maintained. The author suggests using the fp4php/psalm-toolkit package instead.

Testing tool for psalm plugins

v1.2.3 2022-01-12 16:02 UTC

This package is not auto-updated.

Last update: 2022-05-31 17:38:50 UTC


README

Static testing tool for psalm plugins.

Installation

$ composer require --dev klimick/psalm-test
$ vendor/bin/psalm-plugin enable klimick/psalm-test

Usage

At the moment you can use two methods for static asserts:

  • seePsalmIssue: Checks that a code block from the haveCode have specific issue.
  • seeReturnType: Verifies a return type from the haveCode block.

Usage example below:

<?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,
            );
    }
}