Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Finder\Tests;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Finder\Finder;
|
Chris@0
|
15
|
Chris@0
|
16 class FinderTest extends Iterator\RealIteratorTestCase
|
Chris@0
|
17 {
|
Chris@0
|
18 public function testCreate()
|
Chris@0
|
19 {
|
Chris@0
|
20 $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
|
Chris@0
|
21 }
|
Chris@0
|
22
|
Chris@0
|
23 public function testDirectories()
|
Chris@0
|
24 {
|
Chris@0
|
25 $finder = $this->buildFinder();
|
Chris@0
|
26 $this->assertSame($finder, $finder->directories());
|
Chris@0
|
27 $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
28
|
Chris@0
|
29 $finder = $this->buildFinder();
|
Chris@0
|
30 $finder->directories();
|
Chris@0
|
31 $finder->files();
|
Chris@0
|
32 $finder->directories();
|
Chris@0
|
33 $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 public function testFiles()
|
Chris@0
|
37 {
|
Chris@0
|
38 $finder = $this->buildFinder();
|
Chris@0
|
39 $this->assertSame($finder, $finder->files());
|
Chris@0
|
40 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
41
|
Chris@0
|
42 $finder = $this->buildFinder();
|
Chris@0
|
43 $finder->files();
|
Chris@0
|
44 $finder->directories();
|
Chris@0
|
45 $finder->files();
|
Chris@0
|
46 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@13
|
49 public function testRemoveTrailingSlash()
|
Chris@13
|
50 {
|
Chris@13
|
51 $finder = $this->buildFinder();
|
Chris@13
|
52
|
Chris@13
|
53 $expected = $this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py', 'foo bar'));
|
Chris@13
|
54 $in = self::$tmpDir.'//';
|
Chris@13
|
55
|
Chris@13
|
56 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
|
Chris@13
|
57 }
|
Chris@13
|
58
|
Chris@13
|
59 public function testSymlinksNotResolved()
|
Chris@13
|
60 {
|
Chris@13
|
61 if ('\\' === DIRECTORY_SEPARATOR) {
|
Chris@13
|
62 $this->markTestSkipped('symlinks are not supported on Windows');
|
Chris@13
|
63 }
|
Chris@13
|
64
|
Chris@13
|
65 $finder = $this->buildFinder();
|
Chris@13
|
66
|
Chris@13
|
67 symlink($this->toAbsolute('foo'), $this->toAbsolute('baz'));
|
Chris@13
|
68 $expected = $this->toAbsolute(array('baz/bar.tmp'));
|
Chris@13
|
69 $in = self::$tmpDir.'/baz/';
|
Chris@13
|
70 try {
|
Chris@13
|
71 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
|
Chris@13
|
72 unlink($this->toAbsolute('baz'));
|
Chris@13
|
73 } catch (\Exception $e) {
|
Chris@13
|
74 unlink($this->toAbsolute('baz'));
|
Chris@13
|
75 throw $e;
|
Chris@13
|
76 }
|
Chris@13
|
77 }
|
Chris@13
|
78
|
Chris@13
|
79 public function testBackPathNotNormalized()
|
Chris@13
|
80 {
|
Chris@13
|
81 $finder = $this->buildFinder();
|
Chris@13
|
82
|
Chris@13
|
83 $expected = $this->toAbsolute(array('foo/../foo/bar.tmp'));
|
Chris@13
|
84 $in = self::$tmpDir.'/foo/../foo/';
|
Chris@13
|
85 $this->assertIterator($expected, $finder->in($in)->files()->getIterator());
|
Chris@13
|
86 }
|
Chris@13
|
87
|
Chris@0
|
88 public function testDepth()
|
Chris@0
|
89 {
|
Chris@0
|
90 $finder = $this->buildFinder();
|
Chris@0
|
91 $this->assertSame($finder, $finder->depth('< 1'));
|
Chris@0
|
92 $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
93
|
Chris@0
|
94 $finder = $this->buildFinder();
|
Chris@0
|
95 $this->assertSame($finder, $finder->depth('<= 0'));
|
Chris@0
|
96 $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
97
|
Chris@0
|
98 $finder = $this->buildFinder();
|
Chris@0
|
99 $this->assertSame($finder, $finder->depth('>= 1'));
|
Chris@0
|
100 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
101
|
Chris@0
|
102 $finder = $this->buildFinder();
|
Chris@0
|
103 $finder->depth('< 1')->depth('>= 1');
|
Chris@0
|
104 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 public function testName()
|
Chris@0
|
108 {
|
Chris@0
|
109 $finder = $this->buildFinder();
|
Chris@0
|
110 $this->assertSame($finder, $finder->name('*.php'));
|
Chris@0
|
111 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
112
|
Chris@0
|
113 $finder = $this->buildFinder();
|
Chris@0
|
114 $finder->name('test.ph*');
|
Chris@0
|
115 $finder->name('test.py');
|
Chris@0
|
116 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
117
|
Chris@0
|
118 $finder = $this->buildFinder();
|
Chris@0
|
119 $finder->name('~^test~i');
|
Chris@0
|
120 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
121
|
Chris@0
|
122 $finder = $this->buildFinder();
|
Chris@0
|
123 $finder->name('~\\.php$~i');
|
Chris@0
|
124 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
125
|
Chris@0
|
126 $finder = $this->buildFinder();
|
Chris@0
|
127 $finder->name('test.p{hp,y}');
|
Chris@0
|
128 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 public function testNotName()
|
Chris@0
|
132 {
|
Chris@0
|
133 $finder = $this->buildFinder();
|
Chris@0
|
134 $this->assertSame($finder, $finder->notName('*.php'));
|
Chris@0
|
135 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
136
|
Chris@0
|
137 $finder = $this->buildFinder();
|
Chris@0
|
138 $finder->notName('*.php');
|
Chris@0
|
139 $finder->notName('*.py');
|
Chris@0
|
140 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
141
|
Chris@0
|
142 $finder = $this->buildFinder();
|
Chris@0
|
143 $finder->name('test.ph*');
|
Chris@0
|
144 $finder->name('test.py');
|
Chris@0
|
145 $finder->notName('*.php');
|
Chris@0
|
146 $finder->notName('*.py');
|
Chris@0
|
147 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
148
|
Chris@0
|
149 $finder = $this->buildFinder();
|
Chris@0
|
150 $finder->name('test.ph*');
|
Chris@0
|
151 $finder->name('test.py');
|
Chris@0
|
152 $finder->notName('*.p{hp,y}');
|
Chris@0
|
153 $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * @dataProvider getRegexNameTestData
|
Chris@0
|
158 */
|
Chris@0
|
159 public function testRegexName($regex)
|
Chris@0
|
160 {
|
Chris@0
|
161 $finder = $this->buildFinder();
|
Chris@0
|
162 $finder->name($regex);
|
Chris@0
|
163 $this->assertIterator($this->toAbsolute(array('test.py', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 public function testSize()
|
Chris@0
|
167 {
|
Chris@0
|
168 $finder = $this->buildFinder();
|
Chris@0
|
169 $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
|
Chris@0
|
170 $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 public function testDate()
|
Chris@0
|
174 {
|
Chris@0
|
175 $finder = $this->buildFinder();
|
Chris@0
|
176 $this->assertSame($finder, $finder->files()->date('until last month'));
|
Chris@0
|
177 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 public function testExclude()
|
Chris@0
|
181 {
|
Chris@0
|
182 $finder = $this->buildFinder();
|
Chris@0
|
183 $this->assertSame($finder, $finder->exclude('foo'));
|
Chris@0
|
184 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@0
|
187 public function testIgnoreVCS()
|
Chris@0
|
188 {
|
Chris@0
|
189 $finder = $this->buildFinder();
|
Chris@0
|
190 $this->assertSame($finder, $finder->ignoreVCS(false)->ignoreDotFiles(false));
|
Chris@0
|
191 $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());
|
Chris@0
|
192
|
Chris@0
|
193 $finder = $this->buildFinder();
|
Chris@0
|
194 $finder->ignoreVCS(false)->ignoreVCS(false)->ignoreDotFiles(false);
|
Chris@0
|
195 $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());
|
Chris@0
|
196
|
Chris@0
|
197 $finder = $this->buildFinder();
|
Chris@0
|
198 $this->assertSame($finder, $finder->ignoreVCS(true)->ignoreDotFiles(false));
|
Chris@0
|
199 $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());
|
Chris@0
|
200 }
|
Chris@0
|
201
|
Chris@0
|
202 public function testIgnoreDotFiles()
|
Chris@0
|
203 {
|
Chris@0
|
204 $finder = $this->buildFinder();
|
Chris@0
|
205 $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
|
Chris@0
|
206 $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());
|
Chris@0
|
207
|
Chris@0
|
208 $finder = $this->buildFinder();
|
Chris@0
|
209 $finder->ignoreDotFiles(false)->ignoreDotFiles(false)->ignoreVCS(false);
|
Chris@0
|
210 $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());
|
Chris@0
|
211
|
Chris@0
|
212 $finder = $this->buildFinder();
|
Chris@0
|
213 $this->assertSame($finder, $finder->ignoreDotFiles(true)->ignoreVCS(false));
|
Chris@0
|
214 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 public function testSortByName()
|
Chris@0
|
218 {
|
Chris@0
|
219 $finder = $this->buildFinder();
|
Chris@0
|
220 $this->assertSame($finder, $finder->sortByName());
|
Chris@0
|
221 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 public function testSortByType()
|
Chris@0
|
225 {
|
Chris@0
|
226 $finder = $this->buildFinder();
|
Chris@0
|
227 $this->assertSame($finder, $finder->sortByType());
|
Chris@0
|
228 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 public function testSortByAccessedTime()
|
Chris@0
|
232 {
|
Chris@0
|
233 $finder = $this->buildFinder();
|
Chris@0
|
234 $this->assertSame($finder, $finder->sortByAccessedTime());
|
Chris@0
|
235 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 public function testSortByChangedTime()
|
Chris@0
|
239 {
|
Chris@0
|
240 $finder = $this->buildFinder();
|
Chris@0
|
241 $this->assertSame($finder, $finder->sortByChangedTime());
|
Chris@0
|
242 $this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 public function testSortByModifiedTime()
|
Chris@0
|
246 {
|
Chris@0
|
247 $finder = $this->buildFinder();
|
Chris@0
|
248 $this->assertSame($finder, $finder->sortByModifiedTime());
|
Chris@0
|
249 $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
250 }
|
Chris@0
|
251
|
Chris@0
|
252 public function testSort()
|
Chris@0
|
253 {
|
Chris@0
|
254 $finder = $this->buildFinder();
|
Chris@0
|
255 $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealPath(), $b->getRealPath()); }));
|
Chris@0
|
256 $this->assertIterator($this->toAbsolute(array('foo', 'foo bar', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
257 }
|
Chris@0
|
258
|
Chris@0
|
259 public function testFilter()
|
Chris@0
|
260 {
|
Chris@0
|
261 $finder = $this->buildFinder();
|
Chris@0
|
262 $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
|
Chris@0
|
263 $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
264 }
|
Chris@0
|
265
|
Chris@0
|
266 public function testFollowLinks()
|
Chris@0
|
267 {
|
Chris@0
|
268 if ('\\' == DIRECTORY_SEPARATOR) {
|
Chris@0
|
269 $this->markTestSkipped('symlinks are not supported on Windows');
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 $finder = $this->buildFinder();
|
Chris@0
|
273 $this->assertSame($finder, $finder->followLinks());
|
Chris@0
|
274 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto', 'foo bar')), $finder->in(self::$tmpDir)->getIterator());
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 public function testIn()
|
Chris@0
|
278 {
|
Chris@0
|
279 $finder = $this->buildFinder();
|
Chris@0
|
280 $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
|
Chris@0
|
281
|
Chris@0
|
282 $expected = array(
|
Chris@0
|
283 self::$tmpDir.DIRECTORY_SEPARATOR.'test.php',
|
Chris@0
|
284 __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php',
|
Chris@0
|
285 __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php',
|
Chris@0
|
286 );
|
Chris@0
|
287
|
Chris@0
|
288 $this->assertIterator($expected, $iterator);
|
Chris@0
|
289 }
|
Chris@0
|
290
|
Chris@0
|
291 /**
|
Chris@0
|
292 * @expectedException \InvalidArgumentException
|
Chris@0
|
293 */
|
Chris@0
|
294 public function testInWithNonExistentDirectory()
|
Chris@0
|
295 {
|
Chris@0
|
296 $finder = new Finder();
|
Chris@0
|
297 $finder->in('foobar');
|
Chris@0
|
298 }
|
Chris@0
|
299
|
Chris@0
|
300 public function testInWithGlob()
|
Chris@0
|
301 {
|
Chris@0
|
302 $finder = $this->buildFinder();
|
Chris@13
|
303 $finder->in(array(__DIR__.'/Fixtures/*/B/C/', __DIR__.'/Fixtures/*/*/B/C/'))->getIterator();
|
Chris@0
|
304
|
Chris@0
|
305 $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 /**
|
Chris@0
|
309 * @expectedException \InvalidArgumentException
|
Chris@0
|
310 */
|
Chris@0
|
311 public function testInWithNonDirectoryGlob()
|
Chris@0
|
312 {
|
Chris@0
|
313 $finder = new Finder();
|
Chris@0
|
314 $finder->in(__DIR__.'/Fixtures/A/a*');
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 public function testInWithGlobBrace()
|
Chris@0
|
318 {
|
Chris@0
|
319 $finder = $this->buildFinder();
|
Chris@0
|
320 $finder->in(array(__DIR__.'/Fixtures/{A,copy/A}/B/C'))->getIterator();
|
Chris@0
|
321
|
Chris@0
|
322 $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
|
Chris@0
|
323 }
|
Chris@0
|
324
|
Chris@0
|
325 /**
|
Chris@0
|
326 * @expectedException \LogicException
|
Chris@0
|
327 */
|
Chris@0
|
328 public function testGetIteratorWithoutIn()
|
Chris@0
|
329 {
|
Chris@0
|
330 $finder = Finder::create();
|
Chris@0
|
331 $finder->getIterator();
|
Chris@0
|
332 }
|
Chris@0
|
333
|
Chris@0
|
334 public function testGetIterator()
|
Chris@0
|
335 {
|
Chris@0
|
336 $finder = $this->buildFinder();
|
Chris@0
|
337 $dirs = array();
|
Chris@0
|
338 foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
|
Chris@0
|
339 $dirs[] = (string) $dir;
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 $expected = $this->toAbsolute(array('foo', 'toto'));
|
Chris@0
|
343
|
Chris@0
|
344 sort($dirs);
|
Chris@0
|
345 sort($expected);
|
Chris@0
|
346
|
Chris@0
|
347 $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
|
Chris@0
|
348
|
Chris@0
|
349 $finder = $this->buildFinder();
|
Chris@0
|
350 $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
|
Chris@0
|
351
|
Chris@0
|
352 $finder = $this->buildFinder();
|
Chris@0
|
353 $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
|
Chris@0
|
354 $a = array_values(array_map('strval', $a));
|
Chris@0
|
355 sort($a);
|
Chris@0
|
356 $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 public function testRelativePath()
|
Chris@0
|
360 {
|
Chris@0
|
361 $finder = $this->buildFinder()->in(self::$tmpDir);
|
Chris@0
|
362
|
Chris@0
|
363 $paths = array();
|
Chris@0
|
364
|
Chris@0
|
365 foreach ($finder as $file) {
|
Chris@0
|
366 $paths[] = $file->getRelativePath();
|
Chris@0
|
367 }
|
Chris@0
|
368
|
Chris@0
|
369 $ref = array('', '', '', '', 'foo', '');
|
Chris@0
|
370
|
Chris@0
|
371 sort($ref);
|
Chris@0
|
372 sort($paths);
|
Chris@0
|
373
|
Chris@0
|
374 $this->assertEquals($ref, $paths);
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 public function testRelativePathname()
|
Chris@0
|
378 {
|
Chris@0
|
379 $finder = $this->buildFinder()->in(self::$tmpDir)->sortByName();
|
Chris@0
|
380
|
Chris@0
|
381 $paths = array();
|
Chris@0
|
382
|
Chris@0
|
383 foreach ($finder as $file) {
|
Chris@0
|
384 $paths[] = $file->getRelativePathname();
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 $ref = array('test.php', 'toto', 'test.py', 'foo', 'foo'.DIRECTORY_SEPARATOR.'bar.tmp', 'foo bar');
|
Chris@0
|
388
|
Chris@0
|
389 sort($paths);
|
Chris@0
|
390 sort($ref);
|
Chris@0
|
391
|
Chris@0
|
392 $this->assertEquals($ref, $paths);
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 public function testAppendWithAFinder()
|
Chris@0
|
396 {
|
Chris@0
|
397 $finder = $this->buildFinder();
|
Chris@0
|
398 $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
|
Chris@0
|
399
|
Chris@0
|
400 $finder1 = $this->buildFinder();
|
Chris@0
|
401 $finder1->directories()->in(self::$tmpDir);
|
Chris@0
|
402
|
Chris@0
|
403 $finder = $finder->append($finder1);
|
Chris@0
|
404
|
Chris@0
|
405 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
|
Chris@0
|
406 }
|
Chris@0
|
407
|
Chris@0
|
408 public function testAppendWithAnArray()
|
Chris@0
|
409 {
|
Chris@0
|
410 $finder = $this->buildFinder();
|
Chris@0
|
411 $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
|
Chris@0
|
412
|
Chris@0
|
413 $finder->append($this->toAbsolute(array('foo', 'toto')));
|
Chris@0
|
414
|
Chris@0
|
415 $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
|
Chris@0
|
416 }
|
Chris@0
|
417
|
Chris@0
|
418 public function testAppendReturnsAFinder()
|
Chris@0
|
419 {
|
Chris@0
|
420 $this->assertInstanceOf('Symfony\\Component\\Finder\\Finder', Finder::create()->append(array()));
|
Chris@0
|
421 }
|
Chris@0
|
422
|
Chris@0
|
423 public function testAppendDoesNotRequireIn()
|
Chris@0
|
424 {
|
Chris@0
|
425 $finder = $this->buildFinder();
|
Chris@0
|
426 $finder->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
|
Chris@0
|
427
|
Chris@0
|
428 $finder1 = Finder::create()->append($finder);
|
Chris@0
|
429
|
Chris@0
|
430 $this->assertIterator(iterator_to_array($finder->getIterator()), $finder1->getIterator());
|
Chris@0
|
431 }
|
Chris@0
|
432
|
Chris@0
|
433 public function testCountDirectories()
|
Chris@0
|
434 {
|
Chris@0
|
435 $directory = Finder::create()->directories()->in(self::$tmpDir);
|
Chris@0
|
436 $i = 0;
|
Chris@0
|
437
|
Chris@0
|
438 foreach ($directory as $dir) {
|
Chris@0
|
439 ++$i;
|
Chris@0
|
440 }
|
Chris@0
|
441
|
Chris@0
|
442 $this->assertCount($i, $directory);
|
Chris@0
|
443 }
|
Chris@0
|
444
|
Chris@0
|
445 public function testCountFiles()
|
Chris@0
|
446 {
|
Chris@0
|
447 $files = Finder::create()->files()->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures');
|
Chris@0
|
448 $i = 0;
|
Chris@0
|
449
|
Chris@0
|
450 foreach ($files as $file) {
|
Chris@0
|
451 ++$i;
|
Chris@0
|
452 }
|
Chris@0
|
453
|
Chris@0
|
454 $this->assertCount($i, $files);
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 /**
|
Chris@0
|
458 * @expectedException \LogicException
|
Chris@0
|
459 */
|
Chris@0
|
460 public function testCountWithoutIn()
|
Chris@0
|
461 {
|
Chris@0
|
462 $finder = Finder::create()->files();
|
Chris@0
|
463 count($finder);
|
Chris@0
|
464 }
|
Chris@0
|
465
|
Chris@12
|
466 public function testHasResults()
|
Chris@12
|
467 {
|
Chris@12
|
468 $finder = $this->buildFinder();
|
Chris@12
|
469 $finder->in(__DIR__);
|
Chris@12
|
470 $this->assertTrue($finder->hasResults());
|
Chris@12
|
471 }
|
Chris@12
|
472
|
Chris@12
|
473 public function testNoResults()
|
Chris@12
|
474 {
|
Chris@12
|
475 $finder = $this->buildFinder();
|
Chris@12
|
476 $finder->in(__DIR__)->name('DoesNotExist');
|
Chris@12
|
477 $this->assertFalse($finder->hasResults());
|
Chris@12
|
478 }
|
Chris@12
|
479
|
Chris@0
|
480 /**
|
Chris@0
|
481 * @dataProvider getContainsTestData
|
Chris@0
|
482 */
|
Chris@0
|
483 public function testContains($matchPatterns, $noMatchPatterns, $expected)
|
Chris@0
|
484 {
|
Chris@0
|
485 $finder = $this->buildFinder();
|
Chris@0
|
486 $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
|
Chris@0
|
487 ->name('*.txt')->sortByName()
|
Chris@0
|
488 ->contains($matchPatterns)
|
Chris@0
|
489 ->notContains($noMatchPatterns);
|
Chris@0
|
490
|
Chris@0
|
491 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 public function testContainsOnDirectory()
|
Chris@0
|
495 {
|
Chris@0
|
496 $finder = $this->buildFinder();
|
Chris@0
|
497 $finder->in(__DIR__)
|
Chris@0
|
498 ->directories()
|
Chris@0
|
499 ->name('Fixtures')
|
Chris@0
|
500 ->contains('abc');
|
Chris@0
|
501 $this->assertIterator(array(), $finder);
|
Chris@0
|
502 }
|
Chris@0
|
503
|
Chris@0
|
504 public function testNotContainsOnDirectory()
|
Chris@0
|
505 {
|
Chris@0
|
506 $finder = $this->buildFinder();
|
Chris@0
|
507 $finder->in(__DIR__)
|
Chris@0
|
508 ->directories()
|
Chris@0
|
509 ->name('Fixtures')
|
Chris@0
|
510 ->notContains('abc');
|
Chris@0
|
511 $this->assertIterator(array(), $finder);
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 /**
|
Chris@0
|
515 * Searching in multiple locations involves AppendIterator which does an unnecessary rewind which leaves FilterIterator
|
Chris@0
|
516 * with inner FilesystemIterator in an invalid state.
|
Chris@0
|
517 *
|
Chris@0
|
518 * @see https://bugs.php.net/68557
|
Chris@0
|
519 */
|
Chris@0
|
520 public function testMultipleLocations()
|
Chris@0
|
521 {
|
Chris@0
|
522 $locations = array(
|
Chris@0
|
523 self::$tmpDir.'/',
|
Chris@0
|
524 self::$tmpDir.'/toto/',
|
Chris@0
|
525 );
|
Chris@0
|
526
|
Chris@0
|
527 // it is expected that there are test.py test.php in the tmpDir
|
Chris@0
|
528 $finder = new Finder();
|
Chris@0
|
529 $finder->in($locations)
|
Chris@0
|
530 // the default flag IGNORE_DOT_FILES fixes the problem indirectly
|
Chris@0
|
531 // so we set it to false for better isolation
|
Chris@0
|
532 ->ignoreDotFiles(false)
|
Chris@0
|
533 ->depth('< 1')->name('test.php');
|
Chris@0
|
534
|
Chris@0
|
535 $this->assertCount(1, $finder);
|
Chris@0
|
536 }
|
Chris@0
|
537
|
Chris@0
|
538 /**
|
Chris@0
|
539 * Searching in multiple locations with sub directories involves
|
Chris@0
|
540 * AppendIterator which does an unnecessary rewind which leaves
|
Chris@0
|
541 * FilterIterator with inner FilesystemIterator in an invalid state.
|
Chris@0
|
542 *
|
Chris@0
|
543 * @see https://bugs.php.net/68557
|
Chris@0
|
544 */
|
Chris@0
|
545 public function testMultipleLocationsWithSubDirectories()
|
Chris@0
|
546 {
|
Chris@0
|
547 $locations = array(
|
Chris@0
|
548 __DIR__.'/Fixtures/one',
|
Chris@0
|
549 self::$tmpDir.DIRECTORY_SEPARATOR.'toto',
|
Chris@0
|
550 );
|
Chris@0
|
551
|
Chris@0
|
552 $finder = $this->buildFinder();
|
Chris@0
|
553 $finder->in($locations)->depth('< 10')->name('*.neon');
|
Chris@0
|
554
|
Chris@0
|
555 $expected = array(
|
Chris@0
|
556 __DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'c.neon',
|
Chris@0
|
557 __DIR__.'/Fixtures/one'.DIRECTORY_SEPARATOR.'b'.DIRECTORY_SEPARATOR.'d.neon',
|
Chris@0
|
558 );
|
Chris@0
|
559
|
Chris@0
|
560 $this->assertIterator($expected, $finder);
|
Chris@0
|
561 $this->assertIteratorInForeach($expected, $finder);
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 /**
|
Chris@0
|
565 * Iterator keys must be the file pathname.
|
Chris@0
|
566 */
|
Chris@0
|
567 public function testIteratorKeys()
|
Chris@0
|
568 {
|
Chris@0
|
569 $finder = $this->buildFinder()->in(self::$tmpDir);
|
Chris@0
|
570 foreach ($finder as $key => $file) {
|
Chris@0
|
571 $this->assertEquals($file->getPathname(), $key);
|
Chris@0
|
572 }
|
Chris@0
|
573 }
|
Chris@0
|
574
|
Chris@0
|
575 public function testRegexSpecialCharsLocationWithPathRestrictionContainingStartFlag()
|
Chris@0
|
576 {
|
Chris@0
|
577 $finder = $this->buildFinder();
|
Chris@0
|
578 $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'r+e.gex[c]a(r)s')
|
Chris@0
|
579 ->path('/^dir/');
|
Chris@0
|
580
|
Chris@0
|
581 $expected = array('r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.'dir', 'r+e.gex[c]a(r)s'.DIRECTORY_SEPARATOR.'dir'.DIRECTORY_SEPARATOR.'bar.dat');
|
Chris@0
|
582 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
|
Chris@0
|
583 }
|
Chris@0
|
584
|
Chris@0
|
585 public function getContainsTestData()
|
Chris@0
|
586 {
|
Chris@0
|
587 return array(
|
Chris@0
|
588 array('', '', array()),
|
Chris@0
|
589 array('foo', 'bar', array()),
|
Chris@0
|
590 array('', 'foobar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
|
Chris@0
|
591 array('lorem ipsum dolor sit amet', 'foobar', array('lorem.txt')),
|
Chris@0
|
592 array('sit', 'bar', array('dolor.txt', 'ipsum.txt', 'lorem.txt')),
|
Chris@0
|
593 array('dolor sit amet', '@^L@m', array('dolor.txt', 'ipsum.txt')),
|
Chris@0
|
594 array('/^lorem ipsum dolor sit amet$/m', 'foobar', array('lorem.txt')),
|
Chris@0
|
595 array('lorem', 'foobar', array('lorem.txt')),
|
Chris@0
|
596 array('', 'lorem', array('dolor.txt', 'ipsum.txt')),
|
Chris@0
|
597 array('ipsum dolor sit amet', '/^IPSUM/m', array('lorem.txt')),
|
Chris@0
|
598 );
|
Chris@0
|
599 }
|
Chris@0
|
600
|
Chris@0
|
601 public function getRegexNameTestData()
|
Chris@0
|
602 {
|
Chris@0
|
603 return array(
|
Chris@0
|
604 array('~.+\\.p.+~i'),
|
Chris@0
|
605 array('~t.*s~i'),
|
Chris@0
|
606 );
|
Chris@0
|
607 }
|
Chris@0
|
608
|
Chris@0
|
609 /**
|
Chris@0
|
610 * @dataProvider getTestPathData
|
Chris@0
|
611 */
|
Chris@0
|
612 public function testPath($matchPatterns, $noMatchPatterns, array $expected)
|
Chris@0
|
613 {
|
Chris@0
|
614 $finder = $this->buildFinder();
|
Chris@0
|
615 $finder->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures')
|
Chris@0
|
616 ->path($matchPatterns)
|
Chris@0
|
617 ->notPath($noMatchPatterns);
|
Chris@0
|
618
|
Chris@0
|
619 $this->assertIterator($this->toAbsoluteFixtures($expected), $finder);
|
Chris@0
|
620 }
|
Chris@0
|
621
|
Chris@0
|
622 public function getTestPathData()
|
Chris@0
|
623 {
|
Chris@0
|
624 return array(
|
Chris@0
|
625 array('', '', array()),
|
Chris@0
|
626 array('/^A\/B\/C/', '/C$/',
|
Chris@0
|
627 array('A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat'),
|
Chris@0
|
628 ),
|
Chris@0
|
629 array('/^A\/B/', 'foobar',
|
Chris@0
|
630 array(
|
Chris@0
|
631 'A'.DIRECTORY_SEPARATOR.'B',
|
Chris@0
|
632 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
|
Chris@0
|
633 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
|
Chris@0
|
634 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
|
Chris@0
|
635 ),
|
Chris@0
|
636 ),
|
Chris@0
|
637 array('A/B/C', 'foobar',
|
Chris@0
|
638 array(
|
Chris@0
|
639 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
|
Chris@0
|
640 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
|
Chris@0
|
641 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
|
Chris@0
|
642 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
|
Chris@0
|
643 ),
|
Chris@0
|
644 ),
|
Chris@0
|
645 array('A/B', 'foobar',
|
Chris@0
|
646 array(
|
Chris@0
|
647 //dirs
|
Chris@0
|
648 'A'.DIRECTORY_SEPARATOR.'B',
|
Chris@0
|
649 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
|
Chris@0
|
650 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B',
|
Chris@0
|
651 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C',
|
Chris@0
|
652 //files
|
Chris@0
|
653 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat',
|
Chris@0
|
654 'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat',
|
Chris@0
|
655 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'ab.dat.copy',
|
Chris@0
|
656 'copy'.DIRECTORY_SEPARATOR.'A'.DIRECTORY_SEPARATOR.'B'.DIRECTORY_SEPARATOR.'C'.DIRECTORY_SEPARATOR.'abc.dat.copy',
|
Chris@0
|
657 ),
|
Chris@0
|
658 ),
|
Chris@0
|
659 array('/^with space\//', 'foobar',
|
Chris@0
|
660 array(
|
Chris@0
|
661 'with space'.DIRECTORY_SEPARATOR.'foo.txt',
|
Chris@0
|
662 ),
|
Chris@0
|
663 ),
|
Chris@0
|
664 );
|
Chris@0
|
665 }
|
Chris@0
|
666
|
Chris@0
|
667 public function testAccessDeniedException()
|
Chris@0
|
668 {
|
Chris@0
|
669 if ('\\' === DIRECTORY_SEPARATOR) {
|
Chris@0
|
670 $this->markTestSkipped('chmod is not supported on Windows');
|
Chris@0
|
671 }
|
Chris@0
|
672
|
Chris@0
|
673 $finder = $this->buildFinder();
|
Chris@0
|
674 $finder->files()->in(self::$tmpDir);
|
Chris@0
|
675
|
Chris@0
|
676 // make 'foo' directory non-readable
|
Chris@0
|
677 $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.'foo';
|
Chris@0
|
678 chmod($testDir, 0333);
|
Chris@0
|
679
|
Chris@0
|
680 if (false === $couldRead = is_readable($testDir)) {
|
Chris@0
|
681 try {
|
Chris@0
|
682 $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
|
Chris@0
|
683 $this->fail('Finder should throw an exception when opening a non-readable directory.');
|
Chris@0
|
684 } catch (\Exception $e) {
|
Chris@0
|
685 $expectedExceptionClass = 'Symfony\\Component\\Finder\\Exception\\AccessDeniedException';
|
Chris@0
|
686 if ($e instanceof \PHPUnit_Framework_ExpectationFailedException) {
|
Chris@0
|
687 $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, 'PHPUnit_Framework_ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
|
Chris@0
|
688 }
|
Chris@0
|
689
|
Chris@0
|
690 if ($e instanceof \PHPUnit\Framework\ExpectationFailedException) {
|
Chris@0
|
691 $this->fail(sprintf("Expected exception:\n%s\nGot:\n%s\nWith comparison failure:\n%s", $expectedExceptionClass, '\PHPUnit\Framework\ExpectationFailedException', $e->getComparisonFailure()->getExpectedAsString()));
|
Chris@0
|
692 }
|
Chris@0
|
693
|
Chris@0
|
694 $this->assertInstanceOf($expectedExceptionClass, $e);
|
Chris@0
|
695 }
|
Chris@0
|
696 }
|
Chris@0
|
697
|
Chris@0
|
698 // restore original permissions
|
Chris@0
|
699 chmod($testDir, 0777);
|
Chris@0
|
700 clearstatcache($testDir);
|
Chris@0
|
701
|
Chris@0
|
702 if ($couldRead) {
|
Chris@0
|
703 $this->markTestSkipped('could read test files while test requires unreadable');
|
Chris@0
|
704 }
|
Chris@0
|
705 }
|
Chris@0
|
706
|
Chris@0
|
707 public function testIgnoredAccessDeniedException()
|
Chris@0
|
708 {
|
Chris@0
|
709 if ('\\' === DIRECTORY_SEPARATOR) {
|
Chris@0
|
710 $this->markTestSkipped('chmod is not supported on Windows');
|
Chris@0
|
711 }
|
Chris@0
|
712
|
Chris@0
|
713 $finder = $this->buildFinder();
|
Chris@0
|
714 $finder->files()->ignoreUnreadableDirs()->in(self::$tmpDir);
|
Chris@0
|
715
|
Chris@0
|
716 // make 'foo' directory non-readable
|
Chris@0
|
717 $testDir = self::$tmpDir.DIRECTORY_SEPARATOR.'foo';
|
Chris@0
|
718 chmod($testDir, 0333);
|
Chris@0
|
719
|
Chris@0
|
720 if (false === ($couldRead = is_readable($testDir))) {
|
Chris@0
|
721 $this->assertIterator($this->toAbsolute(array('foo bar', 'test.php', 'test.py')), $finder->getIterator());
|
Chris@0
|
722 }
|
Chris@0
|
723
|
Chris@0
|
724 // restore original permissions
|
Chris@0
|
725 chmod($testDir, 0777);
|
Chris@0
|
726 clearstatcache($testDir);
|
Chris@0
|
727
|
Chris@0
|
728 if ($couldRead) {
|
Chris@0
|
729 $this->markTestSkipped('could read test files while test requires unreadable');
|
Chris@0
|
730 }
|
Chris@0
|
731 }
|
Chris@0
|
732
|
Chris@0
|
733 protected function buildFinder()
|
Chris@0
|
734 {
|
Chris@0
|
735 return Finder::create();
|
Chris@0
|
736 }
|
Chris@0
|
737 }
|