-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUrlTest.php
More file actions
191 lines (167 loc) · 5.77 KB
/
UrlTest.php
File metadata and controls
191 lines (167 loc) · 5.77 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace ValueObjects\Tests\Web;
use ValueObjects\StringLiteral\StringLiteral;
use PHPUnit\Framework\TestCase;
use ValueObjects\Web\FragmentIdentifier;
use ValueObjects\Web\NullPortNumber;
use ValueObjects\Web\Path;
use ValueObjects\Web\PortNumber;
use ValueObjects\Web\QueryString;
use ValueObjects\Web\SchemeName;
use ValueObjects\Web\Url;
use ValueObjects\Web\Hostname;
use ValueObjects\ValueObjectInterface;
class UrlTest extends TestCase
{
/** @var Url */
protected $url;
public function setup()
{
$this->url = new Url(
new SchemeName('http'),
new StringLiteral('user'),
new StringLiteral('pass'),
new Hostname('foo.com'),
new PortNumber(80),
new Path('/bar'),
new QueryString('?querystring'),
new FragmentIdentifier('#fragmentidentifier')
);
}
public function fromNativeProviderValidUrls(){
return [
[ 'http://user:pass@foo.com:80/bar?querystring#fragmentidentifier' ],
[ 'http://www.test.com' ],
[ 'http://www.test.com/bar' ],
[ 'http://www.test.com/?querystring' ],
[ 'http://www.test.com/#fragmentidentifier' ]
];
}
/**
* @dataProvider fromNativeProviderValidUrls
*/
public function testFromNativeWithValidUrl($nativeUrlString)
{
$fromNativeUrl = Url::fromNative($nativeUrlString);
$this->assertSame($nativeUrlString, $fromNativeUrl->__toString());
}
public function fromNativeProviderInvalidUrls(){
return [
[ 'foo.com:80/bar?querystring#fragmentidentifier' ],
[ 'www.test.com' ],
[ 'notAUrl' ]
];
}
/**
* @dataProvider fromNativeProviderInvalidUrls
* @expectedException \InvalidArgumentException
*/
public function testFromNativeWithInvalidUrl($nativeUrlString)
{
Url::fromNative($nativeUrlString);
}
public function testSameValueAs()
{
$url2 = new Url(
new SchemeName('http'),
new StringLiteral('user'),
new StringLiteral('pass'),
new Hostname('foo.com'),
new PortNumber(80),
new Path('/bar'),
new QueryString('?querystring'),
new FragmentIdentifier('#fragmentidentifier')
);
$url3 = new Url(
new SchemeName('git+ssh'),
new StringLiteral(''),
new StringLiteral(''),
new Hostname('github.com'),
new NullPortNumber(),
new Path('/nicolopignatelli/valueobjects'),
new QueryString('?querystring'),
new FragmentIdentifier('#fragmentidentifier')
);
$this->assertTrue($this->url->sameValueAs($url2));
$this->assertTrue($url2->sameValueAs($this->url));
$this->assertFalse($this->url->sameValueAs($url3));
$mock = $this->getMockBuilder(ValueObjectInterface::class)
->getMock();
$this->assertFalse($this->url->sameValueAs($mock));
}
public function testGetDomain()
{
$domain = new Hostname('foo.com');
$this->assertTrue($this->url->getDomain()->sameValueAs($domain));
}
public function testGetFragmentIdentifier()
{
$fragment = new FragmentIdentifier('#fragmentidentifier');
$this->assertTrue($this->url->getFragmentIdentifier()->sameValueAs($fragment));
}
public function testGetPassword()
{
$password = new StringLiteral('pass');
$this->assertTrue($this->url->getPassword()->sameValueAs($password));
}
public function testGetPath()
{
$path = new Path('/bar');
$this->assertTrue($this->url->getPath()->sameValueAs($path));
}
public function testGetPort()
{
$port = new PortNumber(80);
$this->assertTrue($this->url->getPort()->sameValueAs($port));
}
public function testGetQueryString()
{
$queryString = new QueryString('?querystring');
$this->assertTrue($this->url->getQueryString()->sameValueAs($queryString));
}
public function testGetScheme()
{
$scheme = new SchemeName('http');
$this->assertTrue($this->url->getScheme()->sameValueAs($scheme));
}
public function testGetUser()
{
$user = new StringLiteral('user');
$this->assertTrue($this->url->getUser()->sameValueAs($user));
}
public function testToString()
{
$this->assertSame('http://user:pass@foo.com:80/bar?querystring#fragmentidentifier', $this->url->__toString());
}
public function testAuthlessUrlToString()
{
$nativeUrlString = 'http://foo.com:80/bar?querystring#fragmentidentifier';
$authlessUrl = new Url(
new SchemeName('http'),
new StringLiteral(''),
new StringLiteral(''),
new Hostname('foo.com'),
new PortNumber(80),
new Path('/bar'),
new QueryString('?querystring'),
new FragmentIdentifier('#fragmentidentifier')
);
$this->assertSame($nativeUrlString, $authlessUrl->__toString());
$fromNativeUrl = Url::fromNative($nativeUrlString);
$this->assertSame($nativeUrlString, Url::fromNative($authlessUrl)->__toString());
}
public function testNullPortUrlToString()
{
$nullPortUrl = new Url(
new SchemeName('http'),
new StringLiteral('user'),
new StringLiteral(''),
new Hostname('foo.com'),
new NullPortNumber(),
new Path('/bar'),
new QueryString('?querystring'),
new FragmentIdentifier('#fragmentidentifier')
);
$this->assertSame('http://user@foo.com/bar?querystring#fragmentidentifier', $nullPortUrl->__toString());
}
}