Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/CP/Toasts/Toast.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,11 +4,12 @@

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable;

/**
* Holds information about a toast message to show to the user.
*/
class Toast implements Arrayable
class Toast implements Arrayable, JsonSerializable
{
private const TYPES = ['error', 'success', 'info'];
private $message;
Expand DownExpand Up@@ -43,6 +44,11 @@ public function toArray(): array
];
}

public function jsonSerialize(): array
{
return $this->toArray();
}

/**
* @throws Exception
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/CP/Toasts/ManagerTest.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\CP\Toasts;

use Illuminate\Session\ArraySessionHandler;
use Illuminate\Session\Store;
use PHPUnit\Framework\Attributes\Test;
use Statamic\CP\Toasts\Manager;
use Tests\TestCase;

class ManagerTest extends TestCase
{
#[Test]
public function toasts_survive_json_session_serialization()
{
$handler = new ArraySessionHandler(120);
$sessionId = str_repeat('a', 40);

// Flash a toast, then persist the session using json serialization,
// like a request ending with a redirect would.
$session = new Store('test', $handler, $sessionId, 'json');
$session->start();
(new Manager($session))->success('Saved!');
$session->save();

// Read it back on the "next request".
$session = new Store('test', $handler, $sessionId, 'json');
$session->start();

$this->assertSame(
[['message' => 'Saved!', 'type' => 'success', 'duration' => null]],
(new Manager($session))->toArray()
);
}
}
Loading