. */ namespace SP\Tests\Core\Context; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\RunClassInSeparateProcess; use SP\Core\Context\SessionLifecycleHandler; use SP\Domain\Core\Exceptions\SPException; use SP\Tests\UnitaryTestCase; /** * Class SessionUtilTest */ #[Group('unitary')] #[RunClassInSeparateProcess] class SessionLifecycleHandlerTest extends UnitaryTestCase { /** * @throws SPException */ public function testCleanSession() { session_start(); $_SESSION['test'] = self::$faker->colorName; SessionLifecycleHandler::clean(); $this->assertArrayNotHasKey('test', $_SESSION); } /** * @throws SPException */ public function testStart() { $this->assertEquals(PHP_SESSION_NONE, session_status()); SessionLifecycleHandler::start(); $this->assertEquals(PHP_SESSION_ACTIVE, session_status()); $this->assertEquals('1', ini_get('session.use_strict_mode')); } /** * @throws SPException */ public function testStartWithHeadersSent() { $this->assertEquals(PHP_SESSION_NONE, session_status()); echo "Test"; ob_flush(); $this->expectException(SPException::class); $this->expectExceptionMessage('Session cannot be initialized'); SessionLifecycleHandler::start(); } /** * @throws SPException */ public function testStartWithGarbageSessionWithDestroyTimeout() { session_start(); $_SESSION['destroy_time'] = time() - 600; SessionLifecycleHandler::start(); $this->assertEquals(PHP_SESSION_ACTIVE, session_status()); $this->assertArrayNotHasKey('destroy_time', $_SESSION); } /** * @throws SPException */ public function testStartWithGarbageSessionAndNewId() { session_start(); $newId = session_create_id('test-'); $_SESSION['destroy_time'] = time(); $_SESSION['new_session_id'] = $newId; SessionLifecycleHandler::start(); $this->assertEquals(PHP_SESSION_ACTIVE, session_status()); $this->assertEquals($newId, session_id()); } /** * @throws SPException */ public function testRegenerate() { $this->assertEquals(PHP_SESSION_NONE, session_status()); $oldId = session_id(); $out = SessionLifecycleHandler::regenerate(); $this->assertEquals(PHP_SESSION_ACTIVE, session_status()); $this->assertNotEquals($oldId, $out); $this->assertEquals($out, session_id()); } /** * @throws SPException */ public function testRestart() { $this->assertEquals(PHP_SESSION_NONE, session_status()); session_start(); $_SESSION['test'] = self::$faker->colorName; SessionLifecycleHandler::restart(); $this->assertEquals(PHP_SESSION_ACTIVE, session_status()); $this->assertArrayNotHasKey('test', $_SESSION); } /** * @throws SPException */ public function testClean() { $this->assertEquals(PHP_SESSION_NONE, session_status()); session_start(); $_SESSION['test'] = self::$faker->colorName; SessionLifecycleHandler::clean(); $this->assertEquals(PHP_SESSION_NONE, session_status()); $this->assertArrayNotHasKey('test', $_SESSION); } public function testNeedsRegenerate() { $this->assertFalse(SessionLifecycleHandler::needsRegenerate(time())); } public function testNeedsRegenerateWithTimeout() { $this->assertTrue(SessionLifecycleHandler::needsRegenerate(time() - 2000)); } protected function tearDown(): void { parent::tearDown(); session_destroy(); } }