-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUrlTest.php
136 lines (112 loc) · 4.37 KB
/
UrlTest.php
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* This file is part of https://github.com/josantonius/php-url repository.
*
* (c) Josantonius <hello@josantonius.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
*/
namespace Josantonius\Url\Tests;
use Josantonius\Url\Url;
use PHPUnit\Framework\TestCase;
class UrlTest extends TestCase
{
public function test_should_set_the_current_url()
{
$currentUrl =
'https://' .
$_SERVER['SERVER_NAME'] . ':' .
$_SERVER['SERVER_PORT'] .
$_SERVER['REQUEST_URL'];
$this->assertEquals($currentUrl, (new Url())->full);
}
/**
* @runInSeparateProcess
*/
public function test_should_not_contain_common_http_ports()
{
$_SERVER['SERVER_PORT'] = 80;
$this->assertStringNotContainsString('80', (new Url())->full);
$_SERVER['SERVER_PORT'] = 443;
$this->assertStringNotContainsString('80', (new Url())->full);
}
/**
* @runInSeparateProcess
*/
public function test_should_contain_valid_scheme_when_is_secure_site()
{
$this->assertStringContainsString('https:', (new Url())->full);
}
/**
* @runInSeparateProcess
*/
public function test_should_contain_valid_scheme_when_is_non_secure_site()
{
$_SERVER['HTTPS'] = 'off';
$this->assertStringContainsString('http:', (new Url())->full);
}
public function test_should_set_all_properties()
{
$url = new Url(
'https://user:pass@sub.domain.com:90/en/web/docs/search.php?tag=bug&order=asc#top'
);
$this->assertEquals('https', $url->scheme);
$this->assertEquals('user', $url->username);
$this->assertEquals('pass', $url->password);
$this->assertEquals('sub.domain.com', $url->host);
$this->assertEquals(90, $url->port);
$this->assertEquals('user:pass@sub.domain.com:90', $url->authority);
$this->assertEquals('https://sub.domain.com:90', $url->base);
$this->assertEquals('/en/web/docs/search.php', $url->path);
$this->assertEquals('search.php', $url->basename);
$this->assertEquals('/en/web/docs', $url->dirname);
$this->assertEquals(["en", "web", "docs", "search.php"], $url->segments);
$this->assertEquals('php', $url->extension);
$this->assertEquals('search', $url->filename);
$this->assertEquals('tag=bug&order=asc', $url->query);
$this->assertEquals(["tag" => "bug", "order" => "asc"], $url->parameters);
$this->assertEquals('top', $url->fragment);
$this->assertEquals('#top', $url->hash);
}
public function test_should_set_nonexistent_properties_as_empty()
{
$url = new Url('');
$this->assertEquals('', $url->scheme);
$this->assertEquals('', $url->username);
$this->assertEquals('', $url->password);
$this->assertEquals('', $url->host);
$this->assertEquals('', $url->port);
$this->assertEquals('', $url->authority);
$this->assertEquals('', $url->base);
$this->assertEquals('', $url->path);
$this->assertEquals('', $url->basename);
$this->assertEquals('', $url->dirname);
$this->assertEquals([], $url->segments);
$this->assertEquals('', $url->extension);
$this->assertEquals('', $url->filename);
$this->assertEquals('', $url->query);
$this->assertEquals([], $url->parameters);
$this->assertEquals('', $url->fragment);
$this->assertEquals('', $url->hash);
}
public function test_authority_component_should_be_set_up_without_the_port()
{
$url = new Url('https://user:pass@sub.domain.com');
$this->assertEquals('user:pass@sub.domain.com', $url->authority);
}
public function test_authority_component_should_be_set_up_without_the_user_info()
{
$url = new Url('https://sub.domain.com');
$this->assertEquals('sub.domain.com', $url->authority);
}
public function test_should_not_ignore_the_port_even_if_it_is_common_when_the_url_is_custom()
{
$url = new Url('https://user:pass@sub.domain.com:80');
$this->assertEquals(80, $url->port);
$url = new Url('https://user:pass@sub.domain.com:443');
$this->assertEquals(443, $url->port);
}
}