Skip to content

Commit

Permalink
fix: Allows get by numeric key
Browse files Browse the repository at this point in the history
  • Loading branch information
neznaika0 committed Nov 4, 2024
1 parent 3435872 commit 09b7935
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions system/HTTP/RequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function setGlobal(string $name, $value)
*
* @param string $name Supergrlobal name (lowercase)
* @phpstan-param 'get'|'post'|'request'|'cookie'|'server' $name
* @param array|string|null $index
* @param array|int|string|null $index
* @param int|null $filter Filter constant
* @param array|int|null $flags Options
*
Expand Down Expand Up @@ -290,7 +290,7 @@ public function fetchGlobal(string $name, $index = null, ?int $filter = null, $f
}

// Does the index contain array notation?
if (($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
if (is_string($index) && ($count = preg_match_all('/(?:^[^\[]+)|\[[^]]*\]/', $index, $matches)) > 1) {
$value = $this->globals[$name];

for ($i = 0; $i < $count; $i++) {
Expand Down
15 changes: 15 additions & 0 deletions tests/system/HTTP/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ public function testFetchGlobalReturnsArrayValues(): void
$this->assertCount(2, $result['ANNOUNCEMENTS']);
}

public function testFetchGlobalReturnsWithListValues(): void
{
$post = [
['foo' => 0],
['bar' => 1],
['baz' => 2],
];

$this->request->setGlobal('post', $post);
$result = $this->request->fetchGlobal('post');

$this->assertIsList($result);
$this->assertSame($post, $result);
}

public function testFetchGlobalWithArrayTop(): void
{
$post = [
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Test/FeatureTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,26 @@ public function testCallWithJsonRequest(): void
$response->assertJSONExact($data);
}

public function testCallWithListJsonRequest(): void
{
$this->withRoutes([
[
'POST',
'home',
'\Tests\Support\Controllers\Popcorn::echoJson',
],
]);
$data = [
['one' => 1, 'two' => 2],
['one' => 3, 'two' => 4],
];
$response = $this->withBodyFormat('json')
->call(Method::POST, 'home', $data);

$response->assertOK();
$response->assertJSONExact($data);
}

public function testSetupRequestBodyWithParams(): void
{
$request = $this->setupRequest('post', 'home');
Expand Down

0 comments on commit 09b7935

Please sign in to comment.