|
15 | 15 | use League\Uri\Components\Fragment; |
16 | 16 | use League\Uri\Exceptions\SyntaxError; |
17 | 17 | use PHPUnit\Framework\Attributes\DataProvider; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
18 | 19 | use PHPUnit\Framework\TestCase; |
19 | 20 | use Stringable; |
20 | 21 |
|
@@ -477,4 +478,43 @@ public static function queryProvider(): array |
477 | 478 | 'Encoded unreserved chars are not decoded' => ['q=v%61lue', 'q=value'], |
478 | 479 | ]; |
479 | 480 | } |
| 481 | + |
| 482 | + #[Test] |
| 483 | + #[DataProvider('queryWithInnerEmptyBracetsProvider')] |
| 484 | + public function it_should_parse_empty_bracets_issue_146(string $query, string $expected): void |
| 485 | + { |
| 486 | + $data = QueryString::extract($query); |
| 487 | + parse_str($query, $result); |
| 488 | + |
| 489 | + self::assertSame($data, $result); |
| 490 | + self::assertSame($expected, http_build_query($data, '', '&', PHP_QUERY_RFC3986)); |
| 491 | + } |
| 492 | + |
| 493 | + public static function queryWithInnerEmptyBracetsProvider(): iterable |
| 494 | + { |
| 495 | + yield 'query with on level empty bracets' => [ |
| 496 | + 'query' => 'foo[]=bar', |
| 497 | + 'expected' => 'foo%5B0%5D=bar', |
| 498 | + ]; |
| 499 | + |
| 500 | + yield 'query with two level bracets' => [ |
| 501 | + 'query' => 'key[][][foo][9]=bar', |
| 502 | + 'expected' => 'key%5B0%5D%5B0%5D%5Bfoo%5D%5B9%5D=bar', |
| 503 | + ]; |
| 504 | + |
| 505 | + yield 'query with invalid remaining; close bracet without an opening bracet' => [ |
| 506 | + 'query' => 'key[][]foo][9]=bar', |
| 507 | + 'expected' => 'key%5B0%5D%5B0%5D=bar', |
| 508 | + ]; |
| 509 | + |
| 510 | + yield 'query with invalid remaining; no opening bracet' => [ |
| 511 | + 'query' => 'key[]9=bar', |
| 512 | + 'expected' => 'key%5B0%5D=bar', |
| 513 | + ]; |
| 514 | + |
| 515 | + yield 'query with invalid remaining; opening bracet no at the start of the remaining string' => [ |
| 516 | + 'query' => 'key[]9[]=bar', |
| 517 | + 'expected' => 'key%5B0%5D=bar', |
| 518 | + ]; |
| 519 | + } |
480 | 520 | } |
0 commit comments