Provides a prophecy/prophecy extension for phpstan/phpstan.
Run
composer require --dev jangregor/phpstan-prophecyWhen using phpstan/extension-installer, no further setup is required.
When not using phpstan/extension-installer, extension.neon needs to be included in phpstan.neon:
includes:
+ - vendor/jangregor/phpstan-prophecy/extension.neon<?phpusePHPUnit\Framework;
finalclass ExampleTest extendsFramework\TestCase
{
private$prophecy;
protectedfunctionsetUp()
{
$this->prophecy = $this->prophesize(SomeModel::class);
}
publicfunctiontestSomething(): void
{
$prophecy = $this->prophesize(SomeModel::class);
$testDouble = $prophecy->reveal();
// ...
}
publicfunctiontestSomethingElse(): void
{
$testDouble = $this->prophecy->reveal();
// ...
}
publicfunctiontestSomethingDifferent(): void
{
$testDouble = $this->createProphecy()->reveal();
// ...
}
privatefunctioncreateProphecy()
{
return$this->prophesize(SomeModel::class);
}
}💡 With this extension enabled, phpstan/phpstan will understand that $testDouble is an instance of SomeModel.
<?phpusePHPUnit\Framework;
finalclass ExampleTest extendsFramework\TestCase
{
private$prophecy;
protectedfunctionsetUp()
{
$this->prophecy = $this->prophesize()->willExtend(SomeModel::class);
}
publicfunctiontestSomething(): void
{
$prophecy = $this->prophesize()->willExtend(SomeModel::class);
$testDouble = $prophecy->reveal();
// ...
}
publicfunctiontestSomethingElse(): void
{
$testDouble = $this->prophecy->reveal();
// ...
}
publicfunctiontestSomethingDifferent(): void
{
$testDouble = $this->createProphecy()->reveal();
// ...
}
privatefunctioncreateProphecy()
{
return$this->prophesize(SomeModel::class)->willExtend(SomeInterface::class);
}
}💡 With this extension enabled, phpstan/phpstan will understand that $testDouble is an instance of SomeModel.
<?phpusePHPUnit\Framework;
finalclass ExampleTest extendsFramework\TestCase
{
private$prophecy;
protectedfunctionsetUp()
{
$this->prophecy = $this->prophesize(SomeModel::class)->willImplement(SomeInterface::class);
}
publicfunctiontestSomething(): void
{
$prophecy = $this->prophesize(SomeModel::class)->willImplement(SomeInterface::class);
$testDouble = $prophecy->reveal();
// ...
}
publicfunctiontestSomethingElse(): void
{
$testDouble = $this->prophecy->reveal();
// ...
}
publicfunctiontestSomethingDifferent(): void
{
$testDouble = $this->createProphecy()->reveal();
// ...
}
privatefunctioncreateProphecy()
{
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.
<?phpusePHPUnit\Framework;
finalclass ExampleTest extendsFramework\TestCase
{
publicfunctiontestSomething(): 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()).
❗ Currently here are no checks in place to validate the arguments of methods that are invoked on prophecies.
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 bashto open a shell in the Docker container.
Please have a look at CHANGELOG.md.
Please have a look at CONTRIBUTING.md.
This package is licensed using the MIT License.
Please have a look at LICENSE.md.