jangregor / phpstan-prophecy
为 phpspec/prophecy 提供 phpstan/phpstan 扩展
1.0.2
2024-04-03 08:15 UTC
Requires
- php: ^7.1 || ^8.0
- phpstan/phpstan: ^1.0.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.1.1
- ergebnis/license: ^1.0.0
- ergebnis/php-cs-fixer-config: ~2.2.0
- phpspec/prophecy: ^1.7.0
- phpunit/phpunit: ^6.0.0 || ^7.0.0 || ^8.0.0 || ^9.0.0
Conflicts
- phpspec/prophecy: <1.7.0 || >=2.0.0
- phpunit/phpunit: <6.0.0 || >=12.0.0
README
为 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
将理解 $testDouble
是 SomeModel
的实例。
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
将理解 $testDouble
是 SomeModel
的实例。
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
是实现了 SomeInterface
的 SomeModel
的实例。
方法预测
<?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
。