Passed
Pull Request — master (#17)
by Mihail
15:10
created

UriSerializationTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 51
c 1
b 0
f 1
dl 0
loc 85
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_json_serialization_with_slash_path() 0 13 1
A test_json_serialization_with_no_path() 0 12 1
A test_json_serialization_with_standard_port() 0 14 1
A test_json_serialization_with_all_the_things() 0 18 1
A test_json_serialization_with_username_and_host() 0 14 1
1
<?php
2
3
namespace Tests\Koded\Http;
4
5
use Koded\Http\Uri;
6
use PHPUnit\Framework\TestCase;
7
8
class UriSerializationTest extends TestCase
9
{
10
    public function test_json_serialization_with_all_the_things()
11
    {
12
        $uri = new Uri('https://username:[email protected]:8080/foo/bar?a=1234&b=5678#baz');
13
;
14
        $this->assertSame([
15
            'scheme' => 'https',
16
            'host' => 'example.com',
17
            'port' => 8080,
18
            'path' => '/foo/bar',
19
            'user' => 'username',
20
            'pass' => 'password',
21
            'fragment' => 'baz',
22
            'query' => 'a=1234&b=5678'
23
        ], $uri->jsonSerialize());
24
25
        $this->assertJsonStringEqualsJsonString(
26
            '{"scheme":"https","host":"example.com","port":8080,"path":"/foo/bar","user":"username","pass":"password","fragment":"baz","query":"a=1234&b=5678"}',
27
            json_encode($uri, JSON_UNESCAPED_SLASHES)
28
        );
29
    }
30
31
    public function test_json_serialization_with_username_and_host()
32
    {
33
        $uri = new Uri('http://[email protected]');
34
35
        $this->assertSame([
36
            'scheme' => 'http',
37
            'host' => 'example.com',
38
            'path' => '/',
39
            'user' => 'username',
40
        ], $uri->jsonSerialize());
41
42
        $this->assertJsonStringEqualsJsonString(
43
            '{"scheme":"http","host":"example.com","path":"/","user":"username"}',
44
            json_encode($uri, JSON_UNESCAPED_SLASHES)
45
        );
46
    }
47
48
    public function test_json_serialization_with_no_path()
49
    {
50
        $uri = new Uri('https://example.com');
51
52
        $this->assertSame([
53
            'scheme' => 'https',
54
            'host' => 'example.com',
55
        ], $uri->jsonSerialize());
56
57
        $this->assertJsonStringEqualsJsonString(
58
            '{"scheme":"https","host":"example.com"}',
59
            json_encode($uri, JSON_UNESCAPED_SLASHES)
60
        );
61
    }
62
63
    public function test_json_serialization_with_slash_path()
64
    {
65
        $uri = new Uri('https://example.com/');
66
67
        $this->assertSame([
68
            'scheme' => 'https',
69
            'host' => 'example.com',
70
            'path' => '/'
71
        ], $uri->jsonSerialize());
72
73
        $this->assertJsonStringEqualsJsonString(
74
            '{"scheme":"https","host":"example.com","path":"/"}',
75
            json_encode($uri, JSON_UNESCAPED_SLASHES)
76
        );
77
    }
78
79
    public function test_json_serialization_with_standard_port()
80
    {
81
        $uri = new Uri('https://example.com:21');
82
83
        $this->assertSame([
84
            'scheme' => 'https',
85
            'host' => 'example.com',
86
        ], $uri->jsonSerialize(),
87
            'The standard port is omitted'
88
        );
89
90
        $this->assertJsonStringEqualsJsonString(
91
            '{"scheme":"https","host":"example.com"}',
92
            json_encode($uri, JSON_UNESCAPED_SLASHES)
93
        );
94
    }
95
}
96