Added tests for pathToUri and findFilesRecursive
parent
6b28cb6a93
commit
ca0fcced20
|
@ -0,0 +1 @@
|
||||||
|
A
|
|
@ -0,0 +1 @@
|
||||||
|
B
|
|
@ -0,0 +1 @@
|
||||||
|
Peeakboo!
|
|
@ -1,31 +1,33 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace LanguageServer;
|
namespace LanguageServer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively Searches files with matching filename, starting at $path.
|
* Recursively Searches files with matching filename, starting at $path.
|
||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $pattern
|
* @param string $pattern
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function findFilesRecursive(string $path, string $pattern): array {
|
function findFilesRecursive(string $path, string $pattern): array {
|
||||||
$dir = new \RecursiveDirectoryIterator($path);
|
$dir = new \RecursiveDirectoryIterator($path);
|
||||||
$ite = new \RecursiveIteratorIterator($dir);
|
$ite = new \RecursiveIteratorIterator($dir);
|
||||||
$files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
|
$files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
|
||||||
$fileList = array();
|
$fileList = array();
|
||||||
foreach($files as $file) {
|
foreach($files as $file) {
|
||||||
$fileList = array_merge($fileList, $file);
|
$fileList = array_merge($fileList, $file);
|
||||||
}
|
}
|
||||||
return $fileList;
|
return $fileList;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transforms an absolute file path into a URI as used by the language server protocol.
|
* Transforms an absolute file path into a URI as used by the language server protocol.
|
||||||
*
|
*
|
||||||
* @param string $filepath
|
* @param string $filepath
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function pathToUri(string $filepath): string {
|
function pathToUri(string $filepath): string {
|
||||||
return 'file://'.($filepath[0] == '/' || $filepath[0] == '\\' ? '' : '/').str_replace('\\', '/', $filepath);
|
$filepath = trim(str_replace('\\', '/', $filepath), '/');
|
||||||
|
$filepath = implode('/', array_map('urlencode', explode('/', $filepath)));
|
||||||
|
return 'file:///'.$filepath;
|
||||||
}
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace LanguageServer\Tests\Utils;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class FileUriTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testSpecialCharsAreEscaped()
|
||||||
|
{
|
||||||
|
$uri = \LanguageServer\pathToUri('c:/path/to/file/dürüm döner.php');
|
||||||
|
$this->assertEquals('file:///c%3A/path/to/file/d%C3%BCr%C3%BCm+d%C3%B6ner.php', $uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUriIsWellFormed()
|
||||||
|
{
|
||||||
|
$uri = \LanguageServer\pathToUri('var/log');
|
||||||
|
$this->assertEquals('file:///var/log', $uri);
|
||||||
|
|
||||||
|
$uri = \LanguageServer\pathToUri('/usr/local/bin');
|
||||||
|
$this->assertEquals('file:///usr/local/bin', $uri);
|
||||||
|
|
||||||
|
$uri = \LanguageServer\pathToUri('a/b/c/');
|
||||||
|
$this->assertEquals('file:///a/b/c', $uri);
|
||||||
|
|
||||||
|
$uri = \LanguageServer\pathToUri('/d/e/f');
|
||||||
|
$this->assertEquals('file:///d/e/f', $uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBackslashesAreTransformed()
|
||||||
|
{
|
||||||
|
$uri = \LanguageServer\pathToUri('c:\\foo\\bar.baz');
|
||||||
|
$this->assertEquals('file:///c%3A/foo/bar.baz', $uri);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace LanguageServer\Tests\Utils;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class RecursiveFileSearchTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testFilesAreFound()
|
||||||
|
{
|
||||||
|
$path = realpath(__DIR__ . '/../../fixtures/recursive');
|
||||||
|
$files = \LanguageServer\findFilesRecursive($path, '/.+\.txt/');
|
||||||
|
|
||||||
|
$this->assertEquals([
|
||||||
|
$path . DIRECTORY_SEPARATOR . 'a.txt',
|
||||||
|
$path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'b.txt',
|
||||||
|
$path . DIRECTORY_SEPARATOR . 'search' . DIRECTORY_SEPARATOR . 'here' . DIRECTORY_SEPARATOR . 'c.txt',
|
||||||
|
], $files);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue