Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
86 / 86 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use AppDemo\Shared\Application\Service\SessionService; |
| 11 | use AppDemo\User\Domain\Email; |
| 12 | use Odan\Session\MemorySession; |
| 13 | use Phexium\Domain\Id\IdInterface; |
| 14 | use Phexium\Plugin\IdGenerator\Adapter\TimestampIdGenerator; |
| 15 | use Phexium\Plugin\Session\Adapter\OdanSession; |
| 16 | use Phexium\Plugin\Session\Port\FlashInterface; |
| 17 | use Tests\Phexium\Fake\Plugin\Logger\Logger as FakeLogger; |
| 18 | |
| 19 | beforeEach(function (): void { |
| 20 | $this->session = new OdanSession(new MemorySession()); |
| 21 | $this->session->start(); |
| 22 | |
| 23 | $this->logger = new FakeLogger(); |
| 24 | |
| 25 | $this->sessionService = new SessionService( |
| 26 | $this->session, |
| 27 | $this->logger, |
| 28 | new TimestampIdGenerator() |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | test('Get session returns the session object', function (): void { |
| 33 | expect($this->sessionService->getSession())->toBe($this->session); |
| 34 | }); |
| 35 | |
| 36 | test('clearUserSession() clears the session', function (): void { |
| 37 | $this->session->set('key', 'value'); |
| 38 | |
| 39 | expect($this->session->all())->not->toBeEmpty(); |
| 40 | |
| 41 | $this->sessionService->clearUserSession(); |
| 42 | |
| 43 | expect($this->session->all())->toBeEmpty() |
| 44 | ->and($this->logger->hasLogContaining('info', 'User session cleared'))->toBeTrue() |
| 45 | ; |
| 46 | }); |
| 47 | |
| 48 | test('regenerateSession() regenerates the session ID', function (): void { |
| 49 | $oldSessionId = $this->session->getId(); |
| 50 | $this->sessionService->regenerateSession(); |
| 51 | $newSessionId = $this->session->getId(); |
| 52 | |
| 53 | expect($newSessionId)->not->toBe($oldSessionId) |
| 54 | ->and($this->logger->hasLogContaining('info', 'Session regeneration requested'))->toBeTrue() |
| 55 | ; |
| 56 | }); |
| 57 | |
| 58 | test('setUserAuthenticated() sets user data in session', function (): void { |
| 59 | $idGenerator = new TimestampIdGenerator(); |
| 60 | $userId = $idGenerator->from(123); |
| 61 | $email = Email::fromString('test@example.com'); |
| 62 | |
| 63 | $this->sessionService->setUserAuthenticated($userId, $email); |
| 64 | |
| 65 | expect($this->session->get('user_id'))->toBe(123) |
| 66 | ->and($this->session->get('user_email'))->toBe('test@example.com') |
| 67 | ; |
| 68 | |
| 69 | expect($this->logger->hasLogWithContext('info', 'User authenticated in session', [ |
| 70 | 'userId' => 123, |
| 71 | 'email' => 'test@example.com', |
| 72 | ]))->toBeTrue(); |
| 73 | }); |
| 74 | |
| 75 | test('isUserAuthenticated() returns true when user ID is in session', function (): void { |
| 76 | $this->session->set('user_id', 123); |
| 77 | |
| 78 | expect($this->sessionService->isUserAuthenticated())->toBeTrue(); |
| 79 | }); |
| 80 | |
| 81 | test('isUserAuthenticated() returns false when user ID is not in session', function (): void { |
| 82 | expect($this->sessionService->isUserAuthenticated())->toBeFalse(); |
| 83 | }); |
| 84 | |
| 85 | test('getUserId() returns the user ID when it exists in session', function (): void { |
| 86 | $userId = 123; |
| 87 | $this->session->set('user_id', $userId); |
| 88 | |
| 89 | $result = $this->sessionService->getUserId(); |
| 90 | |
| 91 | expect($result)->toBeInstanceOf(IdInterface::class) |
| 92 | ->and($result->getValue())->toBe($userId) |
| 93 | ; |
| 94 | }); |
| 95 | |
| 96 | test('getUserId() returns null when it does not exist in session', function (): void { |
| 97 | expect($this->sessionService->getUserId())->toBeNull(); |
| 98 | }); |
| 99 | |
| 100 | test('getUserEmail() returns the user email when it exists in session', function (): void { |
| 101 | $email = 'user@example.com'; |
| 102 | $this->session->set('user_email', $email); |
| 103 | |
| 104 | $result = $this->sessionService->getUserEmail(); |
| 105 | |
| 106 | expect($result)->toBeInstanceOf(Email::class) |
| 107 | ->and($result->getValue())->toBe($email) |
| 108 | ; |
| 109 | }); |
| 110 | |
| 111 | test('getUserEmail() returns null when it does not exist in session', function (): void { |
| 112 | expect($this->sessionService->getUserEmail())->toBeNull(); |
| 113 | }); |
| 114 | |
| 115 | test('addFlashMessage() adds a flash message to the session', function (): void { |
| 116 | $flash = $this->session->getFlash(); |
| 117 | $messages = $flash->all(); |
| 118 | expect($messages)->toBeEmpty(); |
| 119 | |
| 120 | $this->sessionService->addFlashMessage('success', 'Operation successful'); |
| 121 | |
| 122 | $flash = $this->session->getFlash(); |
| 123 | $messages = $flash->get('success'); |
| 124 | expect($messages)->toBeArray() |
| 125 | ->and($messages)->toContain('Operation successful') |
| 126 | ; |
| 127 | }); |
| 128 | |
| 129 | test('getFlash() returns the FlashInterface from the session', function (): void { |
| 130 | $flash = $this->sessionService->getFlash(); |
| 131 | |
| 132 | expect($flash)->toBeInstanceOf(FlashInterface::class); |
| 133 | }); |