Skip to content

Commit 6e8c0b1

Browse files
author
Greg Bowler
authored
Tests (#147)
* maintenance: dependabot closes #142 * maintenance: dependabot closes #142 * feature: override real globals in tests * maintenance: static analysis improvements
1 parent 4e909c4 commit 6e8c0b1

File tree

4 files changed

+53
-105
lines changed

4 files changed

+53
-105
lines changed

src/ProtectedGlobal.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
use ArrayAccess;
55

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

9-
protected $whiteListData;
10+
/** @var array<string, mixed> */
11+
protected array $whiteListData;
1012

13+
/** @param array<string, mixed> $whiteListData */
1114
public function __construct(array $whiteListData = []) {
1215
$this->whiteListData = $whiteListData;
1316
}
@@ -16,6 +19,7 @@ public function __toString():string {
1619
return self::WARNING_MESSAGE;
1720
}
1821

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

3034
$this->throwException();
35+
/** @noinspection PhpUnreachableStatementInspection */
3136
return false;
3237
}
3338

34-
public function offsetGet($offset) {
39+
public function offsetGet($offset):mixed {
3540
if(array_key_exists($offset, $this->whiteListData)) {
3641
return $this->whiteListData[$offset];
3742
}
3843

3944
$this->throwException();
45+
/** @noinspection PhpUnreachableStatementInspection */
46+
return null;
4047
}
4148

4249
public function offsetSet($offset, $value):void {

src/Protection.php

+18-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
namespace Gt\ProtectedGlobal;
33

44
class Protection {
5+
const GLOBAL_KEYS = [
6+
"_ENV",
7+
"_SERVER",
8+
"_GET",
9+
"_POST",
10+
"_FILES",
11+
"_COOKIE",
12+
"_SESSION",
13+
];
514
/**
615
* Pass in an optional whitelist to allow the specified globals to remain set. This is
716
* useful for tools like XDebug which require access to the $_COOKIE superglobal.
@@ -10,6 +19,10 @@ class Protection {
1019
*
1120
* The second parameter is a 2D array describing which keys to whitelist
1221
* within each GLOBAL. For example: ["_ENV" => ["keepThis", "andKeepThis"]]
22+
*
23+
* @param array<string, mixed> $globalsToDeregister
24+
* @param array<string, mixed> $whiteList
25+
* @return array<string, mixed>
1326
*/
1427
public static function removeGlobals(
1528
array $globalsToDeregister,
@@ -44,22 +57,10 @@ public static function removeGlobals(
4457
return $keep;
4558
}
4659

47-
public static function overrideInternals(
48-
array $globals,
49-
array &$env,
50-
array &$server,
51-
array &$get,
52-
array &$post,
53-
array &$files,
54-
array &$cookie,
55-
array &$session
56-
):void {
57-
$env = new ProtectedGlobal($globals["_ENV"] ?? []);
58-
$server = new ProtectedGlobal($globals["_SERVER"] ?? []);
59-
$get = new ProtectedGlobal($globals["_GET"] ?? []);
60-
$post = new ProtectedGlobal($globals["_POST"] ?? []);
61-
$files = new ProtectedGlobal($globals["_FILES"] ?? []);
62-
$cookie = new ProtectedGlobal($globals["_COOKIE"] ?? []);
63-
$session = new ProtectedGlobal($globals["_SESSION"] ?? []);
60+
/** @param array<string, mixed> $whitelistedGlobals */
61+
public static function overrideInternals(array $whitelistedGlobals):void {
62+
foreach(self::GLOBAL_KEYS as $key) {
63+
$GLOBALS[$key] = new ProtectedGlobal($whitelistedGlobals[$key] ?? []);
64+
}
6465
}
6566
}

test/phpunit/ProtectionTest.php

+25-85
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77
use PHPUnit\Framework\TestCase;
88

99
class ProtectionTest extends TestCase {
10+
public function testRemoveGlobals() {
11+
$globals = [
12+
"_ENV" => [
13+
"somekey" => "somevalue",
14+
]
15+
];
16+
17+
self::assertArrayHasKey("somekey", $globals["_ENV"]);
18+
$updated = Protection::removeGlobals($globals);
19+
self::assertArrayNotHasKey("_ENV", $updated);
20+
self::assertNotNull($globals);
21+
}
22+
1023
public function testOverride() {
1124
$env = ["somekey" => "somevalue"];
12-
$server = [];
13-
$get = [];
14-
$post = [];
15-
$files = [];
16-
$cookie = [];
17-
$session = [];
1825
$globals = [
1926
"_ENV" => $env,
2027
];
@@ -26,54 +33,30 @@ public function testOverride() {
2633

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

29-
Protection::overrideInternals(
30-
$globals,
31-
$env,
32-
$server,
33-
$get,
34-
$post,
35-
$files,
36-
$cookie,
37-
$session
38-
);
36+
Protection::overrideInternals($globals);
3937

40-
self::assertInstanceOf(ProtectedGlobal::class, $env);
38+
self::assertInstanceOf(ProtectedGlobal::class, $_ENV);
4139
self::assertEquals("somevalue", $env["somekey"]);
4240
}
4341

4442
public function testWhitelist() {
4543
$env = ["somekey" => "somevalue", "anotherkey" => "anothervalue"];
46-
$server = [];
47-
$get = [];
48-
$post = [];
49-
$files = [];
50-
$cookie = [];
51-
$session = [];
5244
$globals = [
5345
"_ENV" => $env,
5446
];
55-
Protection::removeGlobals(
47+
$whitelist = Protection::removeGlobals(
5648
$globals,
5749
[
5850
"_ENV" => [
5951
"anotherkey",
6052
],
6153
]
6254
);
63-
Protection::overrideInternals(
64-
$globals,
65-
$env,
66-
$server,
67-
$get,
68-
$post,
69-
$files,
70-
$cookie,
71-
$session
72-
);
55+
Protection::overrideInternals($whitelist);
7356

74-
self::assertEquals("anothervalue", $env["anotherkey"]);
57+
self::assertEquals("anothervalue", $_ENV["anotherkey"]);
7558
self::expectException(ProtectedGlobalException::class);
76-
$variable = $env["somevalue"];
59+
$value = $_ENV["somevalue"];
7760
}
7861

7962
public function testWhitelistMany() {
@@ -93,7 +76,7 @@ public function testWhitelistMany() {
9376

9477
Protection::removeGlobals($env);
9578
Protection::removeGlobals($server);
96-
$fixedGlobals = Protection::removeGlobals(
79+
$whitelisted = Protection::removeGlobals(
9780
$globals,
9881
[
9982
"_GET" => [
@@ -108,55 +91,12 @@ public function testWhitelistMany() {
10891

10992
);
11093

111-
Protection::overrideInternals(
112-
$fixedGlobals,
113-
$env,
114-
$server,
115-
$get,
116-
$post,
117-
$files,
118-
$cookie,
119-
$session
120-
);
94+
Protection::overrideInternals($whitelisted);
12195

122-
self::assertEquals("Y2K", $get["name"]);
123-
self::assertEquals("postvalue2", $post["postkey2"]);
96+
self::assertEquals("Y2K", $_GET["name"]);
97+
self::assertEquals("postvalue2", $_POST["postkey2"]);
12498
self::expectException(ProtectedGlobalException::class);
125-
$variable = $post["postkey1"];
126-
}
127-
128-
public function testWhitelistNotExists() {
129-
$env = [];
130-
$server = [];
131-
$get = ["name" => "Cody", "species" => "Feline"];
132-
$post = [];
133-
$files = [];
134-
$cookie = [];
135-
$session = [];
136-
$globals = [
137-
"_GET" => $get,
138-
];
139-
$globals = Protection::removeGlobals(
140-
$globals,
141-
[
142-
"_GET" => [
143-
"name",
144-
"age",
145-
],
146-
]
147-
);
148-
Protection::overrideInternals(
149-
$globals,
150-
$env,
151-
$server,
152-
$get,
153-
$post,
154-
$files,
155-
$cookie,
156-
$session
157-
);
158-
159-
self::assertEquals("Cody", $get["name"]);
160-
self::assertNull($get["age"]);
99+
$variable = $_POST["postkey1"];
100+
var_dump($variable);
161101
}
162102
}

test/phpunit/phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<phpunit colors="true">
2+
<phpunit colors="true" processIsolation="true">
33
<testsuites>
44
<testsuite name="main">
55
<directory suffix="Test.php">.</directory>

0 commit comments

Comments
 (0)