Vytvoril som makro pre triedu Illuminate\Testing\TestResponse, ktoré som
umiestnil do súboru TestCase.php –
je to súčasť pingcrm-svelte.
Toto krátke makro v súčasnosti používam prakticky vo všetkých HTTP testoch
pre Inertia endpointy v Laraveli, takže pokiaľ nerobím niečo zle, možno ho
považovať za celkom užitočné. Pozrite sa na to:
<?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);
});
});
}
}
Na použitie v HTTP teste s PHPUnit ho teraz možno použiť takto:
<?php
public function test_user_can_see_items() {
$this->actingAs($this->user)
->get('/item')
->assertInertiaComponent('Item/Index');
}
Určite by sa to dalo spraviť inak alebo lepšie, ale hej, pre mňa je to dobrý začiatok.