Skip to content

Commit

Permalink
Added functionality to use wildcards in InPath
Browse files Browse the repository at this point in the history
  • Loading branch information
mbed67 committed Aug 16, 2015
1 parent 2f67e4c commit 0a99d5b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
26 changes: 19 additions & 7 deletions src/Specification/InPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ public function __construct(Path $path)
public function isSatisfiedBy(array $value)
{
if (isset($value['dirname'])) {
$path = $this->removeDotSlash((string) $this->path);
$path = $this->cleanPath((string) $this->path);

if (substr($value['dirname'], 0, strlen($path)) === $path) {
$validChars = '[a-zA-Z0-9\\\/\.\<\>\,\|\:\(\)\&\;\#]';

$pattern = '(^(?!\/)'
. str_replace(['?', '*'], [$validChars . '?', $validChars . '*'], $path)
. $validChars . '*)';

if (preg_match($pattern, $value['dirname'] . '/')) {
return true;
}
return false;
Expand All @@ -56,15 +62,21 @@ public function isSatisfiedBy(array $value)

/**
* If a path is given with a leading ./ this will be removed
* If a path doesn't have a trailing /, a slash will be added
*
* @param string $dirname
* @param string $path
* @return string
*/
private function removeDotSlash($dirname)
private function cleanPath($path)
{
if (substr($dirname, 0, 2) === './') {
$dirname = substr($dirname, 1);
if (substr($path, 0, 2) === './') {
$path = substr($path, 1);
}
return $dirname;

if (substr($path, -1) !== '/') {
$path = $path . '/';
}

return $path;
}
}
41 changes: 36 additions & 5 deletions tests/unit/Specification/InPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,59 @@ class InPathTest extends \PHPUnit_Framework_TestCase
*/
public function setUp()
{
$this->fixture = new InPath(new Path('.hiddendir'));
$this->fixture = new InPath(new Path('*dden?ir/n'));
}

/**
* @covers ::__construct
* @covers ::isSatisfiedBy
* @covers ::<private>
* @dataProvider validDirnames
* @uses Flyfinder\Path
*/
public function testIfSpecificationIsSatisfied()
public function testIfSpecificationIsSatisfied($dirname)
{
$this->assertTrue($this->fixture->isSatisfiedBy(['dirname' => '.hiddendir/normaldir']));
$this->assertTrue($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
}

/**
* Data provider for testIfSpecificationIsSatisfied. Contains a few valid directory names
*
* @return array
*/
public function validDirnames()
{
return [
['.hiddendir/n'],
['.hiddendir/n/'],
['.hiddendir/n/somedir'],
['.hiddendir/n/somedir.txt']
];
}

/**
* @covers ::__construct
* @covers ::isSatisfiedBy
* @covers ::<private>
* @dataProvider InvalidDirnames
* @uses Flyfinder\Path
*/
public function testIfSpecificationIsNotSatisfied()
public function testIfSpecificationIsNotSatisfied($dirname)
{
$this->assertFalse($this->fixture->isSatisfiedBy(['dirname' => $dirname]));
}

/**
* Data provider for testIfSpecificationIsNotSatisfied. Contains a few valid directory names
*
* @return array
*/
public function InvalidDirnames()
{
$this->assertFalse($this->fixture->isSatisfiedBy(['dirname' => '/home']));
return [
['/hiddendir/n'],
['.hiddendir/normaldir'],
['.hiddendir.ext/n']
];
}
}

0 comments on commit 0a99d5b

Please sign in to comment.