Skip to content

Commit

Permalink
Test ProtectedGlobal offsetUnset throws exception
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Dec 5, 2018
1 parent 07329d2 commit c95ea9e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ProtectedGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ProtectedGlobal implements ArrayAccess {
const WARNING_MESSAGE = "Global variables are protected - see https://php.gt/globals";

public $whiteListData = [];
protected $whiteListData;

public function __construct(array $whiteListData = []) {
$this->whiteListData = $whiteListData;
Expand All @@ -19,7 +19,7 @@ public function __toString():string {
public function __debugInfo():array {
return array_merge([
"WARNING" => (string)$this,
], $this->whiteListData);
], $this->whiteListData ?? []);
}

public function offsetExists($offset):bool {
Expand All @@ -41,6 +41,7 @@ public function offsetGet($offset) {
public function offsetSet($offset, $value):void {
if(array_key_exists($offset, $this->whiteListData)) {
$this->whiteListData[$offset] = $value;
return;
}

$this->throwException();
Expand All @@ -49,6 +50,7 @@ public function offsetSet($offset, $value):void {
public function offsetUnset($offset):void {
if(array_key_exists($offset, $this->whiteListData)) {
unset($this->whiteListData);
return;
}

$this->throwException();
Expand Down
19 changes: 19 additions & 0 deletions test/unit/ProtectedGlobalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ public function testOffsetSetThrowsException() {
self::expectException(ProtectedGlobalException::class);
$sut["something"] = "broken";
}

public function testOffsetSet() {
$whiteList = [
"name" => "test",
];
$sut = new ProtectedGlobal($whiteList);
$sut["name"] = "changed";

self::assertEquals("changed", $sut["name"]);
}

public function testOffsetUnsetThrowsException() {
$whiteList = [
"name" => "test",
];
$sut = new ProtectedGlobal($whiteList);
self::expectException(ProtectedGlobalException::class);
unset($sut["something"]);
}
}

0 comments on commit c95ea9e

Please sign in to comment.