jangregor/phpstan-prophecy

为 phpspec/prophecy 提供 phpstan/phpstan 扩展

安装次数: 10,202,788

依赖项: 325

建议者: 1

安全: 0

星标: 100

关注者: 4

分支: 29

开放问题: 5

类型:phpstan-extension

1.0.2 2024-04-03 08:15 UTC

This package is auto-updated.

Last update: 2024-09-10 06:16:35 UTC


README

Close Integrate Merge Triage

Latest Stable Version Total Downloads

Violinist Enabled

phpspec/prophecy 提供 phpstan/phpstan 扩展。

安装

运行

composer require --dev jangregor/phpstan-prophecy

配置

自动

当使用 phpstan/extension-installer 时,无需进一步设置。

手动

当不使用 phpstan/extension-installer 时,需要将 extension.neon 包含在 phpstan.neon

 includes:
+	- vendor/jangregor/phpstan-prophecy/extension.neon

使用

prophesize()reveal()

<?php

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
{
    private $prophecy;

    protected function setUp()
    {
        $this->prophecy = $this->prophesize(SomeModel::class);
    }

    public function testSomething(): void
    {
        $prophecy = $this->prophesize(SomeModel::class);

        $testDouble = $prophecy->reveal();

        // ...
    }

    public function testSomethingElse(): void
    {
        $testDouble = $this->prophecy->reveal();

        // ...
    }

    public function testSomethingDifferent(): void
    {
        $testDouble = $this->createProphecy()->reveal();

        // ...
    }

    private function createProphecy()
    {
        return $this->prophesize(SomeModel::class);
    }

}

💡 启用此扩展后,phpstan/phpstan 将理解 $testDoubleSomeModel 的实例。

prophesize()willExtend()reveal()

<?php

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
{
    private $prophecy;

    protected function setUp()
    {
        $this->prophecy = $this->prophesize()->willExtend(SomeModel::class);
    }

    public function testSomething(): void
    {
        $prophecy = $this->prophesize()->willExtend(SomeModel::class);

        $testDouble = $prophecy->reveal();

        // ...
    }

    public function testSomethingElse(): void
    {
        $testDouble = $this->prophecy->reveal();

        // ...
    }

    public function testSomethingDifferent(): void
    {
        $testDouble = $this->createProphecy()->reveal();

        // ...
    }

    private function createProphecy()
    {
        return $this->prophesize(SomeModel::class)->willExtend(SomeInterface::class);
    }
}

💡 启用此扩展后,phpstan/phpstan 将理解 $testDoubleSomeModel 的实例。

prophesize()willImplement()reveal()

<?php

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
{
    private $prophecy;

    protected function setUp()
    {
        $this->prophecy = $this->prophesize(SomeModel::class)->willImplement(SomeInterface::class);
    }

    public function testSomething(): void
    {
        $prophecy = $this->prophesize(SomeModel::class)->willImplement(SomeInterface::class);

        $testDouble = $prophecy->reveal();

        // ...
    }

    public function testSomethingElse(): void
    {
        $testDouble = $this->prophecy->reveal();

        // ...
    }

    public function testSomethingDifferent(): void
    {
        $testDouble = $this->createProphecy()->reveal();

        // ...
    }

    private function createProphecy()
    {
        return $this->prophesize(SomeModel::class)->willImplement(SomeInterface::class);
    }
}

💡 启用此扩展后,phpstan/phpstan 将理解 $testDouble 是实现了 SomeInterfaceSomeModel 的实例。

方法预测

<?php

use PHPUnit\Framework;

final class ExampleTest extends Framework\TestCase
{
    public function testSomething(): void
    {
        $prophecy = $this->prophesize(SomeModel::class);

        $prophecy
            ->doubleTheNumber(Argument::is(2))
            ->willReturn(4);

        $testDouble = $prophecy->reveal();

        // ...
    }
}

💡 启用此扩展后,phpstan/phpstan 将理解 $prophecy 接受对其预言的类(或使用 willImplement() 时,附加实现的接口)的所有方法调用的调用。

方法参数

❗ 目前没有检查来验证在预言上调用的方法的参数。

开发

通过 .docker/Dockerfile 提供开发环境。

运行

$ docker build --tag phpstan-prophecy .docker/

用于构建和标记 Docker 镜像。

运行

$ docker run -it --rm  --volume "$PWD":/var/www/html --workdir /var/www/html phpstan-prophecy bash

用于在 Docker 容器中打开 shell。

变更日志

请参阅 CHANGELOG.md

贡献

请参阅 CONTRIBUTING.md

许可

本软件包使用 MIT 许可证许可。

请参阅 LICENSE.md