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