Skip to content

Commit 5f30f06

Browse files
authored
add tests for UnitySite::arrayGetOrBadRequest (#232)
* add tests for UnitySite::arrayGetOrBadRequest * more similar to original
1 parent 7a04343 commit 5f30f06

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

resources/lib/UnitySite.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class UnitySite
99
{
10-
public static function die($x = null)
10+
public static function die($x = null, $show_user = false)
1111
{
1212
if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) {
1313
if (is_null($x)) {
@@ -16,18 +16,18 @@ public static function die($x = null)
1616
throw new PhpUnitNoDieException($x);
1717
}
1818
} else {
19-
if (is_null($x)) {
20-
die();
21-
} else {
19+
if (!is_null($x) and $show_user) {
2220
die($x);
21+
} else {
22+
die();
2323
}
2424
}
2525
}
2626

2727
public static function redirect($destination)
2828
{
2929
header("Location: $destination");
30-
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.");
30+
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.", true);
3131
}
3232

3333
private static function headerResponseCode(int $code, string $reason)
@@ -55,14 +55,14 @@ public static function badRequest($message)
5555
{
5656
self::headerResponseCode(400, "bad request");
5757
self::errorLog("bad request", $message);
58-
self::die();
58+
self::die($message);
5959
}
6060

6161
public static function forbidden($message)
6262
{
6363
self::headerResponseCode(403, "forbidden");
6464
self::errorLog("forbidden", $message);
65-
self::die();
65+
self::die($message);
6666
}
6767

6868
public static function arrayGetOrBadRequest(array $array, ...$keys)

test/unit/UnitySiteTest.php

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

55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7+
use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
78
// use PHPUnit\Framework\Attributes\BackupGlobals;
89
// use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
910

@@ -80,6 +81,62 @@ public function testTestValidSSHKey(bool $expected, string $key)
8081
$this->assertEquals($expected, UnitySite::testValidSSHKey($key));
8182
}
8283

84+
public function testArrayGetOrBadRequestReturnsValueWhenKeyExists()
85+
{
86+
$array = [
87+
'a' => [
88+
'b' => [
89+
'c' => 123
90+
]
91+
]
92+
];
93+
$result = UnitySite::arrayGetOrBadRequest($array, 'a', 'b', 'c');
94+
$this->assertSame(123, $result);
95+
}
96+
97+
public function testArrayGetOrBadRequestReturnsArrayWhenTraversingPartially()
98+
{
99+
$array = [
100+
'foo' => [
101+
'bar' => 'baz'
102+
]
103+
];
104+
$result = UnitySite::arrayGetOrBadRequest($array, 'foo');
105+
$this->assertSame(['bar' => 'baz'], $result);
106+
}
107+
108+
public function testArrayGetOrBadRequestThrowsOnMissingKeyFirstLevel()
109+
{
110+
$array = ['x' => 1];
111+
$this->expectException(PhpUnitNoDieException::class);
112+
$this->expectExceptionMessage('["y"]');
113+
UnitySite::arrayGetOrBadRequest($array, 'y');
114+
}
115+
116+
public function testArrayGetOrBadRequestThrowsOnMissingKeyNested()
117+
{
118+
$array = ['a' => []];
119+
$this->expectException(PhpUnitNoDieException::class);
120+
// Should include both levels
121+
$this->expectExceptionMessage('["a","b"]');
122+
UnitySite::arrayGetOrBadRequest($array, 'a', 'b');
123+
}
124+
125+
public function testArrayGetOrBadRequestThrowsWhenValueIsNullButKeyNotSet()
126+
{
127+
$array = ['a' => null];
128+
$this->expectException(PhpUnitNoDieException::class);
129+
$this->expectExceptionMessage('["a"]');
130+
UnitySite::arrayGetOrBadRequest($array, 'a');
131+
}
132+
133+
public function testArrayGetOrBadRequestReturnsValueWhenValueIsFalsyButSet()
134+
{
135+
$array = ['a' => 0];
136+
$result = UnitySite::arrayGetOrBadRequest($array, 'a');
137+
$this->assertSame(0, $result);
138+
}
139+
83140
// I suspect that this test could have unexpected interactions with other tests.
84141
// even with RunTestsInSeparateProcess and BackupGlobalState, http_response_code()
85142
// still persists to the next test. header("HTTP/1.1 false") puts it back to its

0 commit comments

Comments
 (0)