Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2fea97

Browse filesBrowse files
committed
[Component][Finder] tests and condition: contains() used on dir
1 parent 6c71409 commit f2fea97
Copy full SHA for f2fea97

File tree

Expand file treeCollapse file tree

3 files changed

+73
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+73
-3
lines changed

‎src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function accept()
3030
return true;
3131
}
3232

33-
$content = file_get_contents($this->getRealpath());
33+
if ($this->isDir() || !$this->isReadable()) {
34+
return false;
35+
}
36+
37+
$content = @file_get_contents($filename = $this->getRealpath());
3438
if (false === $content) {
3539
throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath()));
3640
}

‎src/Symfony/Component/Finder/Tests/FinderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/FinderTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,4 +372,24 @@ public function getContainsTestData()
372372
);
373373
}
374374

375+
public function testContainsOnDirectory()
376+
{
377+
$finder = new Finder();
378+
$finder->in(__DIR__)
379+
->directories()
380+
->name('Fixtures')
381+
->contains('abc');
382+
$this->assertIterator(array(), $finder);
383+
}
384+
385+
public function testNotContainsOnDirectory()
386+
{
387+
$finder = new Finder();
388+
$finder->in(__DIR__)
389+
->directories()
390+
->name('Fixtures')
391+
->notContains('abc');
392+
$this->assertIterator(array(), $finder);
393+
}
394+
375395
}

‎src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php
+48-2Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,34 @@ class FilecontentFilterIteratorTest extends IteratorTestCase
1919
public function testAccept()
2020
{
2121
$inner = new ContentInnerNameIterator(array('test.txt'));
22-
2322
$iterator = new FilecontentFilterIterator($inner, array(), array());
24-
2523
$this->assertIterator(array('test.txt'), $iterator);
2624
}
2725

26+
public function testDirectory()
27+
{
28+
$inner = new ContentInnerNameIterator(array('directory'));
29+
$iterator = new FilecontentFilterIterator($inner, array('directory'), array());
30+
$this->assertIterator(array(), $iterator);
31+
}
32+
33+
public function testUnreadableFile()
34+
{
35+
$inner = new ContentInnerNameIterator(array('file r-'));
36+
$iterator = new FilecontentFilterIterator($inner, array('file r-'), array());
37+
$this->assertIterator(array(), $iterator);
38+
}
39+
40+
/**
41+
* @expectedException RuntimeException
42+
*/
43+
public function testFileGetContents()
44+
{
45+
$inner = new ContentInnerNameIterator(array('file r+'));
46+
$iterator = new FilecontentFilterIterator($inner, array('file r+'), array());
47+
$array = iterator_to_array($iterator);
48+
}
49+
2850
}
2951

3052
class ContentInnerNameIterator extends \ArrayIterator
@@ -38,4 +60,28 @@ public function getFilename()
3860
{
3961
return parent::current();
4062
}
63+
64+
public function isFile()
65+
{
66+
$name = parent::current();
67+
return preg_match('/file/', $name);
68+
}
69+
70+
public function isDir()
71+
{
72+
$name = parent::current();
73+
return preg_match('/directory/', $name);
74+
}
75+
76+
public function getRealpath()
77+
{
78+
return parent::current();
79+
}
80+
81+
public function isReadable()
82+
{
83+
$name = parent::current();
84+
return preg_match('/r\+/', $name);
85+
}
86+
4187
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.