From 30cbb47780db6bb029b9cba46bea96ca5970011b Mon Sep 17 00:00:00 2001 From: "omarkdev@gmail.com" Date: Mon, 14 Feb 2022 20:50:57 -0300 Subject: [PATCH] fix: session array clear meta data even if dont exist in array Session array clear the meta data even if don't exist in array Close #40 Signed-off-by: Marcos Felipe --- src/Storage/AbstractSessionArrayStorage.php | 9 +-------- test/SessionArrayStorageTest.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/Storage/AbstractSessionArrayStorage.php b/src/Storage/AbstractSessionArrayStorage.php index b5ecaf62..7768f1e4 100644 --- a/src/Storage/AbstractSessionArrayStorage.php +++ b/src/Storage/AbstractSessionArrayStorage.php @@ -436,16 +436,9 @@ public function clear($key = null) return $this; } - if (! isset($_SESSION[$key])) { - return $this; - } - - // Clear key data unset($_SESSION[$key]); - - // Clear key metadata $this->setMetadata($key, null) - ->unlock($key); + ->unlock($key); return $this; } diff --git a/test/SessionArrayStorageTest.php b/test/SessionArrayStorageTest.php index 4003b765..c99aae09 100644 --- a/test/SessionArrayStorageTest.php +++ b/test/SessionArrayStorageTest.php @@ -14,6 +14,9 @@ */ class SessionArrayStorageTest extends TestCase { + /** @var SessionArrayStorage */ + private $storage; + protected function setUp(): void { $_SESSION = []; @@ -200,4 +203,20 @@ public function testGetArrayCopyFromContainer(): void $container->baz = 'qux'; self::assertSame(['foo' => 'bar', 'baz' => 'qux'], $container->getArrayCopy()); } + + public function testClearMetaDataIfDontExistInSession(): void + { + $this->storage->setMetadata('foo', 'bar'); + $this->storage->clear('foo'); + + self::assertFalse($this->storage->getMetaData('foo')); + } + + public function testClearRemoveFromSession(): void + { + $this->storage->foo = 'bar'; + $this->storage->clear('foo'); + + self::assertArrayNotHasKey('foo', $_SESSION); + } }