From 2eab602a7b1e164654ecca8a0c4391041069a125 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Sat, 18 Jun 2016 20:47:43 +0200 Subject: [PATCH 1/4] PHP 7 requires that reading always returns a string, regardless the existence of data. Fixes #35. --- src/SaveHandler/Cache.php | 2 +- src/SaveHandler/DbTableGateway.php | 2 +- test/SaveHandler/CacheTest.php | 13 +++++++++++++ test/SaveHandler/DbTableGatewayTest.php | 12 ++++++++++++ test/SaveHandler/MongoDBTest.php | 13 +++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/SaveHandler/Cache.php b/src/SaveHandler/Cache.php index be3b4d75..3018ebd1 100644 --- a/src/SaveHandler/Cache.php +++ b/src/SaveHandler/Cache.php @@ -81,7 +81,7 @@ public function close() */ public function read($id) { - return $this->getCacheStorage()->getItem($id); + return (string) $this->getCacheStorage()->getItem($id); } /** diff --git a/src/SaveHandler/DbTableGateway.php b/src/SaveHandler/DbTableGateway.php index 0822e66e..7466b8c5 100644 --- a/src/SaveHandler/DbTableGateway.php +++ b/src/SaveHandler/DbTableGateway.php @@ -102,7 +102,7 @@ public function read($id) if ($row = $rows->current()) { if ($row->{$this->options->getModifiedColumn()} + $row->{$this->options->getLifetimeColumn()} > time()) { - return $row->{$this->options->getDataColumn()}; + return (string) $row->{$this->options->getDataColumn()}; } $this->destroy($id); } diff --git a/test/SaveHandler/CacheTest.php b/test/SaveHandler/CacheTest.php index fffab873..57afb03a 100644 --- a/test/SaveHandler/CacheTest.php +++ b/test/SaveHandler/CacheTest.php @@ -103,4 +103,17 @@ public function testReadWriteTwice() $this->assertEquals($this->testArray, unserialize($saveHandler->read($id))); } + + public function testReadShouldAlwaysReturnString() + { + $cacheStorage = $this->prophesize('Zend\Cache\Storage\StorageInterface'); + $cacheStorage->getItem('242')->willReturn(null); + $this->usedSaveHandlers[] = $saveHandler = new Cache($cacheStorage->reveal()); + + $id = '242'; + + $data = $saveHandler->read($id); + + $this->assertTrue(is_string($data)); + } } diff --git a/test/SaveHandler/DbTableGatewayTest.php b/test/SaveHandler/DbTableGatewayTest.php index d424a7c2..300a6b13 100644 --- a/test/SaveHandler/DbTableGatewayTest.php +++ b/test/SaveHandler/DbTableGatewayTest.php @@ -123,6 +123,18 @@ public function testReadWriteTwice() $this->assertEquals($this->testArray, unserialize($saveHandler->read($id))); } + public function testReadShouldAlwaysReturnString() + { + $this->usedSaveHandlers[] = $saveHandler = new DbTableGateway($this->tableGateway, $this->options); + $saveHandler->open('savepath', 'sessionname'); + + $id = '242'; + + $data = $saveHandler->read($id); + + $this->assertTrue(is_string($data)); + } + /** * Sets up the database connection and creates the table for session data * diff --git a/test/SaveHandler/MongoDBTest.php b/test/SaveHandler/MongoDBTest.php index e37cd921..5ca2ee3d 100644 --- a/test/SaveHandler/MongoDBTest.php +++ b/test/SaveHandler/MongoDBTest.php @@ -153,4 +153,17 @@ public function testWriteExceptionEdgeCaseForChangedSessionName() $saveHandler->open('savepath', 'sessionname_changed'); $saveHandler->write($id, serialize($data)); } + + public function testReadShouldAlwaysReturnString() + { + $saveHandler = new MongoDB($this->mongoClient, $this->options); + $this->assertTrue($saveHandler->open('savepath', 'sessionname')); + + $id = '242'; + + $data = $saveHandler->read($id); + + $this->assertTrue(is_string($data)); + } + } From ab4876e7786aa7485fe335172aeeea592be3b862 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Sun, 19 Jun 2016 00:06:25 +0200 Subject: [PATCH 2/4] cs fix --- test/SaveHandler/MongoDBTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/SaveHandler/MongoDBTest.php b/test/SaveHandler/MongoDBTest.php index 5ca2ee3d..b06fa666 100644 --- a/test/SaveHandler/MongoDBTest.php +++ b/test/SaveHandler/MongoDBTest.php @@ -165,5 +165,4 @@ public function testReadShouldAlwaysReturnString() $this->assertTrue(is_string($data)); } - } From 88f93fa372380d3afa6c489b0fcadf743ad7b94c Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Mon, 20 Jun 2016 21:35:07 +0200 Subject: [PATCH 3/4] Destroy should only return false when removal of session truely fails. Fixes #36 and #42. --- src/SaveHandler/DbTableGateway.php | 8 ++++-- test/SaveHandler/DbTableGatewayTest.php | 35 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/SaveHandler/DbTableGateway.php b/src/SaveHandler/DbTableGateway.php index 7466b8c5..ddeaf30b 100644 --- a/src/SaveHandler/DbTableGateway.php +++ b/src/SaveHandler/DbTableGateway.php @@ -149,10 +149,14 @@ public function write($id, $data) */ public function destroy($id) { - return (bool) $this->tableGateway->delete([ + $exists = (bool) $this->read($id); + + $deleted = (bool) $this->tableGateway->delete([ $this->options->getIdColumn() => $id, $this->options->getNameColumn() => $this->sessionName, - ]); + ]); + + return $exists ? $deleted : true; } /** diff --git a/test/SaveHandler/DbTableGatewayTest.php b/test/SaveHandler/DbTableGatewayTest.php index 300a6b13..b81f4582 100644 --- a/test/SaveHandler/DbTableGatewayTest.php +++ b/test/SaveHandler/DbTableGatewayTest.php @@ -47,6 +47,13 @@ class DbTableGatewayTest extends \PHPUnit_Framework_TestCase */ protected $usedSaveHandlers = []; + /** + * Test data container. + * + * @var array + */ + private $testArray; + /** * Setup performed prior to each test method * @@ -135,10 +142,36 @@ public function testReadShouldAlwaysReturnString() $this->assertTrue(is_string($data)); } + public function testDestroyReturnsTrueEvenWhenSessionDoesNotExist() + { + $this->usedSaveHandlers[] = $saveHandler = new DbTableGateway($this->tableGateway, $this->options); + $saveHandler->open('savepath', 'sessionname'); + + $id = '242'; + + $result = $saveHandler->destroy($id); + + $this->assertTrue($result); + } + + public function testDestroyReturnsTrueWhenSessionIsDeleted() + { + $this->usedSaveHandlers[] = $saveHandler = new DbTableGateway($this->tableGateway, $this->options); + $saveHandler->open('savepath', 'sessionname'); + + $id = '242'; + + $this->assertTrue($saveHandler->write($id, serialize($this->testArray))); + + $result = $saveHandler->destroy($id); + + $this->assertTrue($result); + } + /** * Sets up the database connection and creates the table for session data * - * @param Zend\Session\SaveHandler\DbTableGatewayOptions $options + * @param \Zend\Session\SaveHandler\DbTableGatewayOptions $options * @return void */ protected function setupDb(DbTableGatewayOptions $options) From 6a6dd8d5e5df8403b727054a759e06acf34a1227 Mon Sep 17 00:00:00 2001 From: Zacharias Luiten Date: Mon, 20 Jun 2016 21:55:27 +0200 Subject: [PATCH 4/4] cs fix --- src/SaveHandler/DbTableGateway.php | 6 +++--- test/SaveHandler/DbTableGatewayTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SaveHandler/DbTableGateway.php b/src/SaveHandler/DbTableGateway.php index ddeaf30b..8cb2b3f5 100644 --- a/src/SaveHandler/DbTableGateway.php +++ b/src/SaveHandler/DbTableGateway.php @@ -150,12 +150,12 @@ public function write($id, $data) public function destroy($id) { $exists = (bool) $this->read($id); - + $deleted = (bool) $this->tableGateway->delete([ $this->options->getIdColumn() => $id, $this->options->getNameColumn() => $this->sessionName, - ]); - + ]); + return $exists ? $deleted : true; } diff --git a/test/SaveHandler/DbTableGatewayTest.php b/test/SaveHandler/DbTableGatewayTest.php index b81f4582..08b5bb8e 100644 --- a/test/SaveHandler/DbTableGatewayTest.php +++ b/test/SaveHandler/DbTableGatewayTest.php @@ -49,7 +49,7 @@ class DbTableGatewayTest extends \PHPUnit_Framework_TestCase /** * Test data container. - * + * * @var array */ private $testArray;