Merge remote-tracking branch 'MyIgel/session'
commit
b46207f911
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Engelsystem\Database\Migration\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class CreateSessionsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migration
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
$this->schema->create('sessions', function (Blueprint $table) {
|
||||
$table->string('id')->unique();
|
||||
$table->text('payload');
|
||||
$table->dateTime('last_activity')->useCurrent();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migration
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$this->schema->dropIfExists('sessions');
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Database;
|
||||
|
||||
use Illuminate\Database\Connection as DatabaseConnection;
|
||||
use PDO;
|
||||
|
||||
class Database
|
||||
{
|
||||
/** @var DatabaseConnection */
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @param DatabaseConnection $connection
|
||||
*/
|
||||
public function __construct(DatabaseConnection $connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a select query
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return object[]
|
||||
*/
|
||||
public function select($query, array $bindings = [])
|
||||
{
|
||||
return $this->connection->select($query, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a select query and return only the first result or null if no result is found.
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return object|null
|
||||
*/
|
||||
public function selectOne($query, array $bindings = [])
|
||||
{
|
||||
return $this->connection->selectOne($query, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run an insert query
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return bool
|
||||
*/
|
||||
public function insert($query, array $bindings = [])
|
||||
{
|
||||
return $this->connection->insert($query, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run an update query
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return int
|
||||
*/
|
||||
public function update($query, array $bindings = [])
|
||||
{
|
||||
return $this->connection->update($query, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a delete query
|
||||
*
|
||||
* @param string $query
|
||||
* @param array $bindings
|
||||
* @return int
|
||||
*/
|
||||
public function delete($query, array $bindings = [])
|
||||
{
|
||||
return $this->connection->delete($query, $bindings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PDO instance
|
||||
*
|
||||
* @return PDO
|
||||
*/
|
||||
public function getPdo()
|
||||
{
|
||||
return $this->connection->getPdo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DatabaseConnection
|
||||
*/
|
||||
public function getConnection()
|
||||
{
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Http\SessionHandlers;
|
||||
|
||||
use SessionHandlerInterface;
|
||||
|
||||
abstract class AbstractHandler implements SessionHandlerInterface
|
||||
{
|
||||
/** @var string */
|
||||
protected $name;
|
||||
|
||||
/** @var string */
|
||||
protected $sessionPath;
|
||||
|
||||
/**
|
||||
* Bootstrap the session handler
|
||||
*
|
||||
* @param string $sessionPath
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function open($sessionPath, $name): bool
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->sessionPath = $sessionPath;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the session handler
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove old sessions
|
||||
*
|
||||
* @param int $maxLifetime
|
||||
* @return bool
|
||||
*/
|
||||
public function gc($maxLifetime): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read session data
|
||||
*
|
||||
* @param string $id
|
||||
* @return string
|
||||
*/
|
||||
abstract public function read($id): string;
|
||||
|
||||
/**
|
||||
* Write session data
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $data
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function write($id, $data): bool;
|
||||
|
||||
/**
|
||||
* Delete a session
|
||||
*
|
||||
* @param string $id
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function destroy($id): bool;
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Http\SessionHandlers;
|
||||
|
||||
use Engelsystem\Database\Database;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
|
||||
class DatabaseHandler extends AbstractHandler
|
||||
{
|
||||
/** @var Database */
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read($id): string
|
||||
{
|
||||
$session = $this->getQuery()
|
||||
->where('id', '=', $id)
|
||||
->first();
|
||||
|
||||
return $session ? $session->payload : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
$values = [
|
||||
'payload' => $data,
|
||||
'last_activity' => $this->getCurrentTimestamp(),
|
||||
];
|
||||
|
||||
$session = $this->getQuery()
|
||||
->where('id', '=', $id)
|
||||
->first();
|
||||
|
||||
if (!$session) {
|
||||
return $this->getQuery()
|
||||
->insert($values + [
|
||||
'id' => $id,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->getQuery()
|
||||
->where('id', '=', $id)
|
||||
->update($values);
|
||||
|
||||
// The update return can't be used directly because it won't change if the second call is in the same second
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroy($id): bool
|
||||
{
|
||||
$this->getQuery()
|
||||
->where('id', '=', $id)
|
||||
->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function gc($maxLifetime): bool
|
||||
{
|
||||
$timestamp = $this->getCurrentTimestamp(-$maxLifetime);
|
||||
|
||||
$this->getQuery()
|
||||
->where('last_activity', '<', $timestamp)
|
||||
->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return QueryBuilder
|
||||
*/
|
||||
protected function getQuery(): QueryBuilder
|
||||
{
|
||||
return $this->database
|
||||
->getConnection()
|
||||
->table('sessions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the SQL timestamp
|
||||
*
|
||||
* @param int $diff
|
||||
* @return string
|
||||
*/
|
||||
protected function getCurrentTimestamp(int $diff = 0): string
|
||||
{
|
||||
return date('Y-m-d H:i:s', strtotime(sprintf('%+d seconds', $diff)));
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Database;
|
||||
|
||||
use Engelsystem\Database\Database;
|
||||
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
||||
use Illuminate\Database\Connection as DatabaseConnection;
|
||||
use PDO;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||
|
||||
class DatabaseTest extends TestCase
|
||||
{
|
||||
/** @var DatabaseConnection */
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::__construct()
|
||||
* @covers \Engelsystem\Database\Database::getPdo()
|
||||
* @covers \Engelsystem\Database\Database::getConnection()
|
||||
*/
|
||||
public function testInit()
|
||||
{
|
||||
/** @var Pdo|MockObject $pdo */
|
||||
$pdo = $this->getMockBuilder(Pdo::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
/** @var DatabaseConnection|MockObject $databaseConnection */
|
||||
$databaseConnection = $this->getMockBuilder(DatabaseConnection::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$databaseConnection->expects($this->atLeastOnce())
|
||||
->method('getPdo')
|
||||
->willReturn($pdo);
|
||||
|
||||
$db = new Database($databaseConnection);
|
||||
|
||||
$this->assertEquals($databaseConnection, $db->getConnection());
|
||||
$this->assertEquals($pdo, $db->getPdo());
|
||||
$this->assertInstanceOf(PDO::class, $db->getPdo());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::select()
|
||||
*/
|
||||
public function testSelect()
|
||||
{
|
||||
$db = new Database($this->connection);
|
||||
|
||||
$return = $db->select('SELECT * FROM test_data');
|
||||
$this->assertTrue(count($return) > 3);
|
||||
|
||||
$return = $db->select('SELECT * FROM test_data WHERE id = ?', [2]);
|
||||
$this->assertCount(1, $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::selectOne()
|
||||
*/
|
||||
public function testSelectOne()
|
||||
{
|
||||
$db = new Database($this->connection);
|
||||
|
||||
$return = $db->selectOne('SELECT * FROM test_data');
|
||||
$this->assertEquals('Foo', $return->data);
|
||||
|
||||
$return = $db->selectOne('SELECT * FROM test_data WHERE id = -1');
|
||||
$this->assertEmpty($return);
|
||||
|
||||
$return = $db->selectOne('SELECT * FROM test_data WHERE id = ?', [3]);
|
||||
$this->assertTrue(!is_array($return));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::insert()
|
||||
*/
|
||||
public function testInsert()
|
||||
{
|
||||
$db = new Database($this->connection);
|
||||
|
||||
$result = $db->insert("INSERT INTO test_data (id, data) VALUES (5, 'Some random text'), (6, 'another text')");
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::update()
|
||||
*/
|
||||
public function testUpdate()
|
||||
{
|
||||
$db = new Database($this->connection);
|
||||
|
||||
$count = $db->update("UPDATE test_data SET data='NOPE' WHERE data LIKE '%Replaceme%'");
|
||||
$this->assertEquals(3, $count);
|
||||
|
||||
$count = $db->update("UPDATE test_data SET data=? WHERE data LIKE '%NOPE%'", ['Some random text!']);
|
||||
$this->assertEquals(3, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Database\Database::delete()
|
||||
*/
|
||||
public function testDelete()
|
||||
{
|
||||
$db = new Database($this->connection);
|
||||
|
||||
$count = $db->delete('DELETE FROM test_data WHERE id=1');
|
||||
$this->assertEquals(1, $count);
|
||||
|
||||
$count = $db->delete('DELETE FROM test_data WHERE data LIKE ?', ['%Replaceme%']);
|
||||
$this->assertEquals(3, $count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup in memory database
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$dbManager = new CapsuleManager();
|
||||
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
||||
|
||||
$connection = $dbManager->getConnection();
|
||||
$this->connection = $connection;
|
||||
|
||||
$connection->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$connection->statement(
|
||||
'
|
||||
CREATE TABLE test_data(
|
||||
id INT PRIMARY KEY NOT NULL,
|
||||
data TEXT NOT NULL
|
||||
);
|
||||
');
|
||||
$connection->statement('CREATE UNIQUE INDEX test_data_id_uindex ON test_data (id);');
|
||||
$connection->insert("
|
||||
INSERT INTO test_data (id, data)
|
||||
VALUES
|
||||
(1, 'Foo'),
|
||||
(2, 'Bar'),
|
||||
(3, 'Batz'),
|
||||
(4, 'Lorem ipsum dolor sit'),
|
||||
(10, 'Replaceme ipsum dolor sit amet'),
|
||||
(11, 'Lorem Replaceme dolor sit amet'),
|
||||
(12, 'Lorem ipsum Replaceme sit amet')
|
||||
;");
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit;
|
||||
|
||||
use Engelsystem\Application;
|
||||
use Engelsystem\Database\Database;
|
||||
use Engelsystem\Database\Migration\Migrate;
|
||||
use Engelsystem\Database\Migration\MigrationServiceProvider;
|
||||
use Illuminate\Database\Capsule\Manager as CapsuleManager;
|
||||
use PDO;
|
||||
|
||||
trait HasDatabase
|
||||
{
|
||||
/** @var Database */
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* Setup in memory database
|
||||
*/
|
||||
protected function initDatabase()
|
||||
{
|
||||
$dbManager = new CapsuleManager();
|
||||
$dbManager->addConnection(['driver' => 'sqlite', 'database' => ':memory:']);
|
||||
|
||||
$connection = $dbManager->getConnection();
|
||||
$connection->getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->database = new Database($connection);
|
||||
|
||||
$app = new Application();
|
||||
$app->instance(Database::class, $this->database);
|
||||
$app->register(MigrationServiceProvider::class);
|
||||
|
||||
/** @var Migrate $migration */
|
||||
$migration = $app->get('db.migration');
|
||||
$migration->initMigration();
|
||||
|
||||
$this->database
|
||||
->getConnection()
|
||||
->table('migrations')
|
||||
->insert([
|
||||
['migration' => '2018_01_01_000001_import_install_sql'],
|
||||
['migration' => '2018_01_01_000002_import_update_sql'],
|
||||
]);
|
||||
|
||||
$migration->run(__DIR__ . '/../../db/migrations');
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\SessionHandlers;
|
||||
|
||||
use Engelsystem\Test\Unit\Http\SessionHandlers\Stub\ArrayHandler;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AbstractHandlerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Http\SessionHandlers\AbstractHandler::open
|
||||
*/
|
||||
public function testOpen()
|
||||
{
|
||||
$handler = new ArrayHandler();
|
||||
$return = $handler->open('/foo/bar', '1337asd098hkl7654');
|
||||
|
||||
$this->assertTrue($return);
|
||||
$this->assertEquals('1337asd098hkl7654', $handler->getName());
|
||||
$this->assertEquals('/foo/bar', $handler->getSessionPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Http\SessionHandlers\AbstractHandler::close
|
||||
*/
|
||||
public function testClose()
|
||||
{
|
||||
$handler = new ArrayHandler();
|
||||
$return = $handler->close();
|
||||
|
||||
$this->assertTrue($return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Http\SessionHandlers\AbstractHandler::gc
|
||||
*/
|
||||
public function testGc()
|
||||
{
|
||||
$handler = new ArrayHandler();
|
||||
$return = $handler->gc(60 * 60 * 24);
|
||||
|
||||
$this->assertTrue($return);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\SessionHandlers;
|
||||
|
||||
use Engelsystem\Http\SessionHandlers\DatabaseHandler;
|
||||
use Engelsystem\Test\Unit\HasDatabase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DatabaseHandlerTest extends TestCase
|
||||
{
|
||||
use HasDatabase;
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::__construct
|
||||
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::read
|
||||
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::getQuery
|
||||
*/
|
||||
public function testRead()
|
||||
{
|
||||
$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::write
|
||||
* @covers \Engelsystem\Http\SessionHandlers\DatabaseHandler::getCurrentTimestamp
|
||||
*/
|
||||
public function testWrite()
|
||||
{
|
||||
$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
|
||||
*/
|
||||
public function testDestroy()
|
||||
{
|
||||
$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
|
||||
*/
|
||||
public function testGc()
|
||||
{
|
||||
$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);
|
||||
|
||||
$this->assertTrue($handler->gc(60 * 60));
|
||||
|
||||
$return = $this->database->select('SELECT * FROM sessions');
|
||||
$this->assertCount(1, $return);
|
||||
|
||||
$return = array_shift($return);
|
||||
$this->assertEquals('bar', $return->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare tests
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
$this->initDatabase();
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Http\SessionHandlers\Stub;
|
||||
|
||||
use Engelsystem\Http\SessionHandlers\AbstractHandler;
|
||||
|
||||
class ArrayHandler extends AbstractHandler
|
||||
{
|
||||
/** @var string[] */
|
||||
protected $content = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function read($id): string
|
||||
{
|
||||
if (isset($this->content[$id])) {
|
||||
return $this->content[$id];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function write($id, $data): bool
|
||||
{
|
||||
$this->content[$id] = $data;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function destroy($id): bool
|
||||
{
|
||||
unset($this->content[$id]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSessionPath(): string
|
||||
{
|
||||
return $this->sessionPath;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue