merged branch IntoWebDevelopment/countable-finder (PR #4068)
Commits
-------
6154ae5
Implement Countable
Discussion
----------
[Finder] Implement the Countable interface
Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes - [![Build Status](https://secure.travis-ci.org/IntoWebDevelopment/symfony.png?branch=countable-finder)](http://travis-ci.org/IntoWebDevelopment/symfony)
Fixes the following tickets: -
Todo: -
This commit is contained in:
commit
ac37e99bd6
@ -26,7 +26,7 @@ namespace Symfony\Component\Finder;
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
class Finder implements \IteratorAggregate
|
||||
class Finder implements \IteratorAggregate, \Countable
|
||||
{
|
||||
const IGNORE_VCS_FILES = 1;
|
||||
const IGNORE_DOT_FILES = 2;
|
||||
@ -558,6 +558,16 @@ class Finder implements \IteratorAggregate
|
||||
throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the results collected by the iterators.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return iterator_count($this->getIterator());
|
||||
}
|
||||
|
||||
private function searchInDirectory($dir)
|
||||
{
|
||||
|
@ -320,6 +320,45 @@ class FinderTest extends Iterator\RealIteratorTestCase
|
||||
|
||||
$this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
|
||||
}
|
||||
|
||||
public function testCountDirectories()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$directory = $finder->directories()->in(self::$tmpDir);
|
||||
$i = 0;
|
||||
|
||||
foreach ($directory as $dir) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
$this->assertCount($i, $directory);
|
||||
}
|
||||
|
||||
public function testCountFiles()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$files = $finder->files()->in(__DIR__.DIRECTORY_SEPARATOR.'Fixtures');
|
||||
$i = 0;
|
||||
|
||||
foreach ($files as $file) {
|
||||
$i++;
|
||||
}
|
||||
|
||||
$this->assertCount($i, $files);
|
||||
}
|
||||
|
||||
public function testCountWithoutIn()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$finder->files();
|
||||
|
||||
try {
|
||||
count($finder);
|
||||
$this->fail('Countable makes use of the getIterator command');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('LogicException', $e, '->getIterator() throws \LogicException when no logic has been entered');
|
||||
}
|
||||
}
|
||||
|
||||
protected function toAbsolute($files)
|
||||
{
|
||||
|
Reference in New Issue
Block a user