|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Quantum\Tests\Unit\Session\Adapters\Database; |
| 4 | + |
| 5 | +use Quantum\Session\Adapters\Database\DatabaseHandler; |
| 6 | +use Quantum\Database\Adapters\Idiorm\IdiormDbal; |
| 7 | +use Quantum\Tests\Unit\Session\TestCaseHelper; |
| 8 | +use Quantum\Database\Contracts\DbalInterface; |
| 9 | +use Quantum\Model\Factories\ModelFactory; |
| 10 | +use Quantum\Tests\Unit\AppTestCase; |
| 11 | +use ReflectionMethod; |
| 12 | +use Mockery; |
| 13 | + |
| 14 | +class DatabaseHandlerTest extends AppTestCase |
| 15 | +{ |
| 16 | + use TestCaseHelper; |
| 17 | + |
| 18 | + public function setUp(): void |
| 19 | + { |
| 20 | + parent::setUp(); |
| 21 | + |
| 22 | + IdiormDbal::connect(['driver' => 'sqlite', 'database' => ':memory:']); |
| 23 | + |
| 24 | + $this->_createSessionsTable(); |
| 25 | + } |
| 26 | + |
| 27 | + public function tearDown(): void |
| 28 | + { |
| 29 | + ModelFactory::createDynamicModel('sessions')->deleteMany(); |
| 30 | + |
| 31 | + parent::tearDown(); |
| 32 | + } |
| 33 | + |
| 34 | + public function testDatabaseHandlerGcDeletesTheExpiredSessionsAndReturnsTheirNumber(): void |
| 35 | + { |
| 36 | + $this->insertSession('expired-1', time() - 7200); |
| 37 | + $this->insertSession('expired-2', time() - 5400); |
| 38 | + $this->insertSession('active', time()); |
| 39 | + |
| 40 | + $this->assertSame(2, $this->createHandler()->gc(3600)); |
| 41 | + |
| 42 | + $this->assertSame([['session_id' => 'active']], IdiormDbal::query('SELECT session_id FROM sessions')); |
| 43 | + } |
| 44 | + |
| 45 | + public function testDatabaseHandlerGcReturnsZeroWhenNoSessionHasExpired(): void |
| 46 | + { |
| 47 | + $this->insertSession('active', time()); |
| 48 | + |
| 49 | + $this->assertSame(0, $this->createHandler()->gc(3600)); |
| 50 | + |
| 51 | + $this->assertSame([['session_id' => 'active']], IdiormDbal::query('SELECT session_id FROM sessions')); |
| 52 | + } |
| 53 | + |
| 54 | + public function testDatabaseHandlerGcReturnsFalseWhenTheSessionsCannotBeDeleted(): void |
| 55 | + { |
| 56 | + $model = Mockery::mock(DbalInterface::class); |
| 57 | + $model->shouldReceive('criteria')->with('ttl', '<', Mockery::type('int'))->andReturnSelf(); |
| 58 | + $model->shouldReceive('count')->andReturn(2); |
| 59 | + $model->shouldReceive('deleteMany')->andReturn(false); |
| 60 | + |
| 61 | + $this->assertFalse((new DatabaseHandler($model))->gc(3600)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @dataProvider interfaceMethodProvider |
| 66 | + */ |
| 67 | + public function testDatabaseHandlerDeclaresTheReturnTypeOfItsInterfaceMethods(string $method): void |
| 68 | + { |
| 69 | + $this->assertTrue((new ReflectionMethod(DatabaseHandler::class, $method))->hasReturnType()); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * A handler serves one session, so every check gets a handler of its own |
| 74 | + */ |
| 75 | + private function createHandler(): DatabaseHandler |
| 76 | + { |
| 77 | + return new DatabaseHandler(ModelFactory::createDynamicModel('sessions')->getOrmInstance()); |
| 78 | + } |
| 79 | + |
| 80 | + private function insertSession(string $id, int $ttl): void |
| 81 | + { |
| 82 | + IdiormDbal::execute('INSERT INTO sessions (session_id, data, ttl) VALUES (?, ?, ?)', [$id, 'data', $ttl]); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array<string, array{0: string}> |
| 87 | + */ |
| 88 | + public function interfaceMethodProvider(): array |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'open' => ['open'], |
| 92 | + 'close' => ['close'], |
| 93 | + 'read' => ['read'], |
| 94 | + 'write' => ['write'], |
| 95 | + 'destroy' => ['destroy'], |
| 96 | + 'gc' => ['gc'], |
| 97 | + ]; |
| 98 | + } |
| 99 | +} |
0 commit comments