8000 merged branch gajdaw/finder_contains_exception_test (PR #4056) · symfony/symfony@dd0da03 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd0da03

Browse files
committed
merged branch gajdaw/finder_contains_exception_test (PR #4056)
Commits ------- f2fea97 [Component][Finder] tests and condition: contains() used on dir Discussion ---------- [Component][Finder] tests and condition: contains() used on dir Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - `Finder::contains()` and `Finder::notContains()` can't be used on directories. --------------------------------------------------------------------------- by travisbot at 2012-05-08T06:33:11Z This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1273818) (merged f2fea97 into 919604a).
2 parents 266b686 + f2fea97 commit dd0da03

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

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

Lines 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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,24 @@ public function getContainsTestData()
411411
);
412412
}
413413

414+
public function testContainsOnDirectory()
415+
{
416+
$finder = new Finder();
417+
$finder->in(__DIR__)
418+
->directories()
419+
->name('Fixtures')
420+
->contains('abc');
421+
$this->assertIterator(array(), $finder);
422+
}
423+
424+
public function testNotContainsOnDirectory()
425+
{
426+
$finder = new Finder();
427+
$finder->in(__DIR__)
428+
->directories()
429+
->name('Fixtures')
430+
->notContains('abc');
431+
$this->assertIterator(array(), $finder);
432+
}
433+
414434
}

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

Lines 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