jangregor / phpstan-prophecy
Provides a phpstan/phpstan extension for phpspec/prophecy
Installs: 10 965 673
Dependents: 325
Suggesters: 1
Security: 0
Stars: 102
Watchers: 4
Forks: 30
Open Issues: 7
Type:phpstan-extension
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
This package is auto-updated.
Last update: 2024-11-21 11:00:53 UTC
README
Provides a phpspec/prophecy
extension for phpstan/phpstan
.
Installation
Run
composer require --dev jangregor/phpstan-prophecy
Configuration
Automatic
When using phpstan/extension-installer
, no further setup is required.
Manual
When not using phpstan/extension-installer
, extension.neon
needs to be included in phpstan.neon
:
includes:
+ - vendor/jangregor/phpstan-prophecy/extension.neon
Usage
prophesize()
and 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); } }
💡 With this extension enabled, phpstan/phpstan
will understand that $testDouble
is an instance of SomeModel
.
prophesize()
, willExtend()
, and 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); } }
💡 With this extension enabled, phpstan/phpstan
will understand that $testDouble
is an instance of SomeModel
.
prophesize()
, willImplement()
, and 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); } }
💡 With this extension enabled, phpstan/phpstan
will understand that $testDouble
is an instance of SomeModel
that also implements SomeInterface
.
Method Predictions
<?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(); // ... } }
💡 With this extension enabled, phpstan/phpstan
will understand that $prophecy
accepts method calls to all methods that are implemented by its prophesized class (or additionally implemented interfaces, when using willImplement()
).
Method Arguments
❗ Currently here are no checks in place to validate the arguments of methods that are invoked on prophecies.
Development
A development environment is provided via .docker/Dockerfile
.
Run
$ docker build --tag phpstan-prophecy .docker/
to build and tag the Docker image.
Run
$ docker run -it --rm --volume "$PWD":/var/www/html --workdir /var/www/html phpstan-prophecy bash
to open a shell in the Docker container.
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.