Skip to content

Commit 97f228c

Browse files
committed
Add assertInertiaFlash and assertInertiaFlashMissing helpers
1 parent b9aac60 commit 97f228c

3 files changed

Lines changed: 135 additions & 10 deletions

File tree

src/Testing/AssertableInertia.php

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -232,34 +232,56 @@ public function reloadExcept(array|string $except, ?Closure $callback = null): s
232232
* Assert that the Flash Data contains the given key, optionally with the expected value.
233233
*/
234234
public function hasFlash(string $key, mixed $expected = null): self
235+
{
236+
func_num_args() > 1
237+
? static::assertFlashHas($this->flash, $key, $expected)
238+
: static::assertFlashHas($this->flash, $key);
239+
240+
return $this;
241+
}
242+
243+
/**
244+
* Assert that the Flash Data does not contain the given key.
245+
*/
246+
public function missingFlash(string $key): self
247+
{
248+
static::assertFlashMissing($this->flash, $key);
249+
250+
return $this;
251+
}
252+
253+
/**
254+
* Assert that the given flash data array contains the given key, optionally with the expected value.
255+
*
256+
* @param array<string, mixed> $flash
257+
*/
258+
public static function assertFlashHas(array $flash, string $key, mixed $expected = null): void
235259
{
236260
PHPUnit::assertTrue(
237-
Arr::has($this->flash, $key),
261+
Arr::has($flash, $key),
238262
sprintf('Inertia Flash Data is missing key [%s].', $key)
239263
);
240264

241-
if (func_num_args() > 1) {
265+
if (func_num_args() > 2) {
242266
PHPUnit::assertSame(
243267
$expected,
244-
Arr::get($this->flash, $key),
268+
Arr::get($flash, $key),
245269
sprintf('Inertia Flash Data [%s] does not match expected value.', $key)
246270
);
247271
}
248-
249-
return $this;
250272
}
251273

252274
/**
253-
* Assert that the Flash Data does not contain the given key.
275+
* Assert that the given flash data array does not contain the given key.
276+
*
277+
* @param array<string, mixed> $flash
254278
*/
255-
public function missingFlash(string $key): self
279+
public static function assertFlashMissing(array $flash, string $key): void
256280
{
257281
PHPUnit::assertFalse(
258-
Arr::has($this->flash, $key),
282+
Arr::has($flash, $key),
259283
sprintf('Inertia Flash Data has unexpected key [%s].', $key)
260284
);
261-
262-
return $this;
263285
}
264286

265287
/**

src/Testing/TestResponseMacros.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Support\Arr;
7+
use Inertia\SessionKey;
78

89
class TestResponseMacros
910
{
@@ -55,4 +56,40 @@ public function inertiaProps()
5556
return Arr::get($page['props'], $propName);
5657
};
5758
}
59+
60+
/**
61+
* Register the 'assertInertiaFlash' macro for TestResponse.
62+
*
63+
* @return Closure
64+
*/
65+
public function assertInertiaFlash()
66+
{
67+
return function (string $key, mixed $expected = null) {
68+
/** @phpstan-ignore-next-line */
69+
$flash = $this->session()->get(SessionKey::FlashData->value, []);
70+
71+
func_num_args() > 1
72+
? AssertableInertia::assertFlashHas($flash, $key, $expected)
73+
: AssertableInertia::assertFlashHas($flash, $key);
74+
75+
return $this;
76+
};
77+
}
78+
79+
/**
80+
* Register the 'assertInertiaFlashMissing' macro for TestResponse.
81+
*
82+
* @return Closure
83+
*/
84+
public function assertInertiaFlashMissing()
85+
{
86+
return function (string $key) {
87+
/** @phpstan-ignore-next-line */
88+
$flash = $this->session()->get(SessionKey::FlashData->value, []);
89+
90+
AssertableInertia::assertFlashMissing($flash, $key);
91+
92+
return $this;
93+
};
94+
}
5895
}

tests/Testing/TestResponseMacrosTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Inertia\Tests\Testing;
44

5+
use Illuminate\Session\Middleware\StartSession;
6+
use Illuminate\Support\Facades\Route;
57
use Illuminate\Testing\Fluent\AssertableJson;
68
use Illuminate\Testing\TestResponse;
79
use Inertia\Inertia;
10+
use Inertia\Middleware;
811
use Inertia\Tests\TestCase;
12+
use PHPUnit\Framework\AssertionFailedError;
913

1014
class TestResponseMacrosTest extends TestCase
1115
{
@@ -77,4 +81,66 @@ public function test_it_can_retrieve_nested_inertia_prop_values_with_dot_notatio
7781
$this->assertSame('qux', $response->inertiaProps('bar.baz'));
7882
$this->assertSame('John', $response->inertiaProps('users.0.name'));
7983
}
84+
85+
public function test_it_can_assert_flash_data_on_redirect_responses(): void
86+
{
87+
$middleware = [StartSession::class, Middleware::class];
88+
89+
Route::middleware($middleware)->post('/users', function () {
90+
return Inertia::flash([
91+
'message' => 'User created!',
92+
'notification' => ['type' => 'success'],
93+
])->back();
94+
});
95+
96+
$this->post('/users')
97+
->assertRedirect()
98+
->assertInertiaFlash('message')
99+
->assertInertiaFlash('message', 'User created!')
100+
->assertInertiaFlash('notification.type', 'success')
101+
->assertInertiaFlashMissing('error')
102+
->assertInertiaFlashMissing('notification.other');
103+
}
104+
105+
public function test_assert_has_inertia_flash_fails_when_key_is_missing(): void
106+
{
107+
$middleware = [StartSession::class, Middleware::class];
108+
109+
Route::middleware($middleware)->post('/users', function () {
110+
return Inertia::flash('message', 'Hello')->back();
111+
});
112+
113+
$this->expectException(AssertionFailedError::class);
114+
$this->expectExceptionMessage('Inertia Flash Data is missing key [other].');
115+
116+
$this->post('/users')->assertInertiaFlash('other');
117+
}
118+
119+
public function test_assert_has_inertia_flash_fails_when_value_does_not_match(): void
120+
{
121+
$middleware = [StartSession::class, Middleware::class];
122+
123+
Route::middleware($middleware)->post('/users', function () {
124+
return Inertia::flash('message', 'Hello')->back();
125+
});
126+
127+
$this->expectException(AssertionFailedError::class);
128+
$this->expectExceptionMessage('Inertia Flash Data [message] does not match expected value.');
129+
130+
$this->post('/users')->assertInertiaFlash('message', 'Different');
131+
}
132+
133+
public function test_assert_missing_inertia_flash_fails_when_key_exists(): void
134+
{
135+
$middleware = [StartSession::class, Middleware::class];
136+
137+
Route::middleware($middleware)->post('/users', function () {
138+
return Inertia::flash('message', 'Hello')->back();
139+
});
140+
141+
$this->expectException(AssertionFailedError::class);
142+
$this->expectExceptionMessage('Inertia Flash Data has unexpected key [message].');
143+
144+
$this->post('/users')->assertInertiaFlashMissing('message');
145+
}
80146
}

0 commit comments

Comments
 (0)