Skip to content

Commit 92866aa

Browse files
committed
Add tests DatabaseSessionHandler
1 parent c01bb8d commit 92866aa

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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, $this->app);
30+
// read non-existing session id:
31+
$this->assertEquals('', $handler->read('invalid_session_id'));
32+
// open and close:
33+
$this->assertTrue($handler->open('', ''));
34+
$this->assertTrue($handler->close());
35+
36+
// write and read:
37+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['foo' => 'bar'])));
38+
$this->assertEquals(['foo' => 'bar'], json_decode($handler->read('valid_session_id_2425'), true));
39+
$this->assertEquals(1, $connection->table('sessions')->count());
40+
41+
// re-write and read:
42+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['over' => 'ride'])));
43+
$this->assertEquals(['over' => 'ride'], json_decode($handler->read('valid_session_id_2425'), true));
44+
$this->assertEquals(1, $connection->table('sessions')->count());
45+
46+
// handler object writes only one session id:
47+
$this->assertTrue($handler->write('other_id', 'data'));
48+
$this->assertEquals(1, $connection->table('sessions')->count());
49+
50+
// read expired:
51+
Carbon::setTestNow(Carbon::now()->addMinutes(2));
52+
$this->assertEquals('', $handler->read('valid_session_id_2425'));
53+
54+
// rewriting an expired session-id, makes it live:
55+
$this->assertTrue($handler->write('valid_session_id_2425', json_encode(['come' => 'alive'])));
56+
$this->assertEquals(['come' => 'alive'], json_decode($handler->read('valid_session_id_2425'), true));
57+
}
58+
59+
public function testGarbageCollector()
60+
{
61+
$connection = $this->app['db']->connection();
62+
63+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
64+
$handler->write('simple_id_1', 'abcd');
65+
$this->assertEquals(0, $handler->gc(1));
66+
67+
Carbon::setTestNow(Carbon::now()->addSeconds(2));
68+
69+
$handler = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
70+
$handler->write('simple_id_2', 'abcd');
71+
$this->assertEquals(2, $connection->table('sessions')->count());
72+
$this->assertEquals(1, $handler->gc(2));
73+
}
74+
75+
public function testDestroy()
76+
{
77+
$connection = $this->app['db']->connection();
78+
$handler1 = new DatabaseSessionHandler($connection, 'sessions', 1, $this->app);
79+
$handler2 = clone $handler1;
80+
81+
$handler1->write('id_1', 'some data');
82+
$handler2->write('id_2', 'some data');
83+
84+
// destroy invalid session-id:
85+
$this->assertEquals(true, $handler1->destroy('invalid_session_id'));
86+
$this->assertEquals(2, $connection->table('sessions')->count());
87+
// destroy valid session-id:
88+
$this->assertEquals(true, $handler2->destroy('id_1'));
89+
$this->assertEquals(1, $connection->table('sessions')->count());
90+
}
91+
}

0 commit comments

Comments
 (0)