Skip to content

Commit bcb5b3e

Browse files
committed
Add support for Responsable (JSON Resources)
See 2b966ac
1 parent d2efb19 commit bcb5b3e

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Assertions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Arr;
77
use PHPUnit\Framework\Assert as PHPUnit;
88
use Illuminate\Contracts\Support\Arrayable;
9+
use Illuminate\Contracts\Support\Responsable;
910

1011
class Assertions
1112
{
@@ -58,6 +59,8 @@ public function assertInertiaHas()
5859
PHPUnit::assertTrue($value(Arr::get($this->inertiaProps(), $key)));
5960
} elseif ($value instanceof Arrayable) {
6061
PHPUnit::assertEquals($value->toArray(), Arr::get($this->inertiaProps(), $key));
62+
} elseif ($value instanceof Responsable) {
63+
PHPUnit::assertEquals($value->toResponse($this)->getData(), Arr::get($this->inertiaProps(), $key));
6164
} else {
6265
PHPUnit::assertEquals($value, Arr::get($this->inertiaProps(), $key));
6366
}

tests/AssertionsTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Auth\User;
77
use Illuminate\Database\Eloquent\Model;
88
use PHPUnit\Framework\AssertionFailedError;
9+
use Illuminate\Http\Resources\Json\JsonResource;
910

1011
class AssertionsTest extends TestCase
1112
{
@@ -278,6 +279,73 @@ public function test_the_inertia_page_does_not_have_a_nested_prop_with_a_value_u
278279
$response->assertInertiaHas('example.nested', $userB);
279280
}
280281

282+
public function test_the_inertia_page_has_a_prop_with_a_value_using_a_responsable()
283+
{
284+
Model::unguard();
285+
$user = User::make(['name' => 'Example']);
286+
$resource = JsonResource::collection([$user, $user]);
287+
288+
$response = $this->makeMockResponse(
289+
Inertia::render('test-component', [
290+
'example' => $resource,
291+
])
292+
);
293+
294+
$response->assertInertiaHas('example', $resource);
295+
}
296+
297+
public function test_the_inertia_page_does_not_have_a_prop_with_a_value_using_a_responsable()
298+
{
299+
Model::unguard();
300+
$resourceA = JsonResource::make(User::make(['name' => 'Another']));
301+
$resourceB = JsonResource::make(User::make(['name' => 'Example']));
302+
303+
$response = $this->makeMockResponse(
304+
Inertia::render('test-component', [
305+
'example' => $resourceA,
306+
])
307+
);
308+
309+
$this->expectException(AssertionFailedError::class);
310+
311+
$response->assertInertiaHas('example', $resourceB);
312+
}
313+
314+
public function test_the_inertia_page_has_a_nested_prop_with_a_value_using_a_responsable()
315+
{
316+
Model::unguard();
317+
$resource = JsonResource::make(User::make(['name' => 'Another']));
318+
319+
$response = $this->makeMockResponse(
320+
Inertia::render('test-component', [
321+
'example' => [
322+
'nested' => $resource,
323+
],
324+
])
325+
);
326+
327+
$response->assertInertiaHas('example.nested', $resource);
328+
}
329+
330+
public function test_the_inertia_page_does_not_have_a_nested_prop_with_a_value_using_a_responsable()
331+
{
332+
Model::unguard();
333+
$resourceA = JsonResource::make(User::make(['name' => 'Another']));
334+
$resourceB = JsonResource::make(User::make(['name' => 'Example']));
335+
336+
$response = $this->makeMockResponse(
337+
Inertia::render('test-component', [
338+
'example' => [
339+
'nested' => $resourceA,
340+
],
341+
])
342+
);
343+
344+
$this->expectException(AssertionFailedError::class);
345+
346+
$response->assertInertiaHas('example.nested', $resourceB);
347+
}
348+
281349
public function test_the_inertia_page_has_all_the_given_props()
282350
{
283351
Model::unguard();

0 commit comments

Comments
 (0)