|
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase;
|
6 | 6 | use PHPUnit\Framework\Attributes\DataProvider;
|
| 7 | +use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; |
7 | 8 | // use PHPUnit\Framework\Attributes\BackupGlobals;
|
8 | 9 | // use PHPUnit\Framework\Attributes\RunTestsInSeparateProcess;
|
9 | 10 |
|
@@ -80,6 +81,62 @@ public function testTestValidSSHKey(bool $expected, string $key)
|
80 | 81 | $this->assertEquals($expected, UnitySite::testValidSSHKey($key));
|
81 | 82 | }
|
82 | 83 |
|
| 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 | + |
83 | 140 | // I suspect that this test could have unexpected interactions with other tests.
|
84 | 141 | // even with RunTestsInSeparateProcess and BackupGlobalState, http_response_code()
|
85 | 142 | // still persists to the next test. header("HTTP/1.1 false") puts it back to its
|
|
0 commit comments