Skip to content

Commit 5b1b8c7

Browse files
Add tests DatabaseSessionHandler (#55485)
1 parent 2abde79 commit 5b1b8c7

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
use Orchestra\Testbench\Attributes\WithMigration;
9+
10+
#[WithMigration('session')]
11+
class DatabaseSessionHandlerTest extends DatabaseTestCase
12+
{
13+
public function test_basic_read_write_functionality()
14+
{
15+
$connection = $this->app['db']->connection();
16+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1);
17+
$handler->setContainer($this->app);
18+
19+
// read non-existing session id:
20+
$this->assertEquals('', $handler->read('invalid_session_id'));
21+
22+
// open and close:
23+
$this->assertTrue($handler->open('', ''));
24+
$this->assertTrue($handler->close());
25+
26+
// write and read:
27+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['foo' => 'bar'])));
28+
$this->assertEquals(['foo' => 'bar'], json_decode($handler->read('valid_session_id_2425'), true));
29+
$this->assertEquals(1, $connection->table('sessions')->count());
30+
31+
$session = $connection->table('sessions')->first();
32+
$this->assertNotNull($session->user_agent);
33+
$this->assertNotNull($session->ip_address);
34+
35+
// re-write and read:
36+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['over' => 'ride'])));
37+
$this->assertEquals(['over' => 'ride'], json_decode($handler->read('valid_session_id_2425'), true));
38+
$this->assertEquals(1, $connection->table('sessions')->count());
39+
40+
// handler object writes only one session id:
41+
$this->assertTrue($handler->write('other_id', 'data'));
42+
$this->assertEquals(1, $connection->table('sessions')->count());
43+
44+
$handler->setExists(false);
45+
$this->assertTrue($handler->write('other_id', 'data'));
46+
$this->assertEquals(2, $connection->table('sessions')->count());
47+
48+
// read expired:
49+
Carbon::setTestNow(Carbon::now()->addMinutes(2));
50+
$this->assertEquals('', $handler->read('valid_session_id_2425'));
51+
52+
// rewriting an expired session-id, makes it live:
53+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['come' => 'alive'])));
54+
$this->assertEquals(['come' => 'alive'], json_decode($handler->read('valid_session_id_2425'), true));
55+
}
56+
57+
public function test_garbage_collector()
58+
{
59+
$connection = $this->app['db']->connection();
60+
61+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
62+
$handler->write('simple_id_1', 'abcd');
63+
$this->assertEquals(0, $handler->gc(1));
64+
65+
Carbon::setTestNow(Carbon::now()->addSeconds(2));
66+
67+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
68+
$handler->write('simple_id_2', 'abcd');
69+
$this->assertEquals(1, $handler->gc(2));
70+
$this->assertEquals(1, $connection->table('sessions')->count());
71+
72+
Carbon::setTestNow(Carbon::now()->addSeconds(2));
73+
74+
$this->assertEquals(1, $handler->gc(1));
75+
$this->assertEquals(0, $connection->table('sessions')->count());
76+
}
77+
78+
public function test_destroy()
79+
{
80+
$connection = $this->app['db']->connection();
81+
$handler1 = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
82+
$handler2 = clone $handler1;
83+
84+
$handler1->write('id_1', 'some data');
85+
$handler2->write('id_2', 'some data');
86+
87+
// destroy invalid session-id:
88+
$this->assertEquals(true, $handler1->destroy('invalid_session_id'));
89+
// nothing deleted:
90+
$this->assertEquals(2, $connection->table('sessions')->count());
91+
92+
// destroy valid session-id:
93+
$this->assertEquals(true, $handler2->destroy('id_1'));
94+
// only one row is deleted:
95+
$this->assertEquals(1, $connection->table('sessions')->where('id', 'id_2')->count());
96+
}
97+
98+
public function test_it_can_work_without_container()
99+
{
100+
$connection = $this->app['db']->connection();
101+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1);
102+
103+
// write and read:
104+
$this->assertTrue($handler->write('session_id', 'some data'));
105+
$this->assertEquals('some data', $handler->read('session_id'));
106+
$this->assertEquals(1, $connection->table('sessions')->count());
107+
108+
$session = $connection->table('sessions')->first();
109+
$this->assertNull($session->user_agent);
110+
$this->assertNull($session->ip_address);
111+
$this->assertNull($session->user_id);
112+
}
113+
}

0 commit comments

Comments
 (0)