Skip to content

Commit facedff

Browse files
committed
move arrayGet from UnitySite to utils
1 parent e20c65a commit facedff

File tree

6 files changed

+88
-99
lines changed

6 files changed

+88
-99
lines changed

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
require_once __DIR__ . "/lib/UnityWebhook.php";
2525
require_once __DIR__ . "/lib/UnityRedis.php";
2626
require_once __DIR__ . "/lib/UnityGithub.php";
27+
require_once __DIR__ . "/lib/utils.php";
2728
require_once __DIR__ . "/lib/exceptions/NoDieException.php";
2829
require_once __DIR__ . "/lib/exceptions/SSOException.php";
2930
require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php";

resources/lib/UnitySite.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,10 @@ public static function shutdown()
106106
);
107107
}
108108

109-
public static function arrayGet($array, ...$keys)
110-
{
111-
$cursor = $array;
112-
$keysTraversed = [];
113-
foreach ($keys as $key) {
114-
array_push($keysTraversed, $key);
115-
if (!isset($cursor[$key])) {
116-
throw new ArrayKeyException(
117-
"key not found: \$array" .
118-
// [1, 2, "foo"] => [1][2]["foo"]
119-
implode("", array_map(fn($x) => json_encode([$x]), $keysTraversed))
120-
);
121-
}
122-
$cursor = $cursor[$key];
123-
}
124-
return $cursor;
125-
}
126-
127109
public static function getPostData(...$keys)
128110
{
129111
try {
130-
return self::arrayGet($_POST, ...$keys);
112+
return \arrayGet($_POST, ...$keys);
131113
} catch (ArrayKeyException $e) {
132114
self::badRequest(strval($e));
133115
}
@@ -136,7 +118,7 @@ public static function getPostData(...$keys)
136118
public static function getUploadedFileContents($filename, $do_delete_tmpfile_after_read = true)
137119
{
138120
try {
139-
$tmpfile_path = self::arrayGet($_FILES, $filename, "tmp_name");
121+
$tmpfile_path = \arrayGet($_FILES, $filename, "tmp_name");
140122
} catch (ArrayKeyException $e) {
141123
self::badRequest(strval($e));
142124
}

resources/lib/utils.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use UnityWebPortal\lib\exceptions\ArrayKeyException;
4+
5+
function arrayGet($array, ...$keys)
6+
{
7+
$cursor = $array;
8+
$keysTraversed = [];
9+
foreach ($keys as $key) {
10+
array_push($keysTraversed, $key);
11+
if (!isset($cursor[$key])) {
12+
throw new ArrayKeyException(
13+
"key not found: \$array" .
14+
// [1, 2, "foo"] => [1][2]["foo"]
15+
implode("", array_map(fn($x) => json_encode([$x]), $keysTraversed))
16+
);
17+
}
18+
$cursor = $cursor[$key];
19+
}
20+
return $cursor;
21+
}

test/phpunit-bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require_once __DIR__ . "/../resources/lib/UnityWebhook.php";
1616
require_once __DIR__ . "/../resources/lib/UnityRedis.php";
1717
require_once __DIR__ . "/../resources/lib/UnityGithub.php";
18+
require_once __DIR__ . "/../resources/lib/utils.php";
1819
require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php";
1920
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";
2021
require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php";

test/unit/UnitySiteTest.php

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7-
use UnityWebPortal\lib\exceptions\ArrayKeyException;
87
// use PHPUnit\Framework\Attributes\BackupGlobals;
98
// use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
109

@@ -80,82 +79,4 @@ public function testTestValidSSHKey(bool $expected, string $key)
8079
{
8180
$this->assertEquals($expected, UnitySite::testValidSSHKey($key));
8281
}
83-
84-
public function testArrayGetReturnsValueWhenKeyExists()
85-
{
86-
$array = [
87-
"a" => [
88-
"b" => [
89-
"c" => 123
90-
]
91-
]
92-
];
93-
$result = UnitySite::arrayGet($array, "a", "b", "c");
94-
$this->assertSame(123, $result);
95-
}
96-
97-
public function testArrayGetReturnsArrayWhenTraversingPartially()
98-
{
99-
$array = [
100-
"foo" => [
101-
"bar" => "baz"
102-
]
103-
];
104-
$result = UnitySite::arrayGet($array, "foo");
105-
$this->assertSame(["bar" => "baz"], $result);
106-
}
107-
108-
public function testArrayGetThrowsOnMissingKeyFirstLevel()
109-
{
110-
$array = ["x" => 1];
111-
$this->expectException(ArrayKeyException::class);
112-
$this->expectExceptionMessage('$array["y"]');
113-
UnitySite::arrayGet($array, "y");
114-
}
115-
116-
public function testArrayGetThrowsOnMissingKeyNested()
117-
{
118-
$array = ["a" => []];
119-
$this->expectException(ArrayKeyException::class);
120-
// Should include both levels
121-
$this->expectExceptionMessage('$array["a"]["b"]');
122-
UnitySite::arrayGet($array, "a", "b");
123-
}
124-
125-
public function testArrayGetThrowsWhenValueIsNullButKeyNotSet()
126-
{
127-
$array = ["a" => null];
128-
$this->expectException(ArrayKeyException::class);
129-
$this->expectExceptionMessage('$array["a"]');
130-
UnitySite::arrayGet($array, "a");
131-
}
132-
133-
public function testArrayGetReturnsValueWhenValueIsFalsyButSet()
134-
{
135-
$array = ["a" => 0];
136-
$result = UnitySite::arrayGet($array, "a");
137-
$this->assertSame(0, $result);
138-
}
139-
140-
// I suspect that this test could have unexpected interactions with other tests.
141-
// even with RunTestsInSeparateProcess and BackupGlobalState, http_response_code()
142-
// still persists to the next test. header("HTTP/1.1 false") puts it back to its
143-
// initial value, but this is a hack and does not inspire confidence.
144-
// #[BackupGlobals(true)]
145-
// #[RunTestsInSeparateProcess]
146-
// public function testHeaderResponseCode()
147-
// {
148-
// $this->assertEquals(false, http_response_code());
149-
// $this->assertArrayNotHasKey("SERVER_PROTOCOL", $_SERVER);
150-
// try {
151-
// $_SERVER["SERVER_PROTOCOL"] = "HTTP/1.1";
152-
// UnitySite::headerResponseCode(400);
153-
// $this->assertEquals(400, http_response_code());
154-
// UnitySite::headerResponseCode(401);
155-
// $this->assertEquals(401, http_response_code());
156-
// } finally {
157-
// unset($_SERVER["SERVER_PROTOCOL"]);
158-
// header("HTTP/1.1 false");
159-
// }
160-
// }
16182
}

test/unit/UtilsTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
use UnityWebPortal\lib\exceptions\ArrayKeyException;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class UtilsTest extends TestCase
7+
{
8+
public function testArrayGetReturnsValueWhenKeyExists()
9+
{
10+
$array = [
11+
"a" => [
12+
"b" => [
13+
"c" => 123
14+
]
15+
]
16+
];
17+
$result = \arrayGet($array, "a", "b", "c");
18+
$this->assertSame(123, $result);
19+
}
20+
21+
public function testArrayGetReturnsArrayWhenTraversingPartially()
22+
{
23+
$array = [
24+
"foo" => [
25+
"bar" => "baz"
26+
]
27+
];
28+
$result = \arrayGet($array, "foo");
29+
$this->assertSame(["bar" => "baz"], $result);
30+
}
31+
32+
public function testArrayGetThrowsOnMissingKeyFirstLevel()
33+
{
34+
$array = ["x" => 1];
35+
$this->expectException(ArrayKeyException::class);
36+
$this->expectExceptionMessage('$array["y"]');
37+
\arrayGet($array, "y");
38+
}
39+
40+
public function testArrayGetThrowsOnMissingKeyNested()
41+
{
42+
$array = ["a" => []];
43+
$this->expectException(ArrayKeyException::class);
44+
// Should include both levels
45+
$this->expectExceptionMessage('$array["a"]["b"]');
46+
\arrayGet($array, "a", "b");
47+
}
48+
49+
public function testArrayGetThrowsWhenValueIsNullButKeyNotSet()
50+
{
51+
$array = ["a" => null];
52+
$this->expectException(ArrayKeyException::class);
53+
$this->expectExceptionMessage('$array["a"]');
54+
\arrayGet($array, "a");
55+
}
56+
57+
public function testArrayGetReturnsValueWhenValueIsFalsyButSet()
58+
{
59+
$array = ["a" => 0];
60+
$result = \arrayGet($array, "a");
61+
$this->assertSame(0, $result);
62+
}
63+
}

0 commit comments

Comments
 (0)