Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SessionArrayWrapper and SessionContainer (#64)
- Loading branch information
Showing
5 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |