diff --git a/resources/lib/UnitySite.php b/resources/lib/UnitySite.php
index b1809d50..bb0ee055 100644
--- a/resources/lib/UnitySite.php
+++ b/resources/lib/UnitySite.php
@@ -7,7 +7,7 @@
class UnitySite
{
- public static function die($x = null)
+ public static function die($x = null, $show_user = false)
{
if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) {
if (is_null($x)) {
@@ -16,10 +16,10 @@ public static function die($x = null)
throw new PhpUnitNoDieException($x);
}
} else {
- if (is_null($x)) {
- die();
- } else {
+ if (!is_null($x) and $show_user) {
die($x);
+ } else {
+ die();
}
}
}
@@ -27,7 +27,7 @@ public static function die($x = null)
public static function redirect($destination)
{
header("Location: $destination");
- self::die("Redirect failed, click here to continue.");
+ self::die("Redirect failed, click here to continue.", true);
}
private static function headerResponseCode(int $code, string $reason)
@@ -55,14 +55,14 @@ public static function badRequest($message)
{
self::headerResponseCode(400, "bad request");
self::errorLog("bad request", $message);
- self::die();
+ self::die($message);
}
public static function forbidden($message)
{
self::headerResponseCode(403, "forbidden");
self::errorLog("forbidden", $message);
- self::die();
+ self::die($message);
}
public static function arrayGetOrBadRequest(array $array, ...$keys)
diff --git a/test/unit/UnitySiteTest.php b/test/unit/UnitySiteTest.php
index 726f788a..796f1dd2 100644
--- a/test/unit/UnitySiteTest.php
+++ b/test/unit/UnitySiteTest.php
@@ -4,6 +4,7 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;
+use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
// use PHPUnit\Framework\Attributes\BackupGlobals;
// use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
@@ -80,6 +81,62 @@ public function testTestValidSSHKey(bool $expected, string $key)
$this->assertEquals($expected, UnitySite::testValidSSHKey($key));
}
+ public function testArrayGetOrBadRequestReturnsValueWhenKeyExists()
+ {
+ $array = [
+ 'a' => [
+ 'b' => [
+ 'c' => 123
+ ]
+ ]
+ ];
+ $result = UnitySite::arrayGetOrBadRequest($array, 'a', 'b', 'c');
+ $this->assertSame(123, $result);
+ }
+
+ public function testArrayGetOrBadRequestReturnsArrayWhenTraversingPartially()
+ {
+ $array = [
+ 'foo' => [
+ 'bar' => 'baz'
+ ]
+ ];
+ $result = UnitySite::arrayGetOrBadRequest($array, 'foo');
+ $this->assertSame(['bar' => 'baz'], $result);
+ }
+
+ public function testArrayGetOrBadRequestThrowsOnMissingKeyFirstLevel()
+ {
+ $array = ['x' => 1];
+ $this->expectException(PhpUnitNoDieException::class);
+ $this->expectExceptionMessage('["y"]');
+ UnitySite::arrayGetOrBadRequest($array, 'y');
+ }
+
+ public function testArrayGetOrBadRequestThrowsOnMissingKeyNested()
+ {
+ $array = ['a' => []];
+ $this->expectException(PhpUnitNoDieException::class);
+ // Should include both levels
+ $this->expectExceptionMessage('["a","b"]');
+ UnitySite::arrayGetOrBadRequest($array, 'a', 'b');
+ }
+
+ public function testArrayGetOrBadRequestThrowsWhenValueIsNullButKeyNotSet()
+ {
+ $array = ['a' => null];
+ $this->expectException(PhpUnitNoDieException::class);
+ $this->expectExceptionMessage('["a"]');
+ UnitySite::arrayGetOrBadRequest($array, 'a');
+ }
+
+ public function testArrayGetOrBadRequestReturnsValueWhenValueIsFalsyButSet()
+ {
+ $array = ['a' => 0];
+ $result = UnitySite::arrayGetOrBadRequest($array, 'a');
+ $this->assertSame(0, $result);
+ }
+
// I suspect that this test could have unexpected interactions with other tests.
// even with RunTestsInSeparateProcess and BackupGlobalState, http_response_code()
// still persists to the next test. header("HTTP/1.1 false") puts it back to its