feature #27024 [Finder] added "use natural sort" option (vyshkant)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Finder] added "use natural sort" option

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26930
| License       | MIT
| Doc PR        | symfony/symfony-docs#9671

Added `$useNaturalSort` optional argument to `Finder::sortByName()` method. If it is specified and equals to `true`, ["natural sort order" algorithm](https://en.wikipedia.org/wiki/Natural_sort_order) will be applied, which means that `strnatcmp` function will be used instead of `strcmp` (see #26930 for details).

Commits
-------

e697c7d272 [Finder] added "use natural sort" option
This commit is contained in:
Fabien Potencier 2018-05-30 06:00:02 +02:00
commit 3bade96ff2
11 changed files with 655 additions and 51 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
4.2.0
-----
* added $useNaturalSort option to Finder::sortByName() method
4.0.0 4.0.0
----- -----

View File

@ -397,13 +397,17 @@ class Finder implements \IteratorAggregate, \Countable
* *
* This can be slow as all the matching files and directories must be retrieved for comparison. * This can be slow as all the matching files and directories must be retrieved for comparison.
* *
* @param bool $useNaturalSort Whether to use natural sort or not, disabled by default
*
* @return $this * @return $this
* *
* @see SortableIterator * @see SortableIterator
*/ */
public function sortByName() public function sortByName(/* bool $useNaturalSort = false */)
{ {
$this->sort = Iterator\SortableIterator::SORT_BY_NAME; $useNaturalSort = 0 < func_num_args() && func_get_arg(0);
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\SortableIterator::SORT_BY_NAME;
return $this; return $this;
} }

View File

@ -23,6 +23,7 @@ class SortableIterator implements \IteratorAggregate
const SORT_BY_ACCESSED_TIME = 3; const SORT_BY_ACCESSED_TIME = 3;
const SORT_BY_CHANGED_TIME = 4; const SORT_BY_CHANGED_TIME = 4;
const SORT_BY_MODIFIED_TIME = 5; const SORT_BY_MODIFIED_TIME = 5;
const SORT_BY_NAME_NATURAL = 6;
private $iterator; private $iterator;
private $sort; private $sort;
@ -41,6 +42,10 @@ class SortableIterator implements \IteratorAggregate
$this->sort = function ($a, $b) { $this->sort = function ($a, $b) {
return strcmp($a->getRealpath() ?: $a->getPathname(), $b->getRealpath() ?: $b->getPathname()); return strcmp($a->getRealpath() ?: $a->getPathname(), $b->getRealpath() ?: $b->getPathname());
}; };
} elseif (self::SORT_BY_NAME_NATURAL === $sort) {
$this->sort = function ($a, $b) {
return strnatcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
};
} elseif (self::SORT_BY_TYPE === $sort) { } elseif (self::SORT_BY_TYPE === $sort) {
$this->sort = function ($a, $b) { $this->sort = function ($a, $b) {
if ($a->isDir() && $b->isFile()) { if ($a->isDir() && $b->isFile()) {

View File

@ -24,33 +24,70 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->directories()); $this->assertSame($finder, $finder->directories());
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo', 'qux', 'toto')), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->directories(); $finder->directories();
$finder->files(); $finder->files();
$finder->directories(); $finder->directories();
$this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo', 'qux', 'toto')), $finder->in(self::$tmpDir)->getIterator());
} }
public function testFiles() public function testFiles()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->files()); $this->assertSame($finder, $finder->files());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo/bar.tmp',
'test.php',
'test.py',
'foo bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->files(); $finder->files();
$finder->directories(); $finder->directories();
$finder->files(); $finder->files();
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo/bar.tmp',
'test.php',
'test.py',
'foo bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testRemoveTrailingSlash() public function testRemoveTrailingSlash()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')); $expected = $this->toAbsolute(array(
'foo/bar.tmp',
'test.php',
'test.py',
'foo bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
));
$in = self::$tmpDir.'//'; $in = self::$tmpDir.'//';
$this->assertIterator($expected, $finder->in($in)->files()->getIterator()); $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
@ -89,15 +126,43 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('< 1')); $this->assertSame($finder, $finder->depth('< 1'));
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo',
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('<= 0')); $this->assertSame($finder, $finder->depth('<= 0'));
$this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('foo',
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->depth('>= 1')); $this->assertSame($finder, $finder->depth('>= 1'));
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo/bar.tmp',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->depth('< 1')->depth('>= 1'); $finder->depth('< 1')->depth('>= 1');
@ -108,7 +173,15 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->name('*.php')); $this->assertSame($finder, $finder->name('*.php'));
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'test.php',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->name('test.ph*'); $finder->name('test.ph*');
@ -121,7 +194,15 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->name('~\\.php$~i'); $finder->name('~\\.php$~i');
$this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'test.php',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->name('test.p{hp,y}'); $finder->name('test.p{hp,y}');
@ -132,12 +213,27 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->notName('*.php')); $this->assertSame($finder, $finder->notName('*.php'));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.py',
'toto',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->notName('*.php'); $finder->notName('*.php');
$finder->notName('*.py'); $finder->notName('*.py');
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo/bar.tmp',
'toto',
'foo bar',
'qux',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->name('test.ph*'); $finder->name('test.ph*');
@ -160,7 +256,10 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->name($regex); $finder->name($regex);
$this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'test.py',
'test.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSize() public function testSize()
@ -181,79 +280,356 @@ class FinderTest extends Iterator\RealIteratorTestCase
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->exclude('foo')); $this->assertSame($finder, $finder->exclude('foo'));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testIgnoreVCS() public function testIgnoreVCS()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false)); $this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'.git',
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'toto/.git',
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false); $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
$this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'.git',
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'toto/.git',
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false)); $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testIgnoreDotFiles() public function testIgnoreDotFiles()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false)); $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'.git',
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'toto/.git',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false); $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
$this->assertIterator($this->toAbsolute(array('.git', '.bar', '.foo', '.foo/.bar', '.foo/bar', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'toto/.git', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'.git',
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'toto/.git',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false)); $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByName() public function testSortByName()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByName()); $this->assertSame($finder, $finder->sortByName());
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php',
'test.py',
'toto',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByType() public function testSortByType()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByType()); $this->assertSame($finder, $finder->sortByType());
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo bar',
'toto',
'foo/bar.tmp',
'test.php',
'test.py',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByAccessedTime() public function testSortByAccessedTime()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByAccessedTime()); $this->assertSame($finder, $finder->sortByAccessedTime());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo/bar.tmp',
'test.php',
'toto',
'test.py',
'foo',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByChangedTime() public function testSortByChangedTime()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByChangedTime()); $this->assertSame($finder, $finder->sortByChangedTime());
$this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'toto',
'test.py',
'test.php',
'foo/bar.tmp',
'foo',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSortByModifiedTime() public function testSortByModifiedTime()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByModifiedTime()); $this->assertSame($finder, $finder->sortByModifiedTime());
$this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo/bar.tmp',
'test.php',
'toto',
'test.py',
'foo',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
}
public function testSortByNameNatural()
{
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByName(true));
$this->assertIterator($this->toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php',
'test.py',
'toto',
)), $finder->in(self::$tmpDir)->getIterator());
$finder = $this->buildFinder();
$this->assertSame($finder, $finder->sortByName(false));
$this->assertIterator($this->toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php',
'test.py',
'toto',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testSort() public function testSort()
{ {
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); })); $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
$this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo bar',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testFilter() public function testFilter()
@ -271,7 +647,23 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertSame($finder, $finder->followLinks()); $this->assertSame($finder, $finder->followLinks());
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)), $finder->in(self::$tmpDir)->getIterator());
} }
public function testIn() public function testIn()
@ -283,6 +675,12 @@ class FinderTest extends Iterator\RealIteratorTestCase
self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', self::$tmpDir.DIRECTORY_SEPARATOR.'test.php',
__DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php',
__DIR__.DIRECTORY_SEPARATOR.'GlobTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_0_1.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_1000_1.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_1002_0.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_10_2.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_12_0.php',
self::$tmpDir.DIRECTORY_SEPARATOR.'qux_2_0.php',
); );
$this->assertIterator($expected, $iterator); $this->assertIterator($expected, $iterator);
@ -339,7 +737,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$dirs[] = (string) $dir; $dirs[] = (string) $dir;
} }
$expected = $this->toAbsolute(array('foo', 'toto')); $expected = $this->toAbsolute(array('foo', 'qux', 'toto'));
sort($dirs); sort($dirs);
sort($expected); sort($expected);
@ -347,7 +745,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface'); $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface'); $this->assertEquals(3, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
$finder = $this->buildFinder(); $finder = $this->buildFinder();
$a = iterator_to_array($finder->directories()->in(self::$tmpDir)); $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
@ -366,7 +764,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$paths[] = $file->getRelativePath(); $paths[] = $file->getRelativePath();
} }
$ref = array('', '', '', '', 'foo', ''); $ref = array('', '', '', '', '', '', '', '', '', '', '', 'foo', 'qux', 'qux', '');
sort($ref); sort($ref);
sort($paths); sort($paths);
@ -384,7 +782,23 @@ class FinderTest extends Iterator\RealIteratorTestCase
$paths[] = $file->getRelativePathname(); $paths[] = $file->getRelativePathname();
} }
$ref = array('test.php', 'toto', 'test.py', 'foo', 'foo'.DIRECTORY_SEPARATOR.'bar.tmp', 'foo bar'); $ref = array(
'test.php',
'toto',
'test.py',
'foo',
'foo'.DIRECTORY_SEPARATOR.'bar.tmp',
'foo bar',
'qux',
'qux'.DIRECTORY_SEPARATOR.'baz_100_1.py',
'qux'.DIRECTORY_SEPARATOR.'baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
);
sort($paths); sort($paths);
sort($ref); sort($ref);
@ -402,7 +816,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
$finder = $finder->append($finder1); $finder = $finder->append($finder1);
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator()); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'qux', 'toto')), $finder->getIterator());
} }
public function testAppendWithAnArray() public function testAppendWithAnArray()
@ -601,7 +1015,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
public function getRegexNameTestData() public function getRegexNameTestData()
{ {
return array( return array(
array('~.+\\.p.+~i'), array('~.*t\\.p.+~i'),
array('~t.*s~i'), array('~t.*s~i'),
); );
} }
@ -718,7 +1132,20 @@ class FinderTest extends Iterator\RealIteratorTestCase
chmod($testDir, 0333); chmod($testDir, 0333);
if (false === ($couldRead = is_readable($testDir))) { if (false === ($couldRead = is_readable($testDir))) {
$this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator()); $this->assertIterator($this->toAbsolute(array(
'foo bar',
'test.php',
'test.py',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
)
), $finder->getIterator());
} }
// restore original permissions // restore original permissions

