Skip to content

Commit

Permalink
Merge pull request #48 from omarkdev/fix/#40-clear-meta-data
Browse files Browse the repository at this point in the history
Clear metadata even if session key didn't exist
  • Loading branch information
Ocramius authored Feb 15, 2022
2 parents fc538bb + 30cbb47 commit 888c6a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/Storage/AbstractSessionArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions test/SessionArrayStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
class SessionArrayStorageTest extends TestCase
{
/** @var SessionArrayStorage */
private $storage;

protected function setUp(): void
{
$_SESSION = [];
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 888c6a3

Please sign in to comment.