Skip to content

Commit

Permalink
Fix Prophecy-related issues
Browse files Browse the repository at this point in the history
1. `prophesize()` has always been protected, not public
2. Provide generic definition for `prophesize()` implemented by `phpspec/prophecy-phpunit`
  • Loading branch information
weirdan committed Aug 24, 2020
1 parent 619506c commit 9c11bac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"codeception/codeception": "^4.0.3",
"composer/semver": "^1.4 || ^2.0 || ^3.0",
"squizlabs/php_codesniffer": "^3.3.1",
"weirdan/codeception-psalm-module": "^0.7.1"
"weirdan/codeception-psalm-module": "^0.7.1",
"weirdan/prophecy-shim": "^1.0 || ^2.0"
},
"extra": {
"psalm": {
Expand Down
13 changes: 13 additions & 0 deletions stubs/Prophecy.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ namespace Prophecy {
public static function allOf(...$tokens): Token\LogicalAndToken {}
}
}

namespace Prophecy\PhpUnit {
use Prophecy\Prophecy\ObjectProphecy;
trait ProphecyTrait
{
/**
* @template T
* @param class-string<T> $classOrInterface
* @return ObjectProphecy<T>
*/
protected function prophesize($classOrInterface): ObjectProphecy {}
}
}
2 changes: 1 addition & 1 deletion stubs/TestCase.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ abstract class TestCase extends Assert implements Test, SelfDescribing
* @param class-string<T> $classOrInterface
* @return ObjectProphecy<T>
*/
public function prophesize($classOrInterface): ObjectProphecy {}
protected function prophesize($classOrInterface): ObjectProphecy {}

/**
* @param class-string<\Throwable> $exception
Expand Down
18 changes: 18 additions & 0 deletions tests/acceptance/Prophecy.feature
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,22 @@ Feature: Prophecy
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 1 of Prophecy\Argument::that expects callable(mixed...):bool, Closure():string(hello) provided |
And I see no other errors

Scenario: prophesize() provided by ProphecyTrait is generic
Given I have the following code
"""
use Prophecy\PhpUnit\ProphecyTrait;
class SUT { public function getString(): string { return "zzz"; } }
class MyTestCase extends TestCase
{
use ProphecyTrait;
public function testSomething(): void {
$this->prophesize(SUT::class)->reveal()->getString();
}
}
"""
When I run Psalm
Then I see no errors

0 comments on commit 9c11bac

Please sign in to comment.