aboutsummaryrefslogtreecommitdiff
path: root/tests/UrlTest.php
blob: 72b9ac4c4787167fcab555cbfd31d77955f0b84b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

declare(strict_types=1);

namespace RssBridge\Tests;

use PHPUnit\Framework\TestCase;
use Url;

class UrlTest extends TestCase
{
    public function testBasicUsages()
    {
        $urls = [
            'http://example.com/',
            'http://example.com:9000/',
            'https://example.com/',
            'https://example.com/?foo',
            'https://example.com/?foo=bar',
        ];
        foreach ($urls as $url) {
            $this->assertSame($url, Url::fromString($url)->__toString());
        }
    }

    public function testNormalization()
    {
        $urls = [
            'http://example.com' => 'http://example.com/',
            'https://example.com/?' => 'https://example.com/',
            'https://example.com/foo?' => 'https://example.com/foo',
            'http://example.com:80/' => 'http://example.com/',
        ];
        foreach ($urls as $from => $to) {
            $this->assertSame($to, Url::fromString($from)->__toString());
        }
    }

    public function testIllegalPath()
    {
        $this->expectException(\UrlException::class);
        Url::fromString('https://example.com//foo');
    }

    public function testMutation()
    {
        $this->assertSame('http://example.com/foo', (Url::fromString('http://example.com/'))->withPath('/foo')->__toString());
        $this->assertSame('http://example.com/foo?a=b', (Url::fromString('http://example.com/?a=b'))->withPath('/foo')->__toString());
        $this->assertSame('http://example.com/', (Url::fromString('http://example.com/'))->withPath('/')->__toString());
        $this->assertSame('http://example.com/qqq?foo=bar', (Url::fromString('http://example.com/qqq'))->withQueryString('foo=bar')->__toString());
        $this->assertSame('http://example.net/qqq?foo=bar', (Url::fromString('http://example.com/qqq?foo=bar'))->withHost('example.net')->__toString());
    }
}