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) (mergedf2fea974
into919604ab
).
This commit is contained in:
commit
dd0da03c8c
@ -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()));
|
||||
}
|
||||
|
@ -411,4 +411,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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user