2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:21:17 +01:00
|
|
|
namespace Tests\Integration\WithApp\App\Services;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Services\FlashMessageService;
|
2026-11-16 15:04:17 +01:00
|
|
|
use Illuminate\Session\ArraySessionHandler;
|
|
|
|
|
use Illuminate\Session\Store;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
|
|
|
|
|
covers(FlashMessageService::class);
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('FlashMessageService', function (): void {
|
|
|
|
|
beforeEach(function (): void {
|
|
|
|
|
Session::swap(new Store('test', new ArraySessionHandler(120)));
|
|
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('add()', function (): void {
|
|
|
|
|
it('adds a message without erasing previous', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Session::put('info', ['First message']);
|
2026-06-14 21:32:07 +02:00
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->add('info', 'Second message');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('info'))->toBe(['First message', 'Second message']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
it('adds a message when existing is not an array', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
2026-11-16 15:04:17 +01:00
|
|
|
Session::put('info', 'First message');
|
2026-06-14 21:32:07 +02:00
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->add('info', 'Second message');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('info'))->toBe(['First message', 'Second message']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('info()', function (): void {
|
|
|
|
|
it('adds an info message', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->info('Info msg');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('info'))->toBe(['Info msg']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('success()', function (): void {
|
|
|
|
|
it('adds a success message', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->success('Success msg');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('success'))->toBe(['Success msg']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('error()', function (): void {
|
|
|
|
|
it('adds an error message', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->error('Error msg');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('error'))->toBe(['Error msg']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:04:17 +01:00
|
|
|
describe('warning()', function (): void {
|
|
|
|
|
it('adds a warning message', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$service = new FlashMessageService();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$service->warning('Warning msg');
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:04:17 +01:00
|
|
|
expect(Session::get('warning'))->toBe(['Warning msg']);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|