[WIP][Finder] Fix wrong implementation on sortable callback comparator
This commit is contained in:
parent
c8476ee744
commit
b965fa23a6
@ -31,7 +31,7 @@ class SortableIterator implements \IteratorAggregate
|
|||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param \Traversable $iterator The Iterator to filter
|
* @param \Traversable $iterator The Iterator to filter
|
||||||
* @param int|callback $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
|
* @param int|callable $sort The sort type (SORT_BY_NAME, SORT_BY_TYPE, or a PHP callback)
|
||||||
*
|
*
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
@ -55,20 +55,20 @@ class SortableIterator implements \IteratorAggregate
|
|||||||
};
|
};
|
||||||
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
|
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
|
||||||
$this->sort = function ($a, $b) {
|
$this->sort = function ($a, $b) {
|
||||||
return ($a->getATime() > $b->getATime());
|
return ($a->getATime() - $b->getATime());
|
||||||
};
|
};
|
||||||
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
|
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
|
||||||
$this->sort = function ($a, $b) {
|
$this->sort = function ($a, $b) {
|
||||||
return ($a->getCTime() > $b->getCTime());
|
return ($a->getCTime() - $b->getCTime());
|
||||||
};
|
};
|
||||||
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
|
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
|
||||||
$this->sort = function ($a, $b) {
|
$this->sort = function ($a, $b) {
|
||||||
return ($a->getMTime() > $b->getMTime());
|
return ($a->getMTime() - $b->getMTime());
|
||||||
};
|
};
|
||||||
} elseif (is_callable($sort)) {
|
} elseif (is_callable($sort)) {
|
||||||
$this->sort = $sort;
|
$this->sort = $sort;
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException('The SortableIterator takes a PHP callback or a valid built-in sort algorithm as an argument.');
|
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +30,26 @@ class SortableIteratorTest extends RealIteratorTestCase
|
|||||||
*/
|
*/
|
||||||
public function testAccept($mode, $expected)
|
public function testAccept($mode, $expected)
|
||||||
{
|
{
|
||||||
|
if (!is_callable($mode)) {
|
||||||
|
switch ($mode) {
|
||||||
|
case SortableIterator::SORT_BY_ACCESSED_TIME :
|
||||||
|
file_get_contents(self::toAbsolute('.git'));
|
||||||
|
sleep(1);
|
||||||
|
file_get_contents(self::toAbsolute('.bar'));
|
||||||
|
break;
|
||||||
|
case SortableIterator::SORT_BY_CHANGED_TIME :
|
||||||
|
file_put_contents(self::toAbsolute('test.php'), 'foo');
|
||||||
|
sleep(1);
|
||||||
|
file_put_contents(self::toAbsolute('test.py'), 'foo');
|
||||||
|
break;
|
||||||
|
case SortableIterator::SORT_BY_MODIFIED_TIME :
|
||||||
|
file_put_contents(self::toAbsolute('test.php'), 'foo');
|
||||||
|
sleep(1);
|
||||||
|
file_put_contents(self::toAbsolute('test.py'), 'foo');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$inner = new Iterator(self::$files);
|
$inner = new Iterator(self::$files);
|
||||||
|
|
||||||
$iterator = new SortableIterator($inner, $mode);
|
$iterator = new SortableIterator($inner, $mode);
|
||||||
@ -82,9 +102,54 @@ class SortableIteratorTest extends RealIteratorTestCase
|
|||||||
'toto',
|
'toto',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$sortByAccessedTime = array(
|
||||||
|
'foo/bar.tmp',
|
||||||
|
'test.php',
|
||||||
|
'toto',
|
||||||
|
'foo bar',
|
||||||
|
'foo',
|
||||||
|
'test.py',
|
||||||
|
'.foo',
|
||||||
|
'.foo/.bar',
|
||||||
|
'.foo/bar',
|
||||||
|
'.git',
|
||||||
|
'.bar'
|
||||||
|
);
|
||||||
|
|
||||||
|
$sortByChangedTime = array(
|
||||||
|
'foo',
|
||||||
|
'foo/bar.tmp',
|
||||||
|
'toto',
|
||||||
|
'.git',
|
||||||
|
'.bar',
|
||||||
|
'.foo',
|
||||||
|
'foo bar',
|
||||||
|
'.foo/.bar',
|
||||||
|
'.foo/bar',
|
||||||
|
'test.php',
|
||||||
|
'test.py'
|
||||||
|
);
|
||||||
|
|
||||||
|
$sortByModifiedTime = array(
|
||||||
|
'foo/bar.tmp',
|
||||||
|
'foo',
|
||||||
|
'toto',
|
||||||
|
'.git',
|
||||||
|
'.bar',
|
||||||
|
'.foo',
|
||||||
|
'foo bar',
|
||||||
|
'.foo/.bar',
|
||||||
|
'.foo/bar',
|
||||||
|
'test.php',
|
||||||
|
'test.py'
|
||||||
|
);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
array(SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)),
|
array(SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)),
|
||||||
array(SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)),
|
array(SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)),
|
||||||
|
array(SortableIterator::SORT_BY_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)),
|
||||||
|
array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)),
|
||||||
|
array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)),
|
||||||
array(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }, $this->toAbsolute($customComparison)),
|
array(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }, $this->toAbsolute($customComparison)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user