Skip to content

Commit

Permalink
ADD: Test to check the work with symlink
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheren committed Dec 24, 2015
1 parent 2a27343 commit c401a85
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 37 deletions.
57 changes: 27 additions & 30 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,14 @@ public function path($source)
}

/**
* Find actual file or directory in the paths.
* @param $paths
* @param $file
* @return null|string
* Get all absolute path to a file or a directory.
* @param $source (example: "default:file.txt")
* @return mixed
*/
protected function _find($paths, $file)
public function paths($source)
{
$paths = (array) $paths;
$file = ltrim($file, "\\/");

foreach ($paths as $path) {
$fullPath = $this->normalize($path . '/' . $file);
if (file_exists($fullPath)) {
return $fullPath;
}
}

return null;
list(, $paths) = $this->parse($source);
return $paths;
}

/**
Expand Down Expand Up @@ -163,20 +153,6 @@ public function parse($source, $package = Path::DEFAULT_PACKAGE)
return array($package, $paths, $path);
}

/**
* Get paths by package name.
* @param string $package
* @return null
*/
public function getPaths($package = Path::DEFAULT_PACKAGE)
{
if (isset($this->_paths[$package])) {
return $this->_paths[$package];
}

return null;
}

/**
* Check virtual or real path.
* @param $path
Expand All @@ -202,4 +178,25 @@ public function prefix($path)
$path = FS::clean($path, '/');
return preg_match('|^(?P<prefix>([a-zA-Z]+:)?//?)|', $path, $matches) ? $matches['prefix'] : null;
}

/**
* Find actual file or directory in the paths.
* @param $paths
* @param $file
* @return null|string
*/
protected function _find($paths, $file)
{
$paths = (array) $paths;
$file = ltrim($file, "\\/");

foreach ($paths as $path) {
$fullPath = $this->normalize($path . '/' . $file);
if (file_exists($fullPath)) {
return $fullPath;
}
}

return null;
}
}
28 changes: 21 additions & 7 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function testRegisterAppend()
$this->_root,
);

$defaultPaths = $path->getPaths();
$testPaths = $path->getPaths('test');
$defaultPaths = $path->paths('default:');
$testPaths = $path->paths('test:');

isSame($expected, $testPaths);
isSame($expected, $defaultPaths);
Expand All @@ -76,7 +76,7 @@ public function testRegisterPrepend()
$appendPath,
);

$package = $path->getPaths();
$package = $path->paths('default:');
isSame($expected, $package);
}

Expand All @@ -90,16 +90,16 @@ public function testRegisterReset()
$path->register($this->_paths);
$path->register($newPath, $path::DEFAULT_PACKAGE, $path::RESET);

isSame($newPath, $path->getPaths());
isSame($newPath, $path->paths($path::DEFAULT_PACKAGE));
}

public function testEmptyPackage()
public function testEmptyPaths()
{
$path = new Path();
$path->register($this->_paths);

$packagePaths = $path->getPaths('alias');
isNull($packagePaths);
$packagePaths = $path->paths('alias:');
isSame(array(), $packagePaths);
}

public function testIsVirtual()
Expand Down Expand Up @@ -187,6 +187,7 @@ public function testPathSuccess()
$fs->mkdir($dir2);

$_dir = $dir2 . DS . 'simple';

$fs->mkdir($_dir);

$f1 = $dir2 . DS . 'text.txt';
Expand All @@ -201,6 +202,14 @@ public function testPathSuccess()
$fs->dumpFile($f4, '');
$fs->dumpFile($f5, '');

// Symlink folder.
$symOrigDir = $dir1 . DS . 'sym-dir-orig';
$symLink = $dir1 . DS . 'symlink' . DS . 'folder';

$fs->mkdir($symOrigDir);
$fs->dumpFile($symOrigDir . DS . 'test-symlink.txt', '');
$fs->symlink($symOrigDir, $symLink, true);

$path->register($paths);

isSame($path->normalize($f1), $path->path('default:text.txt'));
Expand All @@ -221,6 +230,11 @@ public function testPathSuccess()
isSame($path->normalize($f5), $path->path('default:\/simple' . DS . 'file.txt'));
isNull($path->path('alias:/simple' . DS . 'file.txt'));

isSame(
$path->normalize($symLink . DS . 'test-symlink.txt'),
$path->path('default:symlink/folder/test-symlink.txt')
);

$fs->remove($dir1);
}
}

0 comments on commit c401a85

Please sign in to comment.