Skip to content

Commit b6bd02a

Browse files
committed
add url assertions
1 parent 1f16a24 commit b6bd02a

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/UrlAssertions.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions;
4+
5+
use PHPUnit\Framework\Assert as PHPUnit;
6+
7+
trait UrlAssertions
8+
{
9+
public static function assertValidLoose($actual): void
10+
{
11+
PHPUnit::assertIsString($actual);
12+
PHPUnit::assertNotFalse(filter_var($actual, FILTER_VALIDATE_URL));
13+
}
14+
15+
public static function assertSameScheme(string $expected, $actual): void
16+
{
17+
self::assertSameComponent($expected, $actual, PHP_URL_SCHEME);
18+
}
19+
20+
public static function assertSameHost(string $expected, $actual): void
21+
{
22+
self::assertSameComponent($expected, $actual, PHP_URL_HOST);
23+
}
24+
25+
public static function assertSamePath(string $expected, $actual): void
26+
{
27+
self::assertSameComponent($expected, $actual, PHP_URL_PATH);
28+
}
29+
30+
public static function assertSameComponent($expected, $actual, $component): void
31+
{
32+
self::assertValidLoose($actual);
33+
PHPUnit::assertSame($expected, parse_url($actual, $component));
34+
}
35+
}

tests/UrlAssertionsTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests;
4+
5+
use Astrotomic\PhpunitAssertions\UrlAssertions;
6+
7+
final class UrlAssertionsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @dataProvider hundredTimes
12+
*/
13+
public static function it_can_validate_loose(): void
14+
{
15+
UrlAssertions::assertValidLoose('https://'.self::randomString().'.com');
16+
}
17+
18+
/**
19+
* @test
20+
* @dataProvider hundredTimes
21+
*/
22+
public static function it_can_validate_scheme(): void
23+
{
24+
$scheme = self::randomElement([
25+
'http',
26+
'https',
27+
'ftp',
28+
'ssh',
29+
'file',
30+
'git',
31+
'imap',
32+
'irc',
33+
]);
34+
35+
UrlAssertions::assertSameScheme($scheme, $scheme.'://'.self::randomString().'.com');
36+
}
37+
38+
/**
39+
* @test
40+
* @dataProvider hundredTimes
41+
*/
42+
public static function it_can_validate_host(): void
43+
{
44+
$host = self::randomString().'.com';
45+
46+
UrlAssertions::assertSameHost($host, 'https://'.$host);
47+
}
48+
49+
/**
50+
* @test
51+
* @dataProvider hundredTimes
52+
*/
53+
public static function it_can_validate_path(): void
54+
{
55+
$path = '/'.self::randomString().'/'.self::randomString();
56+
57+
UrlAssertions::assertSamePath($path, 'https://'.self::randomString().'.com'.$path);
58+
}
59+
}

0 commit comments

Comments
 (0)