diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-16 14:08:09 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-09-25 14:02:55 +0200 |
commit | 0b0890f425ced27b2204a046296de4cccdac4eb8 (patch) | |
tree | 5b7a697185ca543ef9c0f0959532dfe8bfb9e3f1 /tests/Unit/Http/SessionHandlers | |
parent | 104e4f4c437376eb739dd3ef2de603855947a557 (diff) |
Session: Added DatabaseHandler, replaces Symfony PdoSessionHandler
Diffstat (limited to 'tests/Unit/Http/SessionHandlers')
3 files changed, 198 insertions, 0 deletions
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 @@ +<?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); + } +} 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 @@ +<?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(); + } +} 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 @@ +<?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; + } +} |