From f2fea974600c9860d17614b001058253cba3bbe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?W=C5=82odzimierz=20Gajda?= Date: Sat, 21 Apr 2012 16:08:44 +0200 Subject: [PATCH] [Component][Finder] tests and condition: contains() used on dir --- .../Iterator/FilecontentFilterIterator.php | 6 ++- .../Component/Finder/Tests/FinderTest.php | 20 ++++++++ .../FilecontentFilterIteratorTest.php | 50 ++++++++++++++++++- 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php b/src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php index 9deb722bf9..ffe3ddb7a8 100644 --- a/src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php +++ b/src/Symfony/Component/Finder/Iterator/FilecontentFilterIterator.php @@ -30,7 +30,11 @@ class FilecontentFilterIterator extends MultiplePcreFilterIterator return true; } - $content = file_get_contents($this->getRealpath()); + if ($this->isDir() || !$this->isReadable()) { + return false; + } + + $content = @file_get_contents($filename = $this->getRealpath()); if (false === $content) { throw new \RuntimeException(sprintf('Error reading file "%s".', $this->getRealpath())); } diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index ca58f1894b..b7e6066955 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -372,4 +372,24 @@ class FinderTest extends Iterator\RealIteratorTestCase ); } + public function testContainsOnDirectory() + { + $finder = new Finder(); + $finder->in(__DIR__) + ->directories() + ->name('Fixtures') + ->contains('abc'); + $this->assertIterator(array(), $finder); + } + + public function testNotContainsOnDirectory() + { + $finder = new Finder(); + $finder->in(__DIR__) + ->directories() + ->name('Fixtures') + ->notContains('abc'); + $this->assertIterator(array(), $finder); + } + } diff --git a/src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php b/src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php index dd647c9988..1c0a358802 100644 --- a/src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php +++ b/src/Symfony/Component/Finder/Tests/Iterator/FilecontentFilterIteratorTest.php @@ -19,12 +19,34 @@ class FilecontentFilterIteratorTest extends IteratorTestCase public function testAccept() { $inner = new ContentInnerNameIterator(array('test.txt')); - $iterator = new FilecontentFilterIterator($inner, array(), array()); - $this->assertIterator(array('test.txt'), $iterator); } + public function testDirectory() + { + $inner = new ContentInnerNameIterator(array('directory')); + $iterator = new FilecontentFilterIterator($inner, array('directory'), array()); + $this->assertIterator(array(), $iterator); + } + + public function testUnreadableFile() + { + $inner = new ContentInnerNameIterator(array('file r-')); + $iterator = new FilecontentFilterIterator($inner, array('file r-'), array()); + $this->assertIterator(array(), $iterator); + } + + /** + * @expectedException RuntimeException + */ + public function testFileGetContents() + { + $inner = new ContentInnerNameIterator(array('file r+')); + $iterator = new FilecontentFilterIterator($inner, array('file r+'), array()); + $array = iterator_to_array($iterator); + } + } class ContentInnerNameIterator extends \ArrayIterator @@ -38,4 +60,28 @@ class ContentInnerNameIterator extends \ArrayIterator { return parent::current(); } + + public function isFile() + { + $name = parent::current(); + return preg_match('/file/', $name); + } + + public function isDir() + { + $name = parent::current(); + return preg_match('/directory/', $name); + } + + public function getRealpath() + { + return parent::current(); + } + + public function isReadable() + { + $name = parent::current(); + return preg_match('/r\+/', $name); + } + }