Skip to content

Commit

Permalink
Return empty flash array if flash data is not set into the session
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanvyas22 committed Oct 25, 2020
1 parent c21beb8 commit a75f9ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/Controller/InertiaResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ protected function setFlashData()
$session = $this->getRequest()->getSession();

$this->set('flash', function () use ($session) {
if (! $session->check('Flash.flash.0')) {
return [];
}

$flash = $session->read('Flash.flash.0');

$flash['element'] = strtolower(str_replace('/', '-', $flash['element']));
Expand Down
20 changes: 8 additions & 12 deletions tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,10 @@ public function testItSetsSuccessFlashDataIntoProps()
$this->assertResponseOk();
$this->assertArrayHasKey('flash', $responseArray['props']);
$this->assertEquals([
[
'message' => 'User saved successfully.',
'key' => 'flash',
'element' => 'Flash/success',
'params' => [],
],
'message' => 'User saved successfully.',
'key' => 'flash',
'element' => 'flash-success',
'params' => [],
], $responseArray['props']['flash']);
}

Expand All @@ -156,12 +154,10 @@ public function testItSetsErrorFlashDataIntoProps()
$this->assertResponseOk();
$this->assertArrayHasKey('flash', $responseArray['props']);
$this->assertEquals([
[
'message' => 'Something went wrong!',
'key' => 'flash',
'element' => 'Flash/error',
'params' => [],
],
'message' => 'Something went wrong!',
'key' => 'flash',
'element' => 'flash-error',
'params' => [],
], $responseArray['props']['flash']);
}
}

0 comments on commit a75f9ef

Please sign in to comment.