| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace LanguageServer\Tests\Utils; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  | use InvalidArgumentException; | 
					
						
							|  |  |  | use function LanguageServer\{pathToUri, uriToPath}; | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class FileUriTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |     public function testPathToUri() | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |         $uri = pathToUri('var/log'); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |         $this->assertEquals('file:///var/log', $uri); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |         $uri = pathToUri('/usr/local/bin'); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |         $this->assertEquals('file:///usr/local/bin', $uri); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |         $uri = pathToUri('a/b/c/test.txt'); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |         $this->assertEquals('file:///a/b/c/test.txt', $uri); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |         $uri = pathToUri('/d/e/f'); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |         $this->assertEquals('file:///d/e/f', $uri); | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // special chars are escaped
 | 
					
						
							|  |  |  |         $uri = pathToUri('c:/path/to/file/dürüm döner.php'); | 
					
						
							| 
									
										
										
										
											2016-11-07 09:24:49 +00:00
										 |  |  |         $this->assertEquals('file:///c:/path/to/file/d%C3%BCr%C3%BCm%20d%C3%B6ner.php', $uri); | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //backslashes are transformed
 | 
					
						
							|  |  |  |         $uri = pathToUri('c:\\foo\\bar.baz'); | 
					
						
							|  |  |  |         $this->assertEquals('file:///c:/foo/bar.baz', $uri); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testUriToPath() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $uri = 'file:///var/log'; | 
					
						
							|  |  |  |         $this->assertEquals('/var/log', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///usr/local/bin'; | 
					
						
							|  |  |  |         $this->assertEquals('/usr/local/bin', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///a/b/c/test.txt'; | 
					
						
							|  |  |  |         $this->assertEquals('/a/b/c/test.txt', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///d/e/f'; | 
					
						
							|  |  |  |         $this->assertEquals('/d/e/f', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///c:/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php'; | 
					
						
							|  |  |  |         $this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///c:/foo/bar.baz'; | 
					
						
							|  |  |  |         $this->assertEquals('c:\\foo\\bar.baz', uriToPath($uri)); | 
					
						
							| 
									
										
										
										
											2016-10-11 22:53:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php'; | 
					
						
							|  |  |  |         $this->assertEquals('c:\\path\\to\\file\\dürüm döner.php', uriToPath($uri)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $uri = 'file:///c%3A/foo/bar.baz'; | 
					
						
							|  |  |  |         $this->assertEquals('c:\\foo\\bar.baz', uriToPath($uri)); | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testUriToPathForUnknownProtocol() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->expectException(InvalidArgumentException::class); | 
					
						
							|  |  |  |         $uri = 'vfs:///whatever'; | 
					
						
							|  |  |  |         uriToPath($uri); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |     public function testUriToPathForInvalidProtocol() | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-10 13:06:02 +00:00
										 |  |  |         $this->expectException(InvalidArgumentException::class); | 
					
						
							|  |  |  |         $uri = 'http://www.google.com'; | 
					
						
							|  |  |  |         uriToPath($uri); | 
					
						
							| 
									
										
										
										
											2016-09-30 09:30:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |