AddressTest::testScheme()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 14
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
3
use Codeception\Test\Unit;
4
use veejay\address\Address;
5
6
class AddressTest extends Unit
7
{
8
    /**
9
     * Key - input data (absolute url)
10
     * Value - relative part of url
11
     * @var array
12
     */
13
    private $data = [
14
        'http://domain.ru:80/index.php?param=1#anchor' => '/index.php?param=1#anchor',
15
        'http://domain.ru:80/index.php?param=1' => '/index.php?param=1',
16
        'http://domain.ru:80/index.php' => '/index.php',
17
        'http://domain.ru:80/' => '/',
18
    ];
19
20
    public function testUrl()
21
    {
22
        foreach ($this->data as $abs => $rel) {
23
            $address = new Address($abs);
24
            $this->assertEquals($abs, $address->abs());
25
            $this->assertEquals($rel, $address->rel());
26
        }
27
    }
28
29
    public function testCustom()
30
    {
31
        $address = new Address('http://domain.ru:80/index.php?param=1#anchor');
32
33
        $a = clone $address;
34
        $a->scheme = null;
35
        $this->assertEquals('/index.php?param=1#anchor', $a->abs());
36
37
        $a = clone $address;
38
        $a->host = null;
39
        $this->assertEquals('/index.php?param=1#anchor', $a->abs());
40
41
        $a = clone $address;
42
        $a->port = null;
43
        $this->assertEquals('http://domain.ru/index.php?param=1#anchor', $a->abs());
44
45
        $a = clone $address;
46
        $a->path = null;
47
        $this->assertEquals('http://domain.ru:80/?param=1#anchor', $a->abs());
48
49
        $a = clone $address;
50
        $a->query = null;
51
        $this->assertEquals('http://domain.ru:80/index.php#anchor', $a->abs());
52
53
        $a = clone $address;
54
        $a->fragment = null;
55
        $this->assertEquals('http://domain.ru:80/index.php?param=1', $a->abs());
56
    }
57
58
    public function testScheme()
59
    {
60
        $a = new Address('');
61
62
        $a->scheme = 'http';        $this->assertEquals('http', $a->scheme);
63
        $a->scheme = [];            $this->assertEquals('http', $a->scheme);
64
        $a->scheme = true;          $this->assertEquals('http', $a->scheme);
65
        $a->scheme = '';            $this->assertNull($a->scheme);
66
        $a->scheme = 'http';
67
        $a->scheme = false;         $this->assertNull($a->scheme);
68
        $a->scheme = 'http';
69
        $a->scheme = null;          $this->assertNull($a->scheme);
70
        $a->scheme = 'http';
71
        $a->scheme = 0;             $this->assertNull($a->scheme);
72
    }
73
74
    public function testHost()
75
    {
76
        $a = new Address('');
77
78
        $a->host = 'http';          $this->assertEquals('http', $a->host);
79
        $a->host = [];              $this->assertEquals('http', $a->host);
80
        $a->host = true;            $this->assertEquals('http', $a->host);
81
        $a->host = '';              $this->assertNull($a->host);
82
        $a->host = 'http';
83
        $a->host = false;           $this->assertNull($a->host);
84
        $a->host = 'http';
85
        $a->host = null;            $this->assertNull($a->host);
86
        $a->host = 'http';
87
        $a->host = 0;               $this->assertNull($a->host);
88
    }
89
90
    public function testPort()
91
    {
92
        $a = new Address('');
93
94
        $a->port = 80;              $this->assertEquals(80, $a->port);
95
        $a->port = 'http';          $this->assertNull($a->port);
96
        $a->port = 80;
97
        $a->port = [];              $this->assertNull($a->port);
98
        $a->port = true;            $this->assertEquals(1, $a->port);
99
        $a->port = false;           $this->assertNull($a->port);
100
        $a->port = 80;
101
        $a->port = null;            $this->assertNull($a->port);
102
        $a->port = 80;
103
        $a->port = 0;               $this->assertNull($a->port);
104
    }
105
106
    public function testPath()
107
    {
108
        $a = new Address('');
109
110
        $a->path = 'http';          $this->assertEquals('http', $a->path);
111
        $a->path = [];              $this->assertEquals('http', $a->path);
112
        $a->path = true;            $this->assertEquals('http', $a->path);
113
        $a->path = '';              $this->assertNull($a->path);
114
        $a->path = 'http';
115
        $a->path = false;           $this->assertNull($a->path);
116
        $a->path = 'http';
117
        $a->path = null;            $this->assertNull($a->path);
118
        $a->path = 'http';
119
        $a->path = 0;               $this->assertNull($a->path);
120
    }
121
122
    public function testQuery()
123
    {
124
        $a = new Address('');
125
        $array = ['q' => 1];
126
127
        $a->query = $array;         $this->assertEquals($array, $a->query);
128
        $a->query = true;           $this->assertEquals($array, $a->query);
129
        $a->query = 'q=2';          $this->assertEquals($array, $a->query);
130
        $a->query = '';             $this->assertEquals([], $a->query);
131
        $a->query = $array;
132
        $a->query = false;          $this->assertEquals([], $a->query);
133
        $a->query = $array;
134
        $a->query = null;           $this->assertEquals([], $a->query);
135
        $a->query = $array;
136
        $a->query = 0;              $this->assertEquals([], $a->query);
137
    }
138
139
    public function testFragment()
140
    {
141
        $a = new Address('');
142
143
        $a->fragment = 'q';         $this->assertEquals('q', $a->fragment);
144
        $a->fragment = true;        $this->assertEquals('q', $a->fragment);
145
        $a->fragment = '';          $this->assertNull($a->fragment);
146
        $a->fragment = 'q';
147
        $a->fragment = false;       $this->assertNull($a->fragment);
148
        $a->fragment = 'q';
149
        $a->fragment = null;        $this->assertNull($a->fragment);
150
        $a->fragment = 'q';
151
        $a->fragment = 0;           $this->assertNull($a->fragment);
152
    }
153
}
154