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.58k stars 356 forks source link

Feature Request: assertable method returns in architecture tests #1305

Open josephkerkhof opened 1 week ago

josephkerkhof commented 1 week ago

I would love to use Pest's architecture test framework to perform assertions on what methods return.

My personal use case is to assert that my project's Http controller public methods (invokable or otherwise) return objects that inherit from LaravelData.

danielh-official commented 1 day ago

$response = get('route')->assertOk();

$json = $response->json();

# some code to determine that the response json is tied to LaravelData

Correct me if I'm wrong, but response JSON always resolves to array (in PHP), so you can't really tell what object they inherited from.

If you're testing the method directly, then toBeInstanceOf could help you with determining the inheritance of returned object.

https://pestphp.com/docs/expectations#expect-toBeInstanceOf