Skip to content

add tests for UnitySite::arrayGetOrBadRequest #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions resources/lib/UnitySite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -16,18 +16,18 @@ 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();
}
}
}

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

private static function headerResponseCode(int $code, string $reason)
Expand Down Expand Up @@ -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)
Expand Down
57 changes: 57 additions & 0 deletions test/unit/UnitySiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down