Files
breadcrumb-the-shire/tests/Unit/Module/SessionProviderTest.php
fs 975651b183 fix: eliminate $_SESSION drift in SessionProvider and add notification UI states
Pass AppContainer to SessionProvider::clear() so all module providers use
SessionStoreInterface consistently instead of raw $_SESSION. Also add
loading and error states to the notification bell dropdown (GR-UI-014).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-20 22:49:03 +01:00

84 lines
2.7 KiB
PHP

<?php
namespace MintyPHP\Tests\Unit\Module;
use MintyPHP\App\AppContainer;
use MintyPHP\Http\SessionStore;
use MintyPHP\Http\SessionStoreInterface;
use MintyPHP\Module\AddressBook\Providers\AddressBookSessionProvider;
use PHPUnit\Framework\TestCase;
/**
* @covers \MintyPHP\Module\AddressBook\Providers\AddressBookSessionProvider
*/
final class SessionProviderTest extends TestCase
{
protected function setUp(): void
{
$_SESSION = [];
}
protected function tearDown(): void
{
$_SESSION = [];
}
private function containerWithSessionStore(): AppContainer
{
$container = new AppContainer();
$container->set(SessionStoreInterface::class, static fn (): SessionStore => new SessionStore());
return $container;
}
public function testPopulateWithInvalidUserClearsSessionKey(): void
{
$_SESSION['module.addressbook.departments_by_tenant'] = [['tenant' => 'old']];
$provider = new AddressBookSessionProvider();
$provider->populate(['id' => 0], $this->containerWithSessionStore());
self::assertArrayNotHasKey('module.addressbook.departments_by_tenant', $_SESSION);
}
public function testPopulateWithMissingUserIdClearsSessionKey(): void
{
$_SESSION['module.addressbook.departments_by_tenant'] = [['tenant' => 'old']];
$provider = new AddressBookSessionProvider();
$provider->populate([], $this->containerWithSessionStore());
self::assertArrayNotHasKey('module.addressbook.departments_by_tenant', $_SESSION);
}
public function testPopulateReturnsEarlyWhenContainerMissesDependencies(): void
{
$container = $this->containerWithSessionStore();
$provider = new AddressBookSessionProvider();
// Container has SessionStore but no UserTenantContextService → provider returns early
$provider->populate(['id' => 42], $container);
self::assertArrayNotHasKey('module.addressbook.departments_by_tenant', $_SESSION);
}
public function testClearRemovesSessionKey(): void
{
$_SESSION['module.addressbook.departments_by_tenant'] = [
['tenant' => ['uuid' => 'abc'], 'departments' => []],
];
$provider = new AddressBookSessionProvider();
$provider->clear($this->containerWithSessionStore());
self::assertArrayNotHasKey('module.addressbook.departments_by_tenant', $_SESSION);
}
public function testClearIsIdempotent(): void
{
// Key doesn't exist — clear() should not throw
$provider = new AddressBookSessionProvider();
$provider->clear($this->containerWithSessionStore());
self::assertArrayNotHasKey('module.addressbook.departments_by_tenant', $_SESSION);
}
}