pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.46k stars 341 forks source link

[Bug]: mixed-in expectation object causes IDE type errors #1166

Open wand2016 opened 3 months ago

wand2016 commented 3 months ago

What Happened

With using a mixed-in expectation object, method chaining results in IDE type errors:

        expect($response->content())
            ->toBeJson()
            ->json()
            ->not()->toHaveKey('errors')
            ->toHaveKey('data')
            ->data->toEqual(...);
             // ^ IDE type error occurs

https://github.com/pestphp/pest/blob/b33af71036ec9babcdacbc0e49db45a5152191d0/src/Mixins/Expectation.php#L60

Suggested fix:

-    * @return self<TValue>
+    * @return $this

Changing the PHPDoc return type to $this seems to be a better solution to avoid these IDE type errors.

How to Reproduce

        expect($response->content())
            ->toBeJson()
            ->json()
            ->not()->toHaveKey('errors')
            ->toHaveKey('data')
            ->data->toEqual([]);

image

The suggested fix above solves the problem:

image

Sample Repository

No response

Pest Version

2.x-dev

PHP Version

8.2.15

Operation System

Linux

Notes

pestphp/pest-plugin version is v2.1.1

michaeldyrynda commented 3 weeks ago

Flagging this one @nunomaduro based on our chat.

I'm less concerned with the type errors, and more about the lack of completion of the proxied object.

expect($user)-> will provide completion for methods on the Pest\Expectation object, but nothing from the $user, which makes it possible to introduce failing tests because typos, for example, will lead to an assertion on null.

CleanShot 2024-09-11 at 10 17 31

We certainly get the @template annotations for the generics, etc., but to no avail.

CleanShot 2024-09-11 at 10 18 30

I've tried making the change as in the linked PR (#1168), but that doesn't seem to yield the expected outcome.

I'm not sure how exactly to remedy this situation, or if it's possible. Or if this is just something in my setup, though one of my colleagues has suggested he also has no completion in PhpStorm (and seems to make it chug, at that).