Bicycle/tests/Unit/SessionTest.php
Egor Isaev b516ca07dc Initial commit: Bicycle PHP MVC micro-framework
Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:19 +03:00

98 lines
2.7 KiB
PHP

<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
use System\Classes\Session;
class SessionTest extends TestCase
{
private Session $session;
protected function setUp(): void
{
// Используем реальную сессию в CLI — заголовки не отправляются
$this->session = Session::instance('test');
}
protected function tearDown(): void
{
$this->session->destroy();
// Сбрасываем синглтон через рефлексию
$ref = new \ReflectionClass(Session::class);
$prop = $ref->getProperty('instances');
$prop->setAccessible(true);
$prop->setValue(null, []);
}
public function testSetAndGet(): void
{
$this->session->set('foo', 'bar');
$this->assertSame('bar', $this->session->get('foo'));
}
public function testGetDefault(): void
{
$this->assertNull($this->session->get('nonexistent'));
$this->assertSame('default', $this->session->get('nonexistent', 'default'));
}
public function testDelete(): void
{
$this->session->set('key', 'value');
$this->session->delete('key');
$this->assertNull($this->session->get('key'));
}
public function testSetReturnsThis(): void
{
$result = $this->session->set('x', 1);
$this->assertSame($this->session, $result);
}
public function testDeleteReturnsThis(): void
{
$result = $this->session->delete('x');
$this->assertSame($this->session, $result);
}
public function testGetDataReturnsArray(): void
{
$this->session->set('a', 1)->set('b', 2);
$data = $this->session->getData();
$this->assertIsArray($data);
$this->assertSame(1, $data['a']);
$this->assertSame(2, $data['b']);
}
public function testIdIsString(): void
{
$this->assertIsString($this->session->id());
$this->assertNotEmpty($this->session->id());
}
public function testInstanceReturnsSameSingleton(): void
{
$a = Session::instance('test');
$b = Session::instance('test');
$this->assertSame($a, $b);
}
public function testBindByReference(): void
{
$value = 'initial';
$this->session->bind('ref', $value);
$value = 'changed';
$this->assertSame('changed', $this->session->get('ref'));
}
public function testDestroyReturnsTrueAndClearsData(): void
{
$this->session->set('key', 'val');
$result = $this->session->destroy();
$this->assertTrue($result);
$this->assertEmpty($this->session->getData());
}
}