View File

@ -45,6 +45,15 @@ class DateRangeFilterIteratorTest extends RealIteratorTestCase
'.foo/.bar', '.foo/.bar',
'foo bar', 'foo bar',
'.foo/bar', '.foo/bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$since2MonthsAgo = array( $since2MonthsAgo = array(
@ -58,6 +67,15 @@ class DateRangeFilterIteratorTest extends RealIteratorTestCase
'.foo/.bar', '.foo/.bar',
'foo bar', 'foo bar',
'.foo/bar', '.foo/bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$untilLastMonth = array( $untilLastMonth = array(

View File

@ -41,6 +41,13 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
'.foo', '.foo',
'.bar', '.bar',
'foo bar', 'foo bar',
'qux',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$lessThanOrEqualTo1 = array( $lessThanOrEqualTo1 = array(
@ -56,6 +63,15 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
'.bar', '.bar',
'foo bar', 'foo bar',
'.foo/bar', '.foo/bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$graterThanOrEqualTo1 = array( $graterThanOrEqualTo1 = array(
@ -63,6 +79,8 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp', 'foo/bar.tmp',
'.foo/.bar', '.foo/.bar',
'.foo/bar', '.foo/bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
); );
$equalTo1 = array( $equalTo1 = array(
@ -70,6 +88,8 @@ class DepthRangeFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp', 'foo/bar.tmp',
'.foo/.bar', '.foo/.bar',
'.foo/bar', '.foo/bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
); );
return array( return array(

View File

@ -41,6 +41,15 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
'toto', 'toto',
'toto/.git', 'toto/.git',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$fo = array( $fo = array(
@ -56,6 +65,15 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
'toto', 'toto',
'toto/.git', 'toto/.git',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$toto = array( $toto = array(
@ -69,6 +87,15 @@ class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
'foo/bar.tmp', 'foo/bar.tmp',
'test.php', 'test.php',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
return array( return array(

View File

@ -37,11 +37,20 @@ class FileTypeFilterIteratorTest extends RealIteratorTestCase
'.foo/.bar', '.foo/.bar',
'.foo/bar', '.foo/bar',
'foo bar', 'foo bar',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
); );
$onlyDirectories = array( $onlyDirectories = array(
'.git', '.git',
'foo', 'foo',
'qux',
'toto', 'toto',
'toto/.git', 'toto/.git',
'.foo', '.foo',

View File

@ -33,6 +33,15 @@ abstract class RealIteratorTestCase extends IteratorTestCase
'toto/', 'toto/',
'toto/.git/', 'toto/.git/',
'foo bar', 'foo bar',
'qux_0_1.php',
'qux_2_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux/',
'qux/baz_1_2.py',
'qux/baz_100_1.py',
); );
self::$files = self::toAbsolute(self::$files); self::$files = self::toAbsolute(self::$files);

View File

@ -34,6 +34,7 @@ class SizeRangeFilterIteratorTest extends RealIteratorTestCase
'.foo', '.foo',
'.git', '.git',
'foo', 'foo',
'qux',
'test.php', 'test.php',
'toto', 'toto',
'toto/.git', 'toto/.git',

View File

@ -82,6 +82,15 @@ class SortableIteratorTest extends RealIteratorTestCase
'foo', 'foo',
'foo bar', 'foo bar',
'foo/bar.tmp', 'foo/bar.tmp',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php', 'test.php',
'test.py', 'test.py',
'toto', 'toto',
@ -92,6 +101,7 @@ class SortableIteratorTest extends RealIteratorTestCase
'.foo', '.foo',
'.git', '.git',
'foo', 'foo',
'qux',
'toto', 'toto',
'toto/.git', 'toto/.git',
'.bar', '.bar',
@ -99,25 +109,18 @@ class SortableIteratorTest extends RealIteratorTestCase
'.foo/bar', '.foo/bar',
'foo bar', 'foo bar',
'foo/bar.tmp', 'foo/bar.tmp',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php', 'test.php',
'test.py', 'test.py',
); );
$customComparison = array(
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'.git',
'foo',
'foo bar',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'toto/.git',
);
$sortByAccessedTime = array( $sortByAccessedTime = array(
// For these two files the access time was set to 2005-10-15 // For these two files the access time was set to 2005-10-15
array('foo/bar.tmp', 'test.php'), array('foo/bar.tmp', 'test.php'),
@ -132,6 +135,15 @@ class SortableIteratorTest extends RealIteratorTestCase
'toto', 'toto',
'toto/.git', 'toto/.git',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
), ),
// This file was accessed after sleeping for 1 sec // This file was accessed after sleeping for 1 sec
array('.bar'), array('.bar'),
@ -149,6 +161,15 @@ class SortableIteratorTest extends RealIteratorTestCase
'toto', 'toto',
'toto/.git', 'toto/.git',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
), ),
array('test.php'), array('test.php'),
array('test.py'), array('test.py'),
@ -166,17 +187,75 @@ class SortableIteratorTest extends RealIteratorTestCase
'toto', 'toto',
'toto/.git', 'toto/.git',
'foo bar', 'foo bar',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
), ),
array('test.php'), array('test.php'),
array('test.py'), array('test.py'),
); );
$sortByNameNatural = array(
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'.git',
'foo',
'foo/bar.tmp',
'foo bar',
'qux',
'qux/baz_1_2.py',
'qux/baz_100_1.py',
'qux_0_1.php',
'qux_2_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_1000_1.php',
'qux_1002_0.php',
'test.php',
'test.py',
'toto',
'toto/.git',
);
$customComparison = array(
'.bar',
'.foo',
'.foo/.bar',
'.foo/bar',
'.git',
'foo',
'foo bar',
'foo/bar.tmp',
'qux',
'qux/baz_100_1.py',
'qux/baz_1_2.py',
'qux_0_1.php',
'qux_1000_1.php',
'qux_1002_0.php',
'qux_10_2.php',
'qux_12_0.php',
'qux_2_0.php',
'test.php',
'test.py',
'toto',
'toto/.git',
);
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_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)),
array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)), array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)),
array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)), array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)),
array(SortableIterator::SORT_BY_NAME_NATURAL, $this->toAbsolute($sortByNameNatural)),
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)),
); );
} }