Skip to content

Commit

Permalink
Tests (#147)
Browse files Browse the repository at this point in the history
* maintenance: dependabot
closes #142

* maintenance: dependabot
closes #142

* feature: override real globals in tests

* maintenance: static analysis improvements
  • Loading branch information
g105b authored Sep 25, 2022
1 parent 4e909c4 commit 6e8c0b1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 105 deletions.
11 changes: 9 additions & 2 deletions src/ProtectedGlobal.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

use ArrayAccess;

/** @implements ArrayAccess<string, mixed> */
class ProtectedGlobal implements ArrayAccess {
const WARNING_MESSAGE = "Global variables are protected - see https://php.gt/globals";

protected $whiteListData;
/** @var array<string, mixed> */
protected array $whiteListData;

/** @param array<string, mixed> $whiteListData */
public function __construct(array $whiteListData = []) {
$this->whiteListData = $whiteListData;
}
Expand All @@ -16,6 +19,7 @@ public function __toString():string {
return self::WARNING_MESSAGE;
}

/** @return array<string, mixed> */
public function __debugInfo():array {
return array_merge([
"WARNING" => (string)$this,
Expand All @@ -28,15 +32,18 @@ public function offsetExists($offset):bool {
}

$this->throwException();
/** @noinspection PhpUnreachableStatementInspection */
return false;
}

public function offsetGet($offset) {
public function offsetGet($offset):mixed {
if(array_key_exists($offset, $this->whiteListData)) {
return $this->whiteListData[$offset];
}

$this->throwException();
/** @noinspection PhpUnreachableStatementInspection */
return null;
}

public function offsetSet($offset, $value):void {
Expand Down
35 changes: 18 additions & 17 deletions src/Protection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
namespace Gt\ProtectedGlobal;

class Protection {
const GLOBAL_KEYS = [
"_ENV",
"_SERVER",
"_GET",
"_POST",
"_FILES",
"_COOKIE",
"_SESSION",
];
/**
* Pass in an optional whitelist to allow the specified globals to remain set. This is
* useful for tools like XDebug which require access to the $_COOKIE superglobal.
Expand All @@ -10,6 +19,10 @@ class Protection {
*
* The second parameter is a 2D array describing which keys to whitelist
* within each GLOBAL. For example: ["_ENV" => ["keepThis", "andKeepThis"]]
*
* @param array<string, mixed> $globalsToDeregister
* @param array<string, mixed> $whiteList
* @return array<string, mixed>
*/
public static function removeGlobals(
array $globalsToDeregister,
Expand Down Expand Up @@ -44,22 +57,10 @@ public static function removeGlobals(
return $keep;
}

public static function overrideInternals(
array $globals,
array &$env,
array &$server,
array &$get,
array &$post,
array &$files,
array &$cookie,
array &$session
):void {
$env = new ProtectedGlobal($globals["_ENV"] ?? []);
$server = new ProtectedGlobal($globals["_SERVER"] ?? []);
$get = new ProtectedGlobal($globals["_GET"] ?? []);
$post = new ProtectedGlobal($globals["_POST"] ?? []);
$files = new ProtectedGlobal($globals["_FILES"] ?? []);
$cookie = new ProtectedGlobal($globals["_COOKIE"] ?? []);
$session = new ProtectedGlobal($globals["_SESSION"] ?? []);
/** @param array<string, mixed> $whitelistedGlobals */
public static function overrideInternals(array $whitelistedGlobals):void {
foreach(self::GLOBAL_KEYS as $key) {
$GLOBALS[$key] = new ProtectedGlobal($whitelistedGlobals[$key] ?? []);
}
}
}
110 changes: 25 additions & 85 deletions test/phpunit/ProtectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
use PHPUnit\Framework\TestCase;

class ProtectionTest extends TestCase {
public function testRemoveGlobals() {
$globals = [
"_ENV" => [
"somekey" => "somevalue",
]
];

self::assertArrayHasKey("somekey", $globals["_ENV"]);
$updated = Protection::removeGlobals($globals);
self::assertArrayNotHasKey("_ENV", $updated);
self::assertNotNull($globals);
}

public function testOverride() {
$env = ["somekey" => "somevalue"];
$server = [];
$get = [];
$post = [];
$files = [];
$cookie = [];
$session = [];
$globals = [
"_ENV" => $env,
];
Expand All @@ -26,54 +33,30 @@ public function testOverride() {

self::assertEquals("somevalue", $env["somekey"]);

Protection::overrideInternals(
$globals,
$env,
$server,
$get,
$post,
$files,
$cookie,
$session
);
Protection::overrideInternals($globals);

self::assertInstanceOf(ProtectedGlobal::class, $env);
self::assertInstanceOf(ProtectedGlobal::class, $_ENV);
self::assertEquals("somevalue", $env["somekey"]);
}

public function testWhitelist() {
$env = ["somekey" => "somevalue", "anotherkey" => "anothervalue"];
$server = [];
$get = [];
$post = [];
$files = [];
$cookie = [];
$session = [];
$globals = [
"_ENV" => $env,
];
Protection::removeGlobals(
$whitelist = Protection::removeGlobals(
$globals,
[
"_ENV" => [
"anotherkey",
],
]
);
Protection::overrideInternals(
$globals,
$env,
$server,
$get,
$post,
$files,
$cookie,
$session
);
Protection::overrideInternals($whitelist);

self::assertEquals("anothervalue", $env["anotherkey"]);
self::assertEquals("anothervalue", $_ENV["anotherkey"]);
self::expectException(ProtectedGlobalException::class);
$variable = $env["somevalue"];
$value = $_ENV["somevalue"];
}

public function testWhitelistMany() {
Expand All @@ -93,7 +76,7 @@ public function testWhitelistMany() {

Protection::removeGlobals($env);
Protection::removeGlobals($server);
$fixedGlobals = Protection::removeGlobals(
$whitelisted = Protection::removeGlobals(
$globals,
[
"_GET" => [
Expand All @@ -108,55 +91,12 @@ public function testWhitelistMany() {

);

Protection::overrideInternals(
$fixedGlobals,
$env,
$server,
$get,
$post,
$files,
$cookie,
$session
);
Protection::overrideInternals($whitelisted);

self::assertEquals("Y2K", $get["name"]);
self::assertEquals("postvalue2", $post["postkey2"]);
self::assertEquals("Y2K", $_GET["name"]);
self::assertEquals("postvalue2", $_POST["postkey2"]);
self::expectException(ProtectedGlobalException::class);
$variable = $post["postkey1"];
}

public function testWhitelistNotExists() {
$env = [];
$server = [];
$get = ["name" => "Cody", "species" => "Feline"];
$post = [];
$files = [];
$cookie = [];
$session = [];
$globals = [
"_GET" => $get,
];
$globals = Protection::removeGlobals(
$globals,
[
"_GET" => [
"name",
"age",
],
]
);
Protection::overrideInternals(
$globals,
$env,
$server,
$get,
$post,
$files,
$cookie,
$session
);

self::assertEquals("Cody", $get["name"]);
self::assertNull($get["age"]);
$variable = $_POST["postkey1"];
var_dump($variable);
}
}
2 changes: 1 addition & 1 deletion test/phpunit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<phpunit colors="true">
<phpunit colors="true" processIsolation="true">
<testsuites>
<testsuite name="main">
<directory suffix="Test.php">.</directory>
Expand Down

0 comments on commit 6e8c0b1

Please sign in to comment.