Yaffs site version 1.1
[yaffs-website] / vendor / symfony / finder / Tests / Iterator / SortableIteratorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Finder\Tests\Iterator;
13
14 use Symfony\Component\Finder\Iterator\SortableIterator;
15
16 class SortableIteratorTest extends RealIteratorTestCase
17 {
18     public function testConstructor()
19     {
20         try {
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');
25         }
26     }
27
28     /**
29      * @dataProvider getAcceptData
30      */
31     public function testAccept($mode, $expected)
32     {
33         if (!is_callable($mode)) {
34             switch ($mode) {
35                 case SortableIterator::SORT_BY_ACCESSED_TIME:
36                     if ('\\' === DIRECTORY_SEPARATOR) {
37                         touch(self::toAbsolute('.git'));
38                     } else {
39                         file_get_contents(self::toAbsolute('.git'));
40                     }
41                     sleep(1);
42                     file_get_contents(self::toAbsolute('.bar'));
43                     break;
44                 case SortableIterator::SORT_BY_CHANGED_TIME:
45                     file_put_contents(self::toAbsolute('test.php'), 'foo');
46                     sleep(1);
47                     file_put_contents(self::toAbsolute('test.py'), 'foo');
48                     break;
49                 case SortableIterator::SORT_BY_MODIFIED_TIME:
50                     file_put_contents(self::toAbsolute('test.php'), 'foo');
51                     sleep(1);
52                     file_put_contents(self::toAbsolute('test.py'), 'foo');
53                     break;
54             }
55         }
56
57         $inner = new Iterator(self::$files);
58
59         $iterator = new SortableIterator($inner, $mode);
60
61         if ($mode === SortableIterator::SORT_BY_ACCESSED_TIME
62             || $mode === SortableIterator::SORT_BY_CHANGED_TIME
63             || $mode === SortableIterator::SORT_BY_MODIFIED_TIME
64         ) {
65             if ('\\' === DIRECTORY_SEPARATOR && SortableIterator::SORT_BY_MODIFIED_TIME !== $mode) {
66                 $this->markTestSkipped('Sorting by atime or ctime is not supported on Windows');
67             }
68             $this->assertOrderedIteratorForGroups($expected, $iterator);
69         } else {
70             $this->assertOrderedIterator($expected, $iterator);
71         }
72     }
73
74     public function getAcceptData()
75     {
76         $sortByName = array(
77             '.bar',
78             '.foo',
79             '.foo/.bar',
80             '.foo/bar',
81             '.git',
82             'foo',
83             'foo bar',
84             'foo/bar.tmp',
85             'test.php',
86             'test.py',
87             'toto',
88             'toto/.git',
89         );
90
91         $sortByType = array(
92             '.foo',
93             '.git',
94             'foo',
95             'toto',
96             'toto/.git',
97             '.bar',
98             '.foo/.bar',
99             '.foo/bar',
100             'foo bar',
101             'foo/bar.tmp',
102             'test.php',
103             'test.py',
104         );
105
106         $customComparison = array(
107             '.bar',
108             '.foo',
109             '.foo/.bar',
110             '.foo/bar',
111             '.git',
112             'foo',
113             'foo bar',
114             'foo/bar.tmp',
115             'test.php',
116             'test.py',
117             'toto',
118             'toto/.git',
119         );
120
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
125             array(
126                 '.git',
127                 '.foo',
128                 '.foo/.bar',
129                 '.foo/bar',
130                 'test.py',
131                 'foo',
132                 'toto',
133                 'toto/.git',
134                 'foo bar',
135             ),
136             // This file was accessed after sleeping for 1 sec
137             array('.bar'),
138         );
139
140         $sortByChangedTime = array(
141             array(
142                 '.git',
143                 '.foo',
144                 '.foo/.bar',
145                 '.foo/bar',
146                 '.bar',
147                 'foo',
148                 'foo/bar.tmp',
149                 'toto',
150                 'toto/.git',
151                 'foo bar',
152             ),
153             array('test.php'),
154             array('test.py'),
155         );
156
157         $sortByModifiedTime = array(
158             array(
159                 '.git',
160                 '.foo',
161                 '.foo/.bar',
162                 '.foo/bar',
163                 '.bar',
164                 'foo',
165                 'foo/bar.tmp',
166                 'toto',
167                 'toto/.git',
168                 'foo bar',
169             ),
170             array('test.php'),
171             array('test.py'),
172         );
173
174         return 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)),
181         );
182     }
183 }