FinderTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Components\Finder;
  11. use Symfony\Components\Finder\Finder;
  12. require_once __DIR__.'/Iterator/RealIteratorTestCase.php';
  13. class FinderTest extends Iterator\RealIteratorTestCase
  14. {
  15. static protected $tmpDir;
  16. static public function setUpBeforeClass()
  17. {
  18. parent::setUpBeforeClass();
  19. self::$tmpDir = sys_get_temp_dir().'/symfony2_finder/';
  20. }
  21. public function testDirectories()
  22. {
  23. $finder = new Finder();
  24. $this->assertSame($finder, $finder->directories());
  25. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  26. $finder = new Finder();
  27. $finder->directories();
  28. $finder->files();
  29. $finder->directories();
  30. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  31. }
  32. public function testFiles()
  33. {
  34. $finder = new Finder();
  35. $this->assertSame($finder, $finder->files());
  36. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  37. $finder = new Finder();
  38. $finder->files();
  39. $finder->directories();
  40. $finder->files();
  41. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  42. }
  43. public function testMaxDepth()
  44. {
  45. $finder = new Finder();
  46. $this->assertSame($finder, $finder->maxDepth(0));
  47. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  48. }
  49. public function testMinDepth()
  50. {
  51. $finder = new Finder();
  52. $this->assertSame($finder, $finder->minDepth(1));
  53. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
  54. }
  55. public function testMinMaxDepth()
  56. {
  57. $finder = new Finder();
  58. $finder->maxDepth(0);
  59. $finder->minDepth(1);
  60. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  61. }
  62. public function testName()
  63. {
  64. $finder = new Finder();
  65. $this->assertSame($finder, $finder->name('*.php'));
  66. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  67. $finder = new Finder();
  68. $finder->name('test.ph*');
  69. $finder->name('test.py');
  70. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  71. }
  72. public function testNotName()
  73. {
  74. $finder = new Finder();
  75. $this->assertSame($finder, $finder->notName('*.php'));
  76. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  77. $finder = new Finder();
  78. $finder->notName('*.php');
  79. $finder->notName('*.py');
  80. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  81. $finder = new Finder();
  82. $finder->name('test.ph*');
  83. $finder->name('test.py');
  84. $finder->notName('*.php');
  85. $finder->notName('*.py');
  86. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  87. }
  88. public function testSize()
  89. {
  90. $finder = new Finder();
  91. $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
  92. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  93. }
  94. public function testExclude()
  95. {
  96. $finder = new Finder();
  97. $this->assertSame($finder, $finder->exclude('foo'));
  98. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  99. }
  100. public function testIgnoreVCS()
  101. {
  102. $finder = new Finder();
  103. $this->assertSame($finder, $finder->ignoreVCS(false));
  104. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  105. $finder = new Finder();
  106. $this->assertSame($finder, $finder->ignoreVCS(true));
  107. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  108. }
  109. public function testSortByName()
  110. {
  111. $finder = new Finder();
  112. $this->assertSame($finder, $finder->sortByName());
  113. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  114. }
  115. public function testSortByType()
  116. {
  117. $finder = new Finder();
  118. $this->assertSame($finder, $finder->sortByType());
  119. $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  120. }
  121. public function testSort()
  122. {
  123. $finder = new Finder();
  124. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
  125. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  126. }
  127. public function testFilter()
  128. {
  129. $finder = new Finder();
  130. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
  131. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  132. }
  133. public function testFollowLinks()
  134. {
  135. if ('\\' == DIRECTORY_SEPARATOR) {
  136. return;
  137. }
  138. $finder = new Finder();
  139. $this->assertSame($finder, $finder->followLinks());
  140. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  141. }
  142. public function testIn()
  143. {
  144. $finder = new Finder();
  145. try {
  146. $finder->in('foobar');
  147. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  148. } catch (\Exception $e) {
  149. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  150. }
  151. $finder = new Finder();
  152. $iterator = $finder->files()->name('*.php')->maxDepth(0)->in(array(self::$tmpDir, __DIR__))->getIterator();
  153. $this->assertIterator(array(self::$tmpDir.'test.php', __DIR__.'/FinderTest.php', __DIR__.'/GlobTest.php', __DIR__.'/NumberCompareTest.php'), $iterator);
  154. }
  155. public function testGetIterator()
  156. {
  157. $finder = new Finder();
  158. try {
  159. $finder->getIterator();
  160. $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
  161. } catch (\Exception $e) {
  162. $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
  163. }
  164. $finder = new Finder();
  165. $dirs = array();
  166. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  167. $dirs[] = (string) $dir;
  168. }
  169. $this->assertEquals($this->toAbsolute(array('foo', 'toto')), $dirs, 'implements the \IteratorAggregate interface');
  170. $finder = new Finder();
  171. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  172. $finder = new Finder();
  173. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  174. $this->assertEquals($this->toAbsolute(array('foo', 'toto')), array_values(array_map(function ($a) { return (string) $a; }, $a)), 'implements the \IteratorAggregate interface');
  175. }
  176. protected function toAbsolute($files)
  177. {
  178. $f = array();
  179. foreach ($files as $file) {
  180. $f[] = self::$tmpDir.$file;
  181. }
  182. return $f;
  183. }
  184. }