jwage /
purl
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Purl\Test; |
||
| 6 | |||
| 7 | use PHPUnit\Framework\TestCase; |
||
| 8 | use Purl\Fragment; |
||
| 9 | use Purl\ParserInterface; |
||
| 10 | use Purl\Path; |
||
| 11 | use Purl\Query; |
||
| 12 | use Purl\Url; |
||
| 13 | use function count; |
||
| 14 | |||
| 15 | class UrlTest extends TestCase |
||
| 16 | { |
||
| 17 | public function testConstruct() : void |
||
| 18 | { |
||
| 19 | $url = new Url(); |
||
| 20 | $url->setUrl('http://jwage.com'); |
||
| 21 | $this->assertEquals('http://jwage.com/', $url->getUrl()); |
||
| 22 | $this->assertInstanceOf('Purl\Parser', $url->getParser()); |
||
| 23 | |||
| 24 | $parser = new TestParser(); |
||
| 25 | $url = new Url('http://jwage.com', $parser); |
||
| 26 | $this->assertSame($parser, $url->getParser()); |
||
| 27 | } |
||
| 28 | |||
| 29 | public function testSetParser() : void |
||
| 30 | { |
||
| 31 | $parser = new TestParser(); |
||
| 32 | $url = new Url(); |
||
| 33 | $url->setParser($parser); |
||
| 34 | $this->assertSame($parser, $url->getParser()); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testParseSanity() : void |
||
| 38 | { |
||
| 39 | $url = new Url('https://host.com:443/path with spaces?param1 with spaces=value1 with spaces¶m2=value2#fragment1/fragment2 with spaces?param1=value1¶m2 with spaces=value2 with spaces'); |
||
| 40 | $this->assertEquals('https', $url->scheme); |
||
| 41 | $this->assertEquals('host.com', $url->host); |
||
| 42 | $this->assertEquals('443', $url->port); |
||
| 43 | $this->assertInstanceOf('Purl\Path', $url->path); |
||
| 44 | $this->assertEquals('/path%20with%20spaces', (string) $url->path); |
||
| 45 | $this->assertInstanceOf('Purl\Query', $url->query); |
||
| 46 | $this->assertEquals('param1_with_spaces=value1+with+spaces¶m2=value2', (string) $url->query); |
||
| 47 | $this->assertInstanceOf('Purl\Fragment', $url->fragment); |
||
| 48 | $this->assertEquals('fragment1/fragment2%20with%20spaces?param1=value1¶m2_with_spaces=value2+with+spaces', (string) $url->fragment); |
||
| 49 | $this->assertInstanceOf('Purl\Path', $url->fragment->path); |
||
| 50 | $this->assertInstanceOf('Purl\Query', $url->fragment->query); |
||
| 51 | $this->assertEquals('param1=value1¶m2_with_spaces=value2+with+spaces', (string) $url->fragment->query); |
||
| 52 | $this->assertEquals('fragment1/fragment2%20with%20spaces', (string) $url->fragment->path); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testParseStaticMethod() : void |
||
| 56 | { |
||
| 57 | $url = Url::parse('http://google.com'); |
||
| 58 | $this->assertInstanceOf('Purl\Url', $url); |
||
| 59 | $this->assertEquals('http://google.com/', (string) $url); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testBuild() : void |
||
| 63 | { |
||
| 64 | $url = Url::parse('http://jwage.com') |
||
| 65 | ->set('port', '443') |
||
| 66 | ->set('scheme', 'https'); |
||
| 67 | |||
| 68 | $url->query |
||
| 69 | ->set('param1', 'value1') |
||
| 70 | ->set('param2', 'value2'); |
||
| 71 | |||
| 72 | $url->path->add('about'); |
||
| 73 | $url->path->add('me'); |
||
| 74 | |||
| 75 | $url->fragment->path->add('fragment1'); |
||
| 76 | $url->fragment->path->add('fragment2'); |
||
| 77 | |||
| 78 | $url->fragment->query |
||
| 79 | ->set('param1', 'value1') |
||
| 80 | ->set('param2', 'value2'); |
||
| 81 | |||
| 82 | $this->assertEquals('https://jwage.com:443/about/me?param1=value1¶m2=value2#/fragment1/fragment2?param1=value1¶m2=value2', (string) $url); |
||
| 83 | } |
||
| 84 | |||
| 85 | public function testJoin() : void |
||
| 86 | { |
||
| 87 | $url = new Url('http://jwage.com/about?param=value#fragment'); |
||
| 88 | $this->assertEquals('http://jwage.com/about?param=value#fragment', (string) $url); |
||
| 89 | $url->join(new Url('http://about.me/jwage')); |
||
| 90 | $this->assertEquals('http://about.me/jwage?param=value#fragment', (string) $url); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testSetPath() : void |
||
| 94 | { |
||
| 95 | $url = new Url('http://jwage.com'); |
||
| 96 | $url->path = 'about'; |
||
|
0 ignored issues
–
show
|
|||
| 97 | $this->assertInstanceOf('Purl\Path', $url->path); |
||
| 98 | $this->assertEquals('about', (string) $url->path); |
||
| 99 | } |
||
| 100 | |||
| 101 | public function testGetPath() : void |
||
| 102 | { |
||
| 103 | $url = new Url('http://jwage.com'); |
||
| 104 | $url->path = 'about'; |
||
|
0 ignored issues
–
show
It seems like
'about' of type string is incompatible with the declared type Purl\Path of property $path.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 105 | $this->assertInstanceOf('Purl\Path', $url->path); |
||
| 106 | $this->assertEquals('about', (string) $url->getPath()); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function testSetQuery() : void |
||
| 110 | { |
||
| 111 | $url = new Url('http://jwage.com'); |
||
| 112 | $url->query->set('param1', 'value1'); |
||
| 113 | $this->assertInstanceOf('Purl\Query', $url->query); |
||
| 114 | $this->assertEquals('param1=value1', (string) $url->query); |
||
| 115 | $this->assertEquals(['param1' => 'value1'], $url->query->getData()); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testGetQuery() : void |
||
| 119 | { |
||
| 120 | $url = new Url('http://jwage.com'); |
||
| 121 | $url->query->set('param1', 'value1'); |
||
| 122 | $this->assertInstanceOf('Purl\Query', $url->query); |
||
| 123 | $this->assertEquals('param1=value1', $url->getQuery()); |
||
| 124 | $this->assertEquals(['param1' => 'value1'], $url->getQuery()->getData()); |
||
| 125 | } |
||
| 126 | |||
| 127 | public function testSetFragment() : void |
||
| 128 | { |
||
| 129 | $url = new Url('http://jwage.com'); |
||
| 130 | $url->fragment->path = 'about'; |
||
|
0 ignored issues
–
show
It seems like
'about' of type string is incompatible with the declared type Purl\Path of property $path.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 131 | $url->fragment->query->set('param1', 'value1'); |
||
| 132 | $this->assertEquals('http://jwage.com/#about?param1=value1', (string) $url); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testProtocolRelativeUrl() : void |
||
| 136 | { |
||
| 137 | $url = new Url('https://example.com'); |
||
| 138 | $this->assertEquals('https', $url->join('//code.jquery.com/jquery-3.10.js')->scheme); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testGetFragment() : void |
||
| 142 | { |
||
| 143 | $url = new Url('http://jwage.com'); |
||
| 144 | $url->fragment->path = 'about'; |
||
|
0 ignored issues
–
show
It seems like
'about' of type string is incompatible with the declared type Purl\Path of property $path.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 145 | $url->fragment->query->set('param1', 'value1'); |
||
| 146 | $this->assertEquals('about?param1=value1', (string) $url->getFragment()); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testGetNetloc() : void |
||
| 150 | { |
||
| 151 | $url = new Url('https://user:[email protected]:443'); |
||
| 152 | $this->assertEquals('user:[email protected]:443', $url->getNetloc()); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testGetUrl() : void |
||
| 156 | { |
||
| 157 | $url = new Url('http://jwage.com'); |
||
| 158 | $this->assertEquals('http://jwage.com/', $url->getUrl()); |
||
| 159 | } |
||
| 160 | |||
| 161 | public function testSetUrl() : void |
||
| 162 | { |
||
| 163 | $url = new Url('http://jwage.com'); |
||
| 164 | $this->assertEquals('http://jwage.com/', $url->getUrl()); |
||
| 165 | $url->setUrl('http://google.com'); |
||
| 166 | $this->assertEquals('http://google.com/', $url->getUrl()); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function testArrayAccess() : void |
||
| 170 | { |
||
| 171 | $url = new Url('http://jwage.com'); |
||
| 172 | $url['path'] = 'about'; |
||
| 173 | $this->assertEquals('http://jwage.com/about', (string) $url); |
||
| 174 | } |
||
| 175 | |||
| 176 | public function testCanonicalization() : void |
||
| 177 | { |
||
| 178 | $url = new Url('http://jwage.com'); |
||
| 179 | $this->assertEquals('com', $url->publicSuffix); |
||
| 180 | $this->assertEquals('jwage.com', $url->registerableDomain); |
||
| 181 | $this->assertEquals('com.jwage', $url->canonical); |
||
| 182 | |||
| 183 | $url = new Url('http://sub.domain.jwage.com/index.php?param1=value1'); |
||
| 184 | $this->assertEquals('com', $url->publicSuffix); |
||
| 185 | $this->assertEquals('jwage.com', $url->registerableDomain); |
||
| 186 | $this->assertEquals('sub.domain', $url->subdomain); |
||
| 187 | $this->assertEquals('com.jwage.domain.sub/index.php?param1=value1', $url->canonical); |
||
| 188 | |||
| 189 | $url = new Url('http://sub.domain.jwage.co.uk/index.php?param1=value1'); |
||
| 190 | $this->assertEquals('co.uk', $url->publicSuffix); |
||
| 191 | $this->assertEquals('jwage.co.uk', $url->registerableDomain); |
||
| 192 | $this->assertEquals('sub.domain', $url->subdomain); |
||
| 193 | $this->assertEquals('uk.co.jwage.domain.sub/index.php?param1=value1', $url->canonical); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function testPath() : void |
||
| 197 | { |
||
| 198 | $url = new Url('http://jwage.com'); |
||
| 199 | $url->path->add('about')->add('me'); |
||
| 200 | $this->assertEquals('http://jwage.com/about/me', (string) $url); |
||
| 201 | $url->path->setPath('new/path'); |
||
| 202 | $this->assertEquals('http://jwage.com/new/path', (string) $url); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function testFragment() : void |
||
| 206 | { |
||
| 207 | $url = new Url('http://jwage.com'); |
||
| 208 | $url->fragment = 'test'; |
||
|
0 ignored issues
–
show
It seems like
'test' of type string is incompatible with the declared type Purl\Fragment of property $fragment.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 209 | $url->fragment->path->add('about')->add('me'); |
||
|
1 ignored issue
–
show
|
|||
| 210 | $url->fragment->query->set('param1', 'value1'); |
||
|
1 ignored issue
–
show
|
|||
| 211 | $this->assertEquals('http://jwage.com/#test/about/me?param1=value1', (string) $url); |
||
| 212 | |||
| 213 | $url->fragment = 'test/aboutme?param1=value1'; |
||
| 214 | $this->assertEquals('test/aboutme', (string) $url->fragment->path); |
||
| 215 | $this->assertEquals('param1=value1', (string) $url->fragment->query); |
||
| 216 | } |
||
| 217 | |||
| 218 | public function testQuery() : void |
||
| 219 | { |
||
| 220 | $url = new Url('http://jwage.com'); |
||
| 221 | $url->query = 'param1=value1¶m2=value2'; |
||
|
0 ignored issues
–
show
It seems like
'param1=value1¶m2=value2' of type string is incompatible with the declared type Purl\Query of property $query.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||
| 222 | $this->assertEquals(['param1' => 'value1', 'param2' => 'value2'], $url->query->getData()); |
||
| 223 | $url->query->set('param3', 'value3'); |
||
| 224 | $this->assertEquals('param1=value1¶m2=value2¶m3=value3', (string) $url->query); |
||
| 225 | } |
||
| 226 | |||
| 227 | public function testIsAbsolute() : void |
||
| 228 | { |
||
| 229 | $url1 = new Url('http://jwage.com'); |
||
| 230 | $this->assertTrue($url1->isAbsolute()); |
||
| 231 | |||
| 232 | $url2 = new Url('/about/me'); |
||
| 233 | $this->assertFalse($url2->isAbsolute()); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function testGetResource() : void |
||
| 237 | { |
||
| 238 | $url = new Url('http://jwage.com/about?query=value'); |
||
| 239 | $this->assertEquals('/about?query=value', $url->resource); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testPort() : void |
||
| 243 | { |
||
| 244 | $url = new Url('http://jwage.com:443'); |
||
| 245 | $this->assertEquals('443', $url->port); |
||
| 246 | $this->assertEquals('http://jwage.com:443/', (string) $url); |
||
| 247 | } |
||
| 248 | |||
| 249 | public function testAuth() : void |
||
| 250 | { |
||
| 251 | $url = new Url('http://user:[email protected]'); |
||
| 252 | $this->assertEquals('user', $url->user); |
||
| 253 | $this->assertEquals('pass', $url->pass); |
||
| 254 | $this->assertEquals('http://user:[email protected]/', (string) $url); |
||
| 255 | |||
| 256 | $url = new Url('http://user:@jwage.com'); |
||
| 257 | $this->assertEquals('user', $url->user); |
||
| 258 | $this->assertEquals(null, $url->pass); |
||
| 259 | $this->assertEquals('http://[email protected]/', (string) $url); |
||
| 260 | |||
| 261 | $url = new Url('http://[email protected]'); |
||
| 262 | $this->assertEquals('user', $url->user); |
||
| 263 | $this->assertEquals(null, $url->pass); |
||
| 264 | $this->assertEquals('http://[email protected]/', (string) $url); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function testExtract() : void |
||
| 268 | { |
||
| 269 | $urls = Url::extract("test\nmore test https://google.com ftp://jwage.com ftps://jwage.com http://google.com\ntesting this out http://jwage.com more text https://we-are-a-professional-studio-of.photography"); |
||
| 270 | $this->assertEquals(6, count($urls)); |
||
| 271 | $this->assertEquals('https://google.com/', (string) $urls[0]); |
||
| 272 | $this->assertEquals('ftp://jwage.com/', (string) $urls[1]); |
||
| 273 | $this->assertEquals('ftps://jwage.com/', (string) $urls[2]); |
||
| 274 | $this->assertEquals('http://google.com/', (string) $urls[3]); |
||
| 275 | $this->assertEquals('http://jwage.com/', (string) $urls[4]); |
||
| 276 | $this->assertEquals('https://we-are-a-professional-studio-of.photography/', (string) $urls[5]); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function testManualObjectConstruction() : void |
||
| 280 | { |
||
| 281 | $url = new Url('http://jwage.com'); |
||
| 282 | $url->set('path', new Path('about')); |
||
| 283 | $url->set('query', new Query('param=value')); |
||
| 284 | $url->set('fragment', new Fragment(new Path('about'), new Query('param=value'))); |
||
| 285 | $this->assertEquals('http://jwage.com/about?param=value#about?param=value', (string) $url); |
||
| 286 | } |
||
| 287 | |||
| 288 | public function testIdeGettersAndSetters() : void |
||
| 289 | { |
||
| 290 | $url = new Url('http://jwage.com'); |
||
| 291 | $url->setPath(new Path('about')); |
||
| 292 | $url->setQuery(new Query('param=value')); |
||
| 293 | $url->setFragment(new Fragment(new Path('about'), new Query('param=value'))); |
||
| 294 | $this->assertEquals('http://jwage.com/about?param=value#about?param=value', (string) $url); |
||
| 295 | } |
||
| 296 | |||
| 297 | public function testFromCurrentServerVariables() : void |
||
| 298 | { |
||
| 299 | $_SERVER['HTTP_HOST'] = 'jwage.com'; |
||
| 300 | $_SERVER['SERVER_PORT'] = 80; |
||
| 301 | $_SERVER['REQUEST_URI'] = '/about'; |
||
| 302 | |||
| 303 | $url = Url::fromCurrent(); |
||
| 304 | $this->assertEquals('http://jwage.com/about', (string) $url); |
||
| 305 | |||
| 306 | $_SERVER['REQUEST_URI'] = '/about?param=value'; |
||
| 307 | |||
| 308 | $url = Url::fromCurrent(); |
||
| 309 | $this->assertEquals('http://jwage.com/about?param=value', (string) $url); |
||
| 310 | |||
| 311 | $_SERVER['HTTPS'] = 'off'; |
||
| 312 | $_SERVER['HTTP_HOST'] = 'jwage.com'; |
||
| 313 | unset($_SERVER['SERVER_PORT']); |
||
| 314 | unset($_SERVER['REQUEST_URI']); |
||
| 315 | |||
| 316 | $url = Url::fromCurrent(); |
||
| 317 | $this->assertEquals('http://jwage.com/', (string) $url); |
||
| 318 | |||
| 319 | $_SERVER['HTTPS'] = 'on'; |
||
| 320 | $_SERVER['HTTP_HOST'] = 'jwage.com'; |
||
| 321 | $_SERVER['SERVER_PORT'] = 443; |
||
| 322 | unset($_SERVER['REQUEST_URI']); |
||
| 323 | |||
| 324 | $url = Url::fromCurrent(); |
||
| 325 | $this->assertEquals('https://jwage.com/', (string) $url); |
||
| 326 | |||
| 327 | unset($_SERVER['HTTPS']); |
||
| 328 | $_SERVER['HTTP_HOST'] = 'jwage.com'; |
||
| 329 | $_SERVER['SERVER_PORT'] = 8080; |
||
| 330 | unset($_SERVER['REQUEST_URI']); |
||
| 331 | |||
| 332 | $url = Url::fromCurrent(); |
||
| 333 | $this->assertEquals('http://jwage.com:8080/', (string) $url); |
||
| 334 | |||
| 335 | unset($_SERVER['HTTPS']); |
||
| 336 | $_SERVER['HTTP_HOST'] = 'jwage.com'; |
||
| 337 | $_SERVER['SERVER_PORT'] = 80; |
||
| 338 | unset($_SERVER['REQUEST_URI']); |
||
| 339 | $_SERVER['PHP_AUTH_USER'] = 'user'; |
||
| 340 | $_SERVER['PHP_AUTH_PW'] = 'passwd123'; |
||
| 341 | |||
| 342 | $url = Url::fromCurrent(); |
||
| 343 | $this->assertEquals('http://user:[email protected]/', (string) $url); |
||
| 344 | } |
||
| 345 | |||
| 346 | public function testRelativeUrl() : void |
||
| 347 | { |
||
| 348 | // test all resource parts |
||
| 349 | $url = new Url('/path1/path2?x=1&y=2#frag'); |
||
| 350 | $this->assertFalse($url->isAbsolute()); |
||
| 351 | $this->assertEquals('/path1/path2?x=1&y=2#frag', (string) $url); |
||
| 352 | |||
| 353 | // test base path |
||
| 354 | $url = new Url('/path1'); |
||
| 355 | $this->assertEquals('/path1', (string) $url); |
||
| 356 | |||
| 357 | // test minimal path |
||
| 358 | $url = new Url('/'); |
||
| 359 | $this->assertEquals('/', (string) $url); |
||
| 360 | |||
| 361 | // test feature request |
||
| 362 | $url = new Url('/events'); |
||
| 363 | $url->query->set('param1', 'value1'); |
||
| 364 | $this->assertEquals('/events?param1=value1', (string) $url); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | class TestParser implements ParserInterface |
||
| 369 | { |
||
| 370 | /** |
||
| 371 | * @param string|Url $url |
||
| 372 | * |
||
| 373 | * @return mixed[] |
||
| 374 | */ |
||
| 375 | public function parseUrl($url) : array |
||
| 376 | { |
||
| 377 | return $url; |
||
|
0 ignored issues
–
show
|
|||
| 378 | } |
||
| 379 | } |
||
| 380 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..