Skip to content

Commit 4456a77

Browse files
committed
add \Astrotomic\PhpunitAssertions\UrlAssertions::assertQuery()
1 parent 81f2bd7 commit 4456a77

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ This will prevent any method name conflicts with core, your custom or other trai
132132
\Astrotomic\PhpunitAssertions\UrlAssertions::assertScheme('https', 'https://astrotomic.info');
133133
\Astrotomic\PhpunitAssertions\UrlAssertions::assertHost('astrotomic.info', 'https://astrotomic.info');
134134
\Astrotomic\PhpunitAssertions\UrlAssertions::assertPath('/contributor/gummibeer/', 'https://astrotomic.info/contributor/gummibeer/');
135+
\Astrotomic\PhpunitAssertions\UrlAssertions::assertQuery(['_' => '123', 'q' => 'search'], 'https://astrotomic.info?q=search&_=123');
135136
\Astrotomic\PhpunitAssertions\UrlAssertions::assertComponent('gummibeer', 'https://gummibeer@astrotomic.info', PHP_URL_USER);
136137
```
137138

src/UrlAssertions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ public static function assertPath(string $expected, $actual): void
2727
self::assertComponent($expected, $actual, PHP_URL_PATH);
2828
}
2929

30+
public static function assertQuery(array $expected, $actual): void
31+
{
32+
$queryStr = parse_url($actual, PHP_URL_QUERY);
33+
PHPUnit::assertIsString($queryStr);
34+
35+
$query = [];
36+
parse_str($queryStr, $query);
37+
PHPUnit::assertIsArray($query);
38+
39+
ArrayAssertions::assertEquals($expected, $query);
40+
}
41+
3042
public static function assertComponent($expected, $actual, int $component): void
3143
{
3244
self::assertValidLoose($actual);

tests/UrlAssertionsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,19 @@ public static function it_can_validate_path(): void
5656

5757
UrlAssertions::assertPath($path, 'https://'.self::randomString().'.com'.$path);
5858
}
59+
60+
/**
61+
* @test
62+
* @dataProvider hundredTimes
63+
*/
64+
public static function it_can_validate_query(): void
65+
{
66+
$query = [
67+
self::randomString(2) => self::randomString(),
68+
self::randomString(2) => self::randomString(),
69+
self::randomString(2) => self::randomString(),
70+
];
71+
72+
UrlAssertions::assertQuery($query, 'https://'.self::randomString().'.com?'.http_build_query($query));
73+
}
5974
}

0 commit comments

Comments
 (0)