TYPO3  7.6
SortableIteratorTest.php
Go to the documentation of this file.
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 
15 
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 
31  public function testAccept($mode, $expected)
32  {
33  if (!is_callable($mode)) {
34  switch ($mode) {
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;
45  file_put_contents(self::toAbsolute('test.php'), 'foo');
46  sleep(1);
47  file_put_contents(self::toAbsolute('test.py'), 'foo');
48  break;
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 
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  );
89 
90  $sortByType = array(
91  '.foo',
92  '.git',
93  'foo',
94  'toto',
95  '.bar',
96  '.foo/.bar',
97  '.foo/bar',
98  'foo bar',
99  'foo/bar.tmp',
100  'test.php',
101  'test.py',
102  );
103 
104  $customComparison = array(
105  '.bar',
106  '.foo',
107  '.foo/.bar',
108  '.foo/bar',
109  '.git',
110  'foo',
111  'foo bar',
112  'foo/bar.tmp',
113  'test.php',
114  'test.py',
115  'toto',
116  );
117 
118  $sortByAccessedTime = array(
119  // For these two files the access time was set to 2005-10-15
120  array('foo/bar.tmp', 'test.php'),
121  // These files were created more or less at the same time
122  array(
123  '.git',
124  '.foo',
125  '.foo/.bar',
126  '.foo/bar',
127  'test.py',
128  'foo',
129  'toto',
130  'foo bar',
131  ),
132  // This file was accessed after sleeping for 1 sec
133  array('.bar'),
134  );
135 
136  $sortByChangedTime = array(
137  array(
138  '.git',
139  '.foo',
140  '.foo/.bar',
141  '.foo/bar',
142  '.bar',
143  'foo',
144  'foo/bar.tmp',
145  'toto',
146  'foo bar',
147  ),
148  array('test.php'),
149  array('test.py'),
150  );
151 
152  $sortByModifiedTime = array(
153  array(
154  '.git',
155  '.foo',
156  '.foo/.bar',
157  '.foo/bar',
158  '.bar',
159  'foo',
160  'foo/bar.tmp',
161  'toto',
162  'foo bar',
163  ),
164  array('test.php'),
165  array('test.py'),
166  );
167 
168  return array(
169  array(SortableIterator::SORT_BY_NAME, $this->toAbsolute($sortByName)),
170  array(SortableIterator::SORT_BY_TYPE, $this->toAbsolute($sortByType)),
171  array(SortableIterator::SORT_BY_ACCESSED_TIME, $this->toAbsolute($sortByAccessedTime)),
172  array(SortableIterator::SORT_BY_CHANGED_TIME, $this->toAbsolute($sortByChangedTime)),
173  array(SortableIterator::SORT_BY_MODIFIED_TIME, $this->toAbsolute($sortByModifiedTime)),
174  array(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }, $this->toAbsolute($customComparison)),
175  );
176  }
177 }