I've made a macro for the Illuminate\Testing\TestResponse
class that I
put into the TestCase.php
file which is
a part of pingcrm-svelte.
I currently use this short macro in basically all HTTP tests for Inertia
related endpoints in Laravel, so unless I am doing something wrong, it can
be considered quite helpful. Take a look:
<?php
use Illuminate\Testing\TestResponse;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Inertia\Testing\Assert as InertiaAssert;
// use PHPUnit\Framework\Assert;
abstract class TestCase extends BaseTestCase {
protected function setUp(): void {
parent::setUp();
TestResponse::macro('assertInertiaComponent', function ($component) {
return $this->assertStatus(200)->assertInertia(function (
InertiaAssert $page,
) use ($component) {
$page->component($component);
});
});
}
}
To use it inside the HTTP test with PHPUnit it can now be employed like this:
<?php
public function test_user_can_see_items() {
$this->actingAs($this->user)
->get('/item')
->assertInertiaComponent('Item/Index');
}
It can definitely be made different or better, but hey, it's a good start for me.