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