FinderTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Component\Finder;
  11. use Symfony\Component\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 testDepth()
  44. {
  45. $finder = new Finder();
  46. $this->assertSame($finder, $finder->depth('< 1'));
  47. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  48. $finder = new Finder();
  49. $this->assertSame($finder, $finder->depth('<= 0'));
  50. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  51. $finder = new Finder();
  52. $this->assertSame($finder, $finder->depth('>= 1'));
  53. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
  54. $finder = new Finder();
  55. $finder->depth('< 1')->depth('>= 1');
  56. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  57. }
  58. public function testName()
  59. {
  60. $finder = new Finder();
  61. $this->assertSame($finder, $finder->name('*.php'));
  62. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  63. $finder = new Finder();
  64. $finder->name('test.ph*');
  65. $finder->name('test.py');
  66. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  67. }
  68. public function testNotName()
  69. {
  70. $finder = new Finder();
  71. $this->assertSame($finder, $finder->notName('*.php'));
  72. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  73. $finder = new Finder();
  74. $finder->notName('*.php');
  75. $finder->notName('*.py');
  76. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  77. $finder = new Finder();
  78. $finder->name('test.ph*');
  79. $finder->name('test.py');
  80. $finder->notName('*.php');
  81. $finder->notName('*.py');
  82. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  83. }
  84. public function testSize()
  85. {
  86. $finder = new Finder();
  87. $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
  88. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  89. }
  90. public function testDate()
  91. {
  92. $finder = new Finder();
  93. $this->assertSame($finder, $finder->files()->date('until last month'));
  94. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
  95. }
  96. public function testExclude()
  97. {
  98. $finder = new Finder();
  99. $this->assertSame($finder, $finder->exclude('foo'));
  100. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  101. }
  102. public function testIgnoreVCS()
  103. {
  104. $finder = new Finder();
  105. $this->assertSame($finder, $finder->ignoreVCS(false));
  106. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  107. $finder = new Finder();
  108. $this->assertSame($finder, $finder->ignoreVCS(true));
  109. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  110. }
  111. public function testIgnoreDotFiles()
  112. {
  113. $finder = new Finder();
  114. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  115. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  116. $finder = new Finder();
  117. $this->assertSame($finder, $finder->ignoreDotFiles(true));
  118. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  119. }
  120. public function testSortByName()
  121. {
  122. $finder = new Finder();
  123. $this->assertSame($finder, $finder->sortByName());
  124. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  125. }
  126. public function testSortByType()
  127. {
  128. $finder = new Finder();
  129. $this->assertSame($finder, $finder->sortByType());
  130. $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  131. }
  132. public function testSort()
  133. {
  134. $finder = new Finder();
  135. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
  136. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  137. }
  138. public function testFilter()
  139. {
  140. $finder = new Finder();
  141. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
  142. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  143. }
  144. public function testFollowLinks()
  145. {
  146. if ('\\' == DIRECTORY_SEPARATOR) {
  147. return;
  148. }
  149. $finder = new Finder();
  150. $this->assertSame($finder, $finder->followLinks());
  151. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  152. }
  153. public function testIn()
  154. {
  155. $finder = new Finder();
  156. try {
  157. $finder->in('foobar');
  158. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  159. } catch (\Exception $e) {
  160. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  161. }
  162. $finder = new Finder();
  163. $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
  164. $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
  165. }
  166. public function testGetIterator()
  167. {
  168. $finder = new Finder();
  169. try {
  170. $finder->getIterator();
  171. $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
  172. } catch (\Exception $e) {
  173. $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
  174. }
  175. $finder = new Finder();
  176. $dirs = array();
  177. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  178. $dirs[] = (string) $dir;
  179. }
  180. $expected = $this->toAbsolute(array('foo', 'toto'));
  181. sort($dirs);
  182. sort($expected);
  183. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  184. $finder = new Finder();
  185. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  186. $finder = new Finder();
  187. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  188. $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
  189. sort($a);
  190. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  191. }
  192. public function testRelativePath()
  193. {
  194. $finder = new Finder();
  195. $finder->in(self::$tmpDir);
  196. $paths = array();
  197. foreach($finder as $file) {
  198. $paths[] = $file->getRelativePath();
  199. }
  200. $ref = array("", "", "", "", "foo");
  201. sort($ref);
  202. sort($paths);
  203. $this->assertEquals($paths, $ref);
  204. }
  205. public function testRelativePathname()
  206. {
  207. $finder = new Finder();
  208. $finder->in(self::$tmpDir)->sortByName();
  209. $paths = array();
  210. foreach($finder as $file) {
  211. $paths[] = $file->getRelativePathname();
  212. }
  213. $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
  214. sort($paths);
  215. sort($ref);
  216. $this->assertEquals($paths, $ref);
  217. }
  218. public function testAppendWithAFinder()
  219. {
  220. $finder = new Finder();
  221. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  222. $finder1 = new Finder();
  223. $finder1->directories()->in(self::$tmpDir);
  224. $finder->append($finder1);
  225. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  226. }
  227. public function testAppendWithAnArray()
  228. {
  229. $finder = new Finder();
  230. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  231. $finder->append($this->toAbsolute(array('foo', 'toto')));
  232. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  233. }
  234. protected function toAbsolute($files)
  235. {
  236. $f = array();
  237. foreach ($files as $file) {
  238. $f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
  239. }
  240. return $f;
  241. }
  242. }