Skip to content

Commit 1e8f837

Browse files
committed
Add tests DatabaseSessionHandler
1 parent c01bb8d commit 1e8f837

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Session;
4+
5+
use Illuminate\Session\DatabaseSessionHandler;
6+
use Illuminate\Support\Carbon;
7+
use Illuminate\Tests\Integration\Database\DatabaseTestCase;
8+
9+
class DatabaseSessionHandlerTest extends DatabaseTestCase
10+
{
11+
private mixed $migration;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
$this->migration = require __DIR__.'/../../../src/Illuminate/Session/Console/stubs/database.stub';
17+
$this->migration->up();
18+
}
19+
20+
public function tearDown(): void
21+
{
22+
$this->migration->down();
23+
parent::tearDown();
24+
}
25+
26+
public function testBasicReadWriteFunctionality()
27+
{
28+
$connection = $this->app['db']->connection();
29+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1);
30+
$handler->setContainer($this->app);
31+
32+
// read non-existing session id:
33+
$this->assertEquals('', $handler->read('invalid_session_id'));
34+
35+
// open and close:
36+
$this->assertTrue($handler->open('', ''));
37+
$this->assertTrue($handler->close());
38+
39+
// write and read:
40+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['foo' => 'bar'])));
41+
$this->assertEquals(['foo' => 'bar'], json_decode($handler->read('valid_session_id_2425'), true));
42+
$this->assertEquals(1, $connection->table('sessions')->count());
43+
44+
$session = $connection->table('sessions')->first();
45+
$this->assertNotNull($session->user_agent);
46+
$this->assertNotNull($session->ip_address);
47+
48+
// re-write and read:
49+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['over' => 'ride'])));
50+
$this->assertEquals(['over' => 'ride'], json_decode($handler->read('valid_session_id_2425'), true));
51+
$this->assertEquals(1, $connection->table('sessions')->count());
52+
53+
// handler object writes only one session id:
54+
$this->assertTrue($handler->write('other_id', 'data'));
55+
$this->assertEquals(1, $connection->table('sessions')->count());
56+
57+
$handler->setExists(false);
58+
$this->assertTrue($handler->write('other_id', 'data'));
59+
$this->assertEquals(2, $connection->table('sessions')->count());
60+
61+
// read expired:
62+
Carbon::setTestNow(Carbon::now()->addMinutes(2));
63+
$this->assertEquals('', $handler->read('valid_session_id_2425'));
64+
65+
// rewriting an expired session-id, makes it live:
66+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['come' => 'alive'])));
67+
$this->assertEquals(['come' => 'alive'], json_decode($handler->read('valid_session_id_2425'), true));
68+
}
69+
70+
public function testGarbageCollector()
71+
{
72+
$connection = $this->app['db']->connection();
73+
74+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
75+
$handler->write('simple_id_1', 'abcd');
76+
$this->assertEquals(0, $handler->gc(1));
77+
78+
Carbon::setTestNow(Carbon::now()->addSeconds(2));
79+
80+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
81+
$handler->write('simple_id_2', 'abcd');
82+
$this->assertEquals(2, $connection->table('sessions')->count());
83+
$this->assertEquals(1, $handler->gc(2));
84+
}
85+
86+
public function testDestroy()
87+
{
88+
$connection = $this->app['db']->connection();
89+
$handler1 = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
90+
$handler2 = clone $handler1;
91+
92+
$handler1->write('id_1', 'some data');
93+
$handler2->write('id_2', 'some data');
94+
95+
// destroy invalid session-id:
96+
$this->assertEquals(true, $handler1->destroy('invalid_session_id'));
97+
$this->assertEquals(2, $connection->table('sessions')->count());
98+
99+
// destroy valid session-id:
100+
$this->assertEquals(true, $handler2->destroy('id_1'));
101+
$this->assertEquals(1, $connection->table('sessions')->count());
102+
}
103+
104+
public function testItCanWorkWithoutContainer()
105+
{
106+
$connection = $this->app['db']->connection();
107+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1);
108+
109+
// write and read:
110+
$this->assertTrue($handler->write('session_id', 'some data'));
111+
$this->assertEquals('some data', $handler->read('session_id'));
112+
$this->assertEquals(1, $connection->table('sessions')->count());
113+
114+
$session = $connection->table('sessions')->first();
115+
$this->assertNull($session->user_agent);
116+
$this->assertNull($session->ip_address);
117+
$this->assertNull($session->user_id);
118+
}
119+
}

0 commit comments

Comments
 (0)