From edeab5e75ffa02b075c151ca03ea1038f61e4396 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 16 Sep 2018 00:58:25 +0200 Subject: Added Database class as a replacement for Db, fixed naming --- src/Database/Database.php | 98 ++++++++++++++++++++++ src/Database/DatabaseServiceProvider.php | 13 ++- src/Database/Db.php | 1 + src/Database/Migration/Migrate.php | 14 ++-- .../Migration/MigrationServiceProvider.php | 11 ++- 5 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 src/Database/Database.php (limited to 'src/Database') diff --git a/src/Database/Database.php b/src/Database/Database.php new file mode 100644 index 00000000..407a8bf9 --- /dev/null +++ b/src/Database/Database.php @@ -0,0 +1,98 @@ +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; + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index 7328bc4e..cfdc89e7 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -5,6 +5,7 @@ namespace Engelsystem\Database; use Engelsystem\Container\ServiceProvider; use Exception; use Illuminate\Database\Capsule\Manager as CapsuleManager; +use Illuminate\Database\Connection as DatabaseConnection; use PDOException; class DatabaseServiceProvider extends ServiceProvider @@ -36,8 +37,18 @@ class DatabaseServiceProvider extends ServiceProvider $this->exitOnError(); } - $this->app->instance('db', $capsule); + $this->app->instance(CapsuleManager::class, $capsule); + $this->app->instance(Db::class, $capsule); Db::setDbManager($capsule); + + $connection = $capsule->getConnection(); + $this->app->instance(DatabaseConnection::class, $connection); + + $database = $this->app->make(Database::class); + $this->app->instance(Database::class, $database); + $this->app->instance('db', $database); + $this->app->instance('db.pdo', $pdo); + $this->app->instance('db.connection', $connection); } /** diff --git a/src/Database/Db.php b/src/Database/Db.php index f34d1564..30f63494 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -6,6 +6,7 @@ use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Connection as DatabaseConnection; use PDO; +/** @deprecated */ class Db { /** @var CapsuleManager */ diff --git a/src/Database/Migration/Migrate.php b/src/Database/Migration/Migrate.php index 3a08bb6e..cec8bc4a 100644 --- a/src/Database/Migration/Migrate.php +++ b/src/Database/Migration/Migrate.php @@ -18,7 +18,7 @@ class Migrate protected $app; /** @var SchemaBuilder */ - protected $scheme; + protected $schema; /** @var callable */ protected $output; @@ -29,13 +29,13 @@ class Migrate /** * Migrate constructor * - * @param SchemaBuilder $scheme + * @param SchemaBuilder $schema * @param Application $app */ - public function __construct(SchemaBuilder $scheme, Application $app) + public function __construct(SchemaBuilder $schema, Application $app) { $this->app = $app; - $this->scheme = $scheme; + $this->schema = $schema; $this->output = function () { }; } @@ -160,11 +160,11 @@ class Migrate */ protected function initMigration() { - if ($this->scheme->hasTable($this->table)) { + if ($this->schema->hasTable($this->table)) { return; } - $this->scheme->create($this->table, function (Blueprint $table) { + $this->schema->create($this->table, function (Blueprint $table) { $table->increments('id'); $table->string('migration'); }); @@ -177,7 +177,7 @@ class Migrate */ protected function getTableQuery() { - return $this->scheme->getConnection()->table($this->table); + return $this->schema->getConnection()->table($this->table); } /** diff --git a/src/Database/Migration/MigrationServiceProvider.php b/src/Database/Migration/MigrationServiceProvider.php index 15d06eaf..310b2114 100644 --- a/src/Database/Migration/MigrationServiceProvider.php +++ b/src/Database/Migration/MigrationServiceProvider.php @@ -3,16 +3,19 @@ namespace Engelsystem\Database\Migration; use Engelsystem\Container\ServiceProvider; -use Engelsystem\Database\Db; +use Engelsystem\Database\Database; use Illuminate\Database\Schema\Builder as SchemaBuilder; class MigrationServiceProvider extends ServiceProvider { public function register() { - $schema = Db::connection()->getSchemaBuilder(); - $this->app->instance('db.scheme', $schema); - $this->app->bind(SchemaBuilder::class, 'db.scheme'); + /** @var Database $database */ + $database = $this->app->get(Database::class); + $schema = $database->getConnection()->getSchemaBuilder(); + + $this->app->instance('db.schema', $schema); + $this->app->bind(SchemaBuilder::class, 'db.schema'); $migration = $this->app->make(Migrate::class); $this->app->instance('db.migration', $migration); -- cgit v1.2.3-54-g00ecf From 104e4f4c437376eb739dd3ef2de603855947a557 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sat, 15 Sep 2018 17:24:59 +0200 Subject: Session: Added Symfony PDO backend --- config/config.default.php | 9 +++ .../2018_09_11_000000_create_sessions_table.php | 28 ++++++++ src/Database/DatabaseServiceProvider.php | 3 +- src/Http/SessionServiceProvider.php | 32 ++++++++- src/Models/BaseModel.php | 16 +++++ .../Unit/Database/DatabaseServiceProviderTest.php | 6 +- tests/Unit/Http/SessionServiceProviderTest.php | 75 ++++++++++++++++++++-- tests/Unit/Models/BaseModelTest.php | 24 +++++++ tests/Unit/Models/Stub/BaseModelImplementation.php | 12 ++++ 9 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 db/migrations/2018_09_11_000000_create_sessions_table.php (limited to 'src/Database') diff --git a/config/config.default.php b/config/config.default.php index a634c28c..e085d307 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -137,4 +137,13 @@ return [ '3XL' => '3XL', '4XL' => '4XL' ], + + // Session config + 'session' => [ + // Supported: pdo or native + 'driver' => env('SESSION_DRIVER', 'pdo'), + + // Cookie name + 'name' => 'session', + ], ]; diff --git a/db/migrations/2018_09_11_000000_create_sessions_table.php b/db/migrations/2018_09_11_000000_create_sessions_table.php new file mode 100644 index 00000000..0af96d33 --- /dev/null +++ b/db/migrations/2018_09_11_000000_create_sessions_table.php @@ -0,0 +1,28 @@ +schema->create('sessions', function (Blueprint $table) { + $table->string('id')->unique(); + $table->text('payload'); + $table->integer('last_activity'); + $table->integer('lifetime'); + }); + } + + /** + * Reverse the migration + */ + public function down() + { + $this->schema->dropIfExists('sessions'); + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php index cfdc89e7..b3c33588 100644 --- a/src/Database/DatabaseServiceProvider.php +++ b/src/Database/DatabaseServiceProvider.php @@ -31,8 +31,9 @@ class DatabaseServiceProvider extends ServiceProvider $capsule->bootEloquent(); $capsule->getConnection()->useDefaultSchemaGrammar(); + $pdo = null; try { - $capsule->getConnection()->getPdo(); + $pdo = $capsule->getConnection()->getPdo(); } catch (PDOException $e) { $this->exitOnError(); } diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php index 59121a3b..66ff18cc 100644 --- a/src/Http/SessionServiceProvider.php +++ b/src/Http/SessionServiceProvider.php @@ -2,8 +2,10 @@ namespace Engelsystem\Http; +use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; @@ -38,7 +40,35 @@ class SessionServiceProvider extends ServiceProvider return $this->app->make(MockArraySessionStorage::class); } - return $this->app->make(NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]); + /** @var Config $config */ + $config = $this->app->get('config'); + $sessionConfig = $config->get('session'); + + $handler = null; + $driver = $sessionConfig['driver']; + + switch ($driver) { + case 'pdo': + $handler = $this->app->make(PdoSessionHandler::class, [ + 'pdoOrDsn' => $this->app->get('db.pdo'), + 'options' => [ + 'db_table' => 'sessions', + 'db_id_col' => 'id', + 'db_data_col' => 'payload', + 'db_lifetime_col' => 'lifetime', + 'db_time_col' => 'last_activity', + ], + ]); + break; + } + + return $this->app->make(NativeSessionStorage::class, [ + 'options' => [ + 'cookie_httponly' => true, + 'name' => $sessionConfig['name'], + ], + 'handler' => $handler, + ]); } /** diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php index cf718e4f..d5ded428 100644 --- a/src/Models/BaseModel.php +++ b/src/Models/BaseModel.php @@ -2,6 +2,8 @@ namespace Engelsystem\Models; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; abstract class BaseModel extends Model @@ -10,6 +12,8 @@ abstract class BaseModel extends Model public $timestamps = false; /** + * Create a new model + * * @param array $attributes * @return BaseModel */ @@ -20,4 +24,16 @@ abstract class BaseModel extends Model return $instance; } + + /** + * Find a model by its primary key + * + * @param mixed $id + * @param array $columns + * @return Builder|Builder[]|Collection|Model|null + */ + public static function find($id, $columns = ['*']) + { + return static::query()->find($id, $columns); + } } diff --git a/tests/Unit/Database/DatabaseServiceProviderTest.php b/tests/Unit/Database/DatabaseServiceProviderTest.php index 0f259036..7dae065f 100644 --- a/tests/Unit/Database/DatabaseServiceProviderTest.php +++ b/tests/Unit/Database/DatabaseServiceProviderTest.php @@ -2,6 +2,7 @@ namespace Engelsystem\Test\Unit\Database; +use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Database\Database; use Engelsystem\Database\DatabaseServiceProvider; @@ -10,6 +11,7 @@ use Engelsystem\Test\Unit\ServiceProviderTest; use Exception; use Illuminate\Database\Capsule\Manager as CapsuleManager; use Illuminate\Database\Connection; +use PDO; use PDOException; use PHPUnit_Framework_MockObject_MockObject as MockObject; @@ -117,12 +119,12 @@ class DatabaseServiceProviderTest extends ServiceProviderTest $this->setExpects($connection, 'useDefaultSchemaGrammar'); $connection->expects($this->once()) ->method('getPdo') - ->willReturnCallback(function () use ($getPdoThrowException) { + ->willReturnCallback(function () use ($getPdoThrowException, $pdo) { if ($getPdoThrowException) { throw new PDOException(); } - return ''; + return $pdo; }); $this->setExpects($dbManager, 'getConnection', [], $connection, $this->atLeastOnce()); diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php index d0125bc2..5e4575b3 100644 --- a/tests/Unit/Http/SessionServiceProviderTest.php +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -2,11 +2,14 @@ namespace Engelsystem\Test\Unit\Http; +use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Http\SessionServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; +use PDO; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface; @@ -23,6 +26,9 @@ class SessionServiceProviderTest extends ServiceProviderTest $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); $sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class); + $pdoSessionHandler = $this->getMockBuilder(PdoSessionHandler::class) + ->disableOriginalConstructor() + ->getMock(); $session = $this->getSessionMock(); $request = $this->getRequestMock(); @@ -32,22 +38,54 @@ class SessionServiceProviderTest extends ServiceProviderTest ->setConstructorArgs([$app]) ->setMethods(['isCli']) ->getMock(); - $serviceProvider->expects($this->exactly(2)) + + /** @var Config|MockObject $config */ + $config = $this->createMock(Config::class); + /** @var PDO|MockObject $pdo */ + $pdo = $this->getMockBuilder(PDO::class) + ->disableOriginalConstructor() + ->getMock(); + + $serviceProvider->expects($this->exactly(3)) ->method('isCli') - ->willReturnOnConsecutiveCalls(true, false); + ->willReturnOnConsecutiveCalls(true, false, false); - $app->expects($this->exactly(4)) + $app->expects($this->exactly(7)) ->method('make') ->withConsecutive( [MockArraySessionStorage::class], [Session::class], - [NativeSessionStorage::class, ['options' => ['cookie_httponly' => true]]], + [ + NativeSessionStorage::class, + ['options' => ['cookie_httponly' => true, 'name' => 'session'], 'handler' => null] + ], + [Session::class], + [ + PdoSessionHandler::class, + [ + 'pdoOrDsn' => $pdo, + 'options' => [ + 'db_table' => 'sessions', + 'db_id_col' => 'id', + 'db_data_col' => 'payload', + 'db_lifetime_col' => 'lifetime', + 'db_time_col' => 'last_activity', + ], + ] + ], + [ + NativeSessionStorage::class, + ['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $pdoSessionHandler] + ], [Session::class] ) ->willReturnOnConsecutiveCalls( $sessionStorage, $session, $sessionStorage2, + $session, + $pdoSessionHandler, + $sessionStorage2, $session ); $app->expects($this->atLeastOnce()) @@ -58,13 +96,40 @@ class SessionServiceProviderTest extends ServiceProviderTest ['session', $session] ); + $app->expects($this->exactly(6)) + ->method('get') + ->withConsecutive( + ['request'], + ['config'], + ['request'], + ['config'], + ['db.pdo'], + ['request'] + ) + ->willReturnOnConsecutiveCalls( + $request, + $config, + $request, + $config, + $pdo, + $request + ); + + $config->expects($this->exactly(2)) + ->method('get') + ->with('session') + ->willReturnOnConsecutiveCalls( + ['driver' => 'native', 'name' => 'session'], + ['driver' => 'pdo', 'name' => 'foobar'] + ); + $this->setExpects($app, 'bind', [StorageInterface::class, 'session.storage'], null, $this->atLeastOnce()); - $this->setExpects($app, 'get', ['request'], $request, $this->atLeastOnce()); $this->setExpects($request, 'setSession', [$session], null, $this->atLeastOnce()); $this->setExpects($session, 'start', null, null, $this->atLeastOnce()); $serviceProvider->register(); $serviceProvider->register(); + $serviceProvider->register(); } /** diff --git a/tests/Unit/Models/BaseModelTest.php b/tests/Unit/Models/BaseModelTest.php index 52cb8c7b..9af55fa1 100644 --- a/tests/Unit/Models/BaseModelTest.php +++ b/tests/Unit/Models/BaseModelTest.php @@ -3,6 +3,8 @@ namespace Engelsystem\Test\Unit\Models; use Engelsystem\Test\Unit\Models\Stub\BaseModelImplementation; +use Illuminate\Database\Eloquent\Builder as QueryBuilder; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; class BaseModelTest extends TestCase @@ -19,4 +21,26 @@ class BaseModelTest extends TestCase $this->assertEquals('bar', $newModel->foo); $this->assertEquals(1, $newModel->saveCount); } + + /** + * @covers \Engelsystem\Models\BaseModel::find + */ + public function testFind() + { + /** @var QueryBuilder|MockObject $queryBuilder */ + $queryBuilder = $this->createMock(QueryBuilder::class); + BaseModelImplementation::$queryBuilder = $queryBuilder; + + $anotherModel = new BaseModelImplementation(); + + $queryBuilder->expects($this->once()) + ->method('find') + ->with(1337, ['foo', 'bar']) + ->willReturn($anotherModel); + + $model = new BaseModelImplementation(); + $newModel = $model->find(1337, ['foo', 'bar']); + + $this->assertEquals($anotherModel, $newModel); + } } diff --git a/tests/Unit/Models/Stub/BaseModelImplementation.php b/tests/Unit/Models/Stub/BaseModelImplementation.php index 4aa1ef0b..70643b03 100644 --- a/tests/Unit/Models/Stub/BaseModelImplementation.php +++ b/tests/Unit/Models/Stub/BaseModelImplementation.php @@ -3,6 +3,7 @@ namespace Engelsystem\Test\Unit\Models\Stub; use Engelsystem\Models\BaseModel; +use Illuminate\Database\Eloquent\Builder as QueryBuilder; /** * @property string foo @@ -15,6 +16,9 @@ class BaseModelImplementation extends BaseModel /** @var int */ public $saveCount = 0; + /** @var QueryBuilder */ + public static $queryBuilder = null; + /** * @param array $options * @return bool @@ -24,4 +28,12 @@ class BaseModelImplementation extends BaseModel $this->saveCount++; return true; } + + /** + * @return QueryBuilder + */ + public static function query() + { + return self::$queryBuilder; + } } -- cgit v1.2.3-54-g00ecf From 0b0890f425ced27b2204a046296de4cccdac4eb8 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Sun, 16 Sep 2018 14:08:09 +0200 Subject: Session: Added DatabaseHandler, replaces Symfony PdoSessionHandler --- .../2018_08_30_000000_create_log_entries_table.php | 12 ++- .../2018_09_11_000000_create_sessions_table.php | 3 +- src/Database/Migration/Migrate.php | 30 +++--- src/Http/SessionHandlers/AbstractHandler.php | 75 ++++++++++++++ src/Http/SessionHandlers/DatabaseHandler.php | 108 +++++++++++++++++++++ src/Http/SessionServiceProvider.php | 17 +--- tests/Unit/HasDatabase.php | 47 +++++++++ .../Http/SessionHandlers/AbstractHandlerTest.php | 44 +++++++++ .../Http/SessionHandlers/DatabaseHandlerTest.php | 95 ++++++++++++++++++ .../Http/SessionHandlers/Stub/ArrayHandler.php | 59 +++++++++++ tests/Unit/Http/SessionServiceProviderTest.php | 31 ++---- 11 files changed, 460 insertions(+), 61 deletions(-) create mode 100644 src/Http/SessionHandlers/AbstractHandler.php create mode 100644 src/Http/SessionHandlers/DatabaseHandler.php create mode 100644 tests/Unit/HasDatabase.php create mode 100644 tests/Unit/Http/SessionHandlers/AbstractHandlerTest.php create mode 100644 tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php create mode 100644 tests/Unit/Http/SessionHandlers/Stub/ArrayHandler.php (limited to 'src/Database') diff --git a/db/migrations/2018_08_30_000000_create_log_entries_table.php b/db/migrations/2018_08_30_000000_create_log_entries_table.php index 68815434..bef78712 100644 --- a/db/migrations/2018_08_30_000000_create_log_entries_table.php +++ b/db/migrations/2018_08_30_000000_create_log_entries_table.php @@ -17,12 +17,14 @@ class CreateLogEntriesTable extends Migration $table->timestamp('created_at')->nullable(); }); - $this->schema->getConnection()->unprepared(' - INSERT INTO log_entries (`id`, `level`, `message`, `created_at`) - SELECT `id`, `level`, `message`, FROM_UNIXTIME(`timestamp`) FROM LogEntries - '); + if ($this->schema->hasTable('LogEntries')) { + $this->schema->getConnection()->unprepared(' + INSERT INTO log_entries (`id`, `level`, `message`, `created_at`) + SELECT `id`, `level`, `message`, FROM_UNIXTIME(`timestamp`) FROM LogEntries + '); - $this->schema->dropIfExists('LogEntries'); + $this->schema->drop('LogEntries'); + } } /** diff --git a/db/migrations/2018_09_11_000000_create_sessions_table.php b/db/migrations/2018_09_11_000000_create_sessions_table.php index 0af96d33..33a9f569 100644 --- a/db/migrations/2018_09_11_000000_create_sessions_table.php +++ b/db/migrations/2018_09_11_000000_create_sessions_table.php @@ -13,8 +13,7 @@ class CreateSessionsTable extends Migration $this->schema->create('sessions', function (Blueprint $table) { $table->string('id')->unique(); $table->text('payload'); - $table->integer('last_activity'); - $table->integer('lifetime'); + $table->dateTime('last_activity')->useCurrent(); }); } diff --git a/src/Database/Migration/Migrate.php b/src/Database/Migration/Migrate.php index cec8bc4a..9c6d3e43 100644 --- a/src/Database/Migration/Migrate.php +++ b/src/Database/Migration/Migrate.php @@ -76,6 +76,21 @@ class Migrate } } + /** + * Setup migration tables + */ + public function initMigration() + { + if ($this->schema->hasTable($this->table)) { + return; + } + + $this->schema->create($this->table, function (Blueprint $table) { + $table->increments('id'); + $table->string('migration'); + }); + } + /** * Get all migrated migrations * @@ -155,21 +170,6 @@ class Migrate return glob($dir . '/*_*.php'); } - /** - * Setup migration tables - */ - protected function initMigration() - { - if ($this->schema->hasTable($this->table)) { - return; - } - - $this->schema->create($this->table, function (Blueprint $table) { - $table->increments('id'); - $table->string('migration'); - }); - } - /** * Init a table query * diff --git a/src/Http/SessionHandlers/AbstractHandler.php b/src/Http/SessionHandlers/AbstractHandler.php new file mode 100644 index 00000000..135d0d43 --- /dev/null +++ b/src/Http/SessionHandlers/AbstractHandler.php @@ -0,0 +1,75 @@ +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; +} diff --git a/src/Http/SessionHandlers/DatabaseHandler.php b/src/Http/SessionHandlers/DatabaseHandler.php new file mode 100644 index 00000000..8df70287 --- /dev/null +++ b/src/Http/SessionHandlers/DatabaseHandler.php @@ -0,0 +1,108 @@ +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))); + } +} diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php index 66ff18cc..c2e09624 100644 --- a/src/Http/SessionServiceProvider.php +++ b/src/Http/SessionServiceProvider.php @@ -4,8 +4,8 @@ namespace Engelsystem\Http; use Engelsystem\Config\Config; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Http\SessionHandlers\DatabaseHandler; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; @@ -45,20 +45,9 @@ class SessionServiceProvider extends ServiceProvider $sessionConfig = $config->get('session'); $handler = null; - $driver = $sessionConfig['driver']; - - switch ($driver) { + switch ($sessionConfig['driver']) { case 'pdo': - $handler = $this->app->make(PdoSessionHandler::class, [ - 'pdoOrDsn' => $this->app->get('db.pdo'), - 'options' => [ - 'db_table' => 'sessions', - 'db_id_col' => 'id', - 'db_data_col' => 'payload', - 'db_lifetime_col' => 'lifetime', - 'db_time_col' => 'last_activity', - ], - ]); + $handler = $this->app->make(DatabaseHandler::class); break; } diff --git a/tests/Unit/HasDatabase.php b/tests/Unit/HasDatabase.php new file mode 100644 index 00000000..d69f0a3a --- /dev/null +++ b/tests/Unit/HasDatabase.php @@ -0,0 +1,47 @@ +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'); + } +} diff --git a/tests/Unit/Http/SessionHandlers/AbstractHandlerTest.php b/tests/Unit/Http/SessionHandlers/AbstractHandlerTest.php new file mode 100644 index 00000000..bfd2e883 --- /dev/null +++ b/tests/Unit/Http/SessionHandlers/AbstractHandlerTest.php @@ -0,0 +1,44 @@ +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); + } +} diff --git a/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php new file mode 100644 index 00000000..ea4f3701 --- /dev/null +++ b/tests/Unit/Http/SessionHandlers/DatabaseHandlerTest.php @@ -0,0 +1,95 @@ +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(); + } +} diff --git a/tests/Unit/Http/SessionHandlers/Stub/ArrayHandler.php b/tests/Unit/Http/SessionHandlers/Stub/ArrayHandler.php new file mode 100644 index 00000000..4d37da48 --- /dev/null +++ b/tests/Unit/Http/SessionHandlers/Stub/ArrayHandler.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/tests/Unit/Http/SessionServiceProviderTest.php b/tests/Unit/Http/SessionServiceProviderTest.php index 5e4575b3..dd0e538f 100644 --- a/tests/Unit/Http/SessionServiceProviderTest.php +++ b/tests/Unit/Http/SessionServiceProviderTest.php @@ -4,12 +4,11 @@ namespace Engelsystem\Test\Unit\Http; use Engelsystem\Config\Config; use Engelsystem\Http\Request; +use Engelsystem\Http\SessionHandlers\DatabaseHandler; use Engelsystem\Http\SessionServiceProvider; use Engelsystem\Test\Unit\ServiceProviderTest; -use PDO; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; -use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface as StorageInterface; @@ -26,7 +25,7 @@ class SessionServiceProviderTest extends ServiceProviderTest $sessionStorage = $this->getMockForAbstractClass(StorageInterface::class); $sessionStorage2 = $this->getMockForAbstractClass(StorageInterface::class); - $pdoSessionHandler = $this->getMockBuilder(PdoSessionHandler::class) + $databaseHandler = $this->getMockBuilder(DatabaseHandler::class) ->disableOriginalConstructor() ->getMock(); @@ -41,10 +40,6 @@ class SessionServiceProviderTest extends ServiceProviderTest /** @var Config|MockObject $config */ $config = $this->createMock(Config::class); - /** @var PDO|MockObject $pdo */ - $pdo = $this->getMockBuilder(PDO::class) - ->disableOriginalConstructor() - ->getMock(); $serviceProvider->expects($this->exactly(3)) ->method('isCli') @@ -60,22 +55,10 @@ class SessionServiceProviderTest extends ServiceProviderTest ['options' => ['cookie_httponly' => true, 'name' => 'session'], 'handler' => null] ], [Session::class], - [ - PdoSessionHandler::class, - [ - 'pdoOrDsn' => $pdo, - 'options' => [ - 'db_table' => 'sessions', - 'db_id_col' => 'id', - 'db_data_col' => 'payload', - 'db_lifetime_col' => 'lifetime', - 'db_time_col' => 'last_activity', - ], - ] - ], + [DatabaseHandler::class], [ NativeSessionStorage::class, - ['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $pdoSessionHandler] + ['options' => ['cookie_httponly' => true, 'name' => 'foobar'], 'handler' => $databaseHandler] ], [Session::class] ) @@ -84,7 +67,7 @@ class SessionServiceProviderTest extends ServiceProviderTest $session, $sessionStorage2, $session, - $pdoSessionHandler, + $databaseHandler, $sessionStorage2, $session ); @@ -96,14 +79,13 @@ class SessionServiceProviderTest extends ServiceProviderTest ['session', $session] ); - $app->expects($this->exactly(6)) + $app->expects($this->exactly(5)) ->method('get') ->withConsecutive( ['request'], ['config'], ['request'], ['config'], - ['db.pdo'], ['request'] ) ->willReturnOnConsecutiveCalls( @@ -111,7 +93,6 @@ class SessionServiceProviderTest extends ServiceProviderTest $config, $request, $config, - $pdo, $request ); -- cgit v1.2.3-54-g00ecf