2018-09-16 14:08:09 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 14:08:09 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Http\SessionHandlers;
|
|
|
|
|
|
|
|
use Engelsystem\Http\SessionHandlers\DatabaseHandler;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
2019-10-08 13:57:50 +02:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2018-09-16 14:08:09 +02:00
|
|
|
|
|
|
|
class DatabaseHandlerTest extends TestCase
|
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::__construct
|
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::getQuery
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::read
|
2018-09-16 14:08:09 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testRead(): void
|
2018-09-16 14:08:09 +02:00
|
|
|
{
|
|
|
|
$handler = new DatabaseHandler($this->database);
|
|
|
|
$this->assertEquals('', $handler->read('foo'));
|
|
|
|
|
|
|
|
$this->database->insert("INSERT INTO sessions VALUES ('foo', 'Lorem Ipsum', CURRENT_TIMESTAMP)");
|
|
|
|
$this->assertEquals('Lorem Ipsum', $handler->read('foo'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::getCurrentTimestamp
|
2019-04-24 11:01:37 +02:00
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::write
|
2018-09-16 14:08:09 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testWrite(): void
|
2018-09-16 14:08:09 +02:00
|
|
|
{
|
|
|
|
$handler = new DatabaseHandler($this->database);
|
|
|
|
|
|
|
|
foreach (['Lorem Ipsum', 'Dolor Sit!'] as $data) {
|
|
|
|
$this->assertTrue($handler->write('foo', $data));
|
|
|
|
|
|
|
|
$return = $this->database->select('SELECT * FROM sessions WHERE id = :id', ['id' => 'foo']);
|
|
|
|
$this->assertCount(1, $return);
|
|
|
|
|
|
|
|
$return = array_shift($return);
|
|
|
|
$this->assertEquals($data, $return->payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::destroy
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testDestroy(): void
|
2018-09-16 14:08:09 +02:00
|
|
|
{
|
|
|
|
$this->database->insert("INSERT INTO sessions VALUES ('foo', 'Lorem Ipsum', CURRENT_TIMESTAMP)");
|
|
|
|
$this->database->insert("INSERT INTO sessions VALUES ('bar', 'Dolor Sit', CURRENT_TIMESTAMP)");
|
|
|
|
|
|
|
|
$handler = new DatabaseHandler($this->database);
|
|
|
|
$this->assertTrue($handler->destroy('batz'));
|
|
|
|
|
|
|
|
$return = $this->database->select('SELECT * FROM sessions');
|
|
|
|
$this->assertCount(2, $return);
|
|
|
|
|
|
|
|
$this->assertTrue($handler->destroy('bar'));
|
|
|
|
|
|
|
|
$return = $this->database->select('SELECT * FROM sessions');
|
|
|
|
$this->assertCount(1, $return);
|
|
|
|
|
|
|
|
$return = array_shift($return);
|
|
|
|
$this->assertEquals('foo', $return->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::gc
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testGc(): void
|
2018-09-16 14:08:09 +02:00
|
|
|
{
|
|
|
|
$this->database->insert("INSERT INTO sessions VALUES ('foo', 'Lorem Ipsum', '2000-01-01 01:00')");
|
|
|
|
$this->database->insert("INSERT INTO sessions VALUES ('bar', 'Dolor Sit', '3000-01-01 01:00')");
|
|
|
|
|
|
|
|
$handler = new DatabaseHandler($this->database);
|
|
|
|
|
2022-12-10 20:39:09 +01:00
|
|
|
$this->assertEquals(1, $handler->gc(60 * 60));
|
2018-09-16 14:08:09 +02:00
|
|
|
|
|
|
|
$return = $this->database->select('SELECT * FROM sessions');
|
|
|
|
$this->assertCount(1, $return);
|
|
|
|
|
|
|
|
$return = array_shift($return);
|
|
|
|
$this->assertEquals('bar', $return->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare tests
|
|
|
|
*/
|
2019-04-24 10:45:00 +02:00
|
|
|
protected function setUp(): void
|
2018-09-16 14:08:09 +02:00
|
|
|
{
|
2019-10-08 13:57:50 +02:00
|
|
|
parent::setUp();
|
2018-09-16 14:08:09 +02:00
|
|
|
$this->initDatabase();
|
|
|
|
}
|
|
|
|
}
|