4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Tests\Iterator;
14 use Symfony\Component\Finder\Iterator\SortableIterator;
16 class SortableIteratorTest extends RealIteratorTestCase
18 public function testConstructor()
21 new SortableIterator(new Iterator(array()), 'foobar');
22 $this->fail('__construct() throws an \InvalidArgumentException exception if the mode is not valid');
23 } catch (\Exception $e) {
24 $this->assertInstanceOf('InvalidArgumentException', $e, '__construct() throws an \InvalidArgumentException exception if the mode is not valid');
29 * @dataProvider getAcceptData
31 public function testAccept($mode, $expected)
33 if (!is_callable($mode)) {
35 case SortableIterator::SORT_BY_ACCESSED_TIME:
36 if ('\\' === DIRECTORY_SEPARATOR) {
37 touch(self::toAbsolute('.git'));
39 file_get_contents(self::toAbsolute('.git'));
42 file_get_contents(self::toAbsolute('.bar'));
44 case SortableIterator::SORT_BY_CHANGED_TIME:
45 file_put_contents(self::toAbsolute('test.php'), 'foo');
47 file_put_contents(self::toAbsolute('test.py'), 'foo');
49 case SortableIterator::SORT_BY_MODIFIED_TIME:
50 file_put_contents(self::toAbsolute('test.php'), 'foo');
52 file_put_contents(self::toAbsolute('test.py'), 'foo');
57 $inner = new Iterator(self::$files);
59 $iterator = new SortableIterator($inner, $mode);
61 if ($mode === SortableIterator::SORT_BY_ACCESSED_TIME
62 || $mode === SortableIterator::SORT_BY_CHANGED_TIME
63 || $mode === SortableIterator::SORT_BY_MODIFIED_TIME
65 if ('\\' === DIRECTORY_SEPARATOR && SortableIterator::SORT_BY_MODIFIED_TIME !== $mode) {
66 $this->markTestSkipped('Sorting by atime or ctime is not supported on Windows');
68 $this->assertOrderedIteratorForGroups($expected, $iterator);
70 $this->assertOrderedIterator($expected, $iterator);
74 public function getAcceptData()
106 $customComparison = array(
121 $sortByAccessedTime = array(
122 // For these two files the access time was set to 2005-10-15
123 array('foo/bar.tmp', 'test.php'),
124 // These files were created more or less at the same time
136 // This file was accessed after sleeping for 1 sec
140 $sortByChangedTime = array(
157 $sortByModifiedTime = array(
175 array(SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)),
176 array(SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)),
177 array(SortableIterator::SORT_BY_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)),
178 array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)),
179 array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)),
180 array(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }, $this->toAbsolute($customComparison)),