fix: support higher-order method calls on expectations - #17
Conversation
a07b9e7 to
bca9ad6
Compare
MrPunyapal
left a comment
There was a problem hiding this comment.
Tests pass locally. The main case is fixed. Requesting small changes below.
src/Type/Pest/ExpectationMethodsExtension.php
hasMethod returns true for any method name. I checked and expect($post)->typoMethod() no longer errors. Same for expect('string')->foo(). Can we only skip the error when the method exists on the value type? Right now typos are hidden.
src/Type/Pest/HigherOrderExpectationTypeExtension.php
When the method is missing you return null. When getMethod throws you return Mixed wrapped in HigherOrderExpectation. Pick one. I think wrap in Mixed in both cases so the chain type stays the same.
tests/Type/data/higher-order-expectations.php
Please add the exact case from the issue. One method plus toBe is covered. Missing is method plus toBe plus method again:
expect($policy)->view()->toBeTrue()->update()->toBeTrue()
That is what broke before. Without it the reset logic is not tested.
src/Type/Pest/PestConfigReader.php
src/Type/Pest/PestHookPropertyReader.php
These ??= changes are unrelated to this fix. Please remove them from this PR or split them out.
src/Type/Pest/ExpectationMethodReflection.php
This accepts any args and returns Mixed. So wrong args like typedMethod('string') for int param pass silently. Same for protected methods. I am ok to keep it for now since properties work the same way, but please note it as a limit or follow up.
Style:
Run pint on the new files. There are spacing and brace issues in the new extension files.
7b76b98 to
df83a63
Compare
|
@MrPunyapal, I have updated the PR as per your comment. I reverted the rector fixes as it was out of scope and will probably submit another PR for that. Sorry for the mess, still learning how everything works. 😅 |
Fixes pestphp/pest#1896
Problem
When chaining method calls directly on an expectation:
It causes two PHPStan errors:
Call to an undefined method Pest\Expectation<UserPolicy>::view()Calling toBeTrue() on Expectation<UserPolicy>; assertion is impossible.Solution
HigherOrderExpectationTypeExtension&ExpectationChainStateResolver):TValue).toBeTrue(), etc.).MixedTypewrapped inHigherOrderExpectationwhen methods are unresolvable or reflection fails to ensure chain stability.HigherOrderExpectationMethodIgnoreExtension):IgnoreErrorExtensionto ignoremethod.notFoundonly if the method exists onTValue.expect($post)->typoMethod()orexpect('string')->foo()) are properly reported as errors by PHPStan.