| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 | 
							- <?php
 - 
 - /*
 -  * This file is part of the Symfony package.
 -  *
 -  * (c) Fabien Potencier <fabien@symfony.com>
 -  *
 -  * For the full copyright and license information, please view the LICENSE
 -  * file that was distributed with this source code.
 -  */
 - 
 - namespace Symfony\Tests\Component\Finder;
 - 
 - use Symfony\Component\Finder\Finder;
 - 
 - require_once __DIR__.'/Iterator/RealIteratorTestCase.php';
 - 
 - class FinderTest extends Iterator\RealIteratorTestCase
 - {
 -     static protected $tmpDir;
 - 
 -     static public function setUpBeforeClass()
 -     {
 -         parent::setUpBeforeClass();
 - 
 -         self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
 -     }
 - 
 -     public function testCreate()
 -     {
 -         $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
 -     }
 - 
 -     public function testDirectories()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->directories());
 -         $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->directories();
 -         $finder->files();
 -         $finder->directories();
 -         $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testFiles()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->files());
 -         $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->files();
 -         $finder->directories();
 -         $finder->files();
 -         $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testDepth()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->depth('< 1'));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->depth('<= 0'));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->depth('>= 1'));
 -         $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->depth('< 1')->depth('>= 1');
 -         $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testName()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->name('*.php'));
 -         $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->name('test.ph*');
 -         $finder->name('test.py');
 -         $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testNotName()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->notName('*.php'));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->notName('*.php');
 -         $finder->notName('*.py');
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $finder->name('test.ph*');
 -         $finder->name('test.py');
 -         $finder->notName('*.php');
 -         $finder->notName('*.py');
 -         $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testSize()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
 -         $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testDate()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->files()->date('until last month'));
 -         $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testExclude()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->exclude('foo'));
 -         $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testIgnoreVCS()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->ignoreVCS(false));
 -         $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->ignoreVCS(true));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testIgnoreDotFiles()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
 -         $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 - 
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->ignoreDotFiles(true));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testSortByName()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->sortByName());
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testSortByType()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->sortByType());
 -         $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testSort()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testFilter()
 -     {
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
 -         $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testFollowLinks()
 -     {
 -         if ('\\' == DIRECTORY_SEPARATOR) {
 -             return;
 -         }
 - 
 -         $finder = new Finder();
 -         $this->assertSame($finder, $finder->followLinks());
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
 -     }
 - 
 -     public function testIn()
 -     {
 -         $finder = new Finder();
 -         try {
 -             $finder->in('foobar');
 -             $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
 -         } catch (\Exception $e) {
 -             $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
 -         }
 - 
 -         $finder = new Finder();
 -         $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
 - 
 -         $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
 -     }
 - 
 -     public function testGetIterator()
 -     {
 -         $finder = new Finder();
 -         try {
 -             $finder->getIterator();
 -             $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
 -         } catch (\Exception $e) {
 -             $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
 -         }
 - 
 -         $finder = new Finder();
 -         $dirs = array();
 -         foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
 -             $dirs[] = (string) $dir;
 -         }
 - 
 -         $expected = $this->toAbsolute(array('foo', 'toto'));
 - 
 -         sort($dirs);
 -         sort($expected);
 - 
 -         $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
 - 
 -         $finder = new Finder();
 -         $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
 - 
 -         $finder = new Finder();
 -         $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
 -         $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
 -         sort($a);
 -         $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
 -     }
 - 
 -     public function testRelativePath()
 -     {
 -         $finder = new Finder();
 - 
 -         $finder->in(self::$tmpDir);
 - 
 -         $paths = array();
 - 
 -         foreach($finder as $file) {
 -             $paths[] = $file->getRelativePath();
 -         }
 - 
 -         $ref = array("", "", "", "", "foo");
 - 
 -         sort($ref);
 -         sort($paths);
 - 
 -         $this->assertEquals($paths, $ref);
 -     }
 - 
 -     public function testRelativePathname()
 -     {
 -         $finder = new Finder();
 - 
 -         $finder->in(self::$tmpDir)->sortByName();
 - 
 -         $paths = array();
 - 
 -         foreach($finder as $file) {
 -             $paths[] = $file->getRelativePathname();
 -         }
 - 
 -         $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
 - 
 -         sort($paths);
 -         sort($ref);
 - 
 -         $this->assertEquals($paths, $ref);
 -     }
 - 
 -     public function testAppendWithAFinder()
 -     {
 -         $finder = new Finder();
 -         $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
 - 
 -         $finder1 = new Finder();
 -         $finder1->directories()->in(self::$tmpDir);
 - 
 -         $finder->append($finder1);
 - 
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
 -     }
 - 
 -     public function testAppendWithAnArray()
 -     {
 -         $finder = new Finder();
 -         $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
 - 
 -         $finder->append($this->toAbsolute(array('foo', 'toto')));
 - 
 -         $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
 -     }
 - 
 -     protected function toAbsolute($files)
 -     {
 -         $f = array();
 -         foreach ($files as $file) {
 -             $f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
 -         }
 - 
 -         return $f;
 -     }
 - }
 
 
  |