Skip to content

Commit

Permalink
SessionArrayWrapper and SessionContainer (#64)
Browse files Browse the repository at this point in the history
* Correct typehint, closes #45

* Implement SessionContainer and SessionArrayWrapper
Closes #63
  • Loading branch information
g105b committed Feb 27, 2020
1 parent ed157bf commit 97b7932
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Session.php
Expand Up @@ -3,7 +3,7 @@

use SessionHandlerInterface;

class Session {
class Session implements SessionContainer {
const DEFAULT_SESSION_NAME = "PHPSESSID";
const DEFAULT_SESSION_LIFETIME = 0;
const DEFAULT_SESSION_PATH = "/tmp";
Expand Down
30 changes: 30 additions & 0 deletions src/SessionArrayWrapper.php
@@ -0,0 +1,30 @@
<?php
namespace Gt\Session;

class SessionArrayWrapper implements SessionContainer {
private $sourceArray;

public function __construct(array &$sourceArray) {
$this->sourceArray = &$sourceArray;
}

public function get(string $key) {
return $this->sourceArray[$key] ?? null;
}

public function set(string $key, $value) {
$this->sourceArray[$key] = $value;
}

public function contains(string $key):bool {
return isset($this->sourceArray[$key]);
}

public function remove(string $key):void {
if(!$this->contains($key)) {
return;
}

unset($this->sourceArray[$key]);
}
}
9 changes: 9 additions & 0 deletions src/SessionContainer.php
@@ -0,0 +1,9 @@
<?php
namespace Gt\Session;

interface SessionContainer {
public function get(string $key);
public function set(string $key, $value);
public function contains(string $key):bool;
public function remove(string $key):void;
}
2 changes: 1 addition & 1 deletion src/SessionStore.php
@@ -1,7 +1,7 @@
<?php
namespace Gt\Session;

class SessionStore {
class SessionStore implements SessionContainer {
/** @var string */
protected $name;
/** @var Session */
Expand Down
50 changes: 50 additions & 0 deletions test/unit/SessionArrayWrapperTest.php
@@ -0,0 +1,50 @@
<?php
namespace Gt\Session\Test;

use Gt\Session\SessionArrayWrapper;
use PHPUnit\Framework\TestCase;

class SessionArrayWrapperTest extends TestCase {
public function testContains() {
$sessionArray = [];
$key = uniqid();
$sut = new SessionArrayWrapper($sessionArray);
self::assertFalse($sut->contains($key));
$sut->set($key, "test-value");
self::assertTrue($sut->contains($key));
}

public function testSet() {
$sessionArray = [];
$key = uniqid();
$value = uniqid();
$sut = new SessionArrayWrapper($sessionArray);
$sut->set($key, $value);
self::assertEquals($value, $sessionArray[$key]);
}

public function testGet() {
$sessionArray = [];
$key = uniqid();
$value = uniqid();
$sut = new SessionArrayWrapper($sessionArray);
self::assertNull($sut->get($key));
self::assertArrayNotHasKey($key, $sessionArray);
$sut->set($key, $value);
self::assertEquals($value, $sut->get($key));
self::assertEquals($value, $sessionArray[$key]);
}

public function testRemove() {
$key = uniqid();
$value = uniqid();
$sessionArray = [
$key => $value,
];
$sut = new SessionArrayWrapper($sessionArray);
self::assertTrue($sut->contains($key));
$sut->remove($key);
self::assertFalse($sut->contains($key));
self::assertArrayNotHasKey($key, $sessionArray);
}
}

0 comments on commit 97b7932

Please sign in to comment.