FinderTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 testCreate()
  22. {
  23. $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
  24. }
  25. public function testDirectories()
  26. {
  27. $finder = new Finder();
  28. $this->assertSame($finder, $finder->directories());
  29. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  30. $finder = new Finder();
  31. $finder->directories();
  32. $finder->files();
  33. $finder->directories();
  34. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  35. }
  36. public function testFiles()
  37. {
  38. $finder = new Finder();
  39. $this->assertSame($finder, $finder->files());
  40. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  41. $finder = new Finder();
  42. $finder->files();
  43. $finder->directories();
  44. $finder->files();
  45. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  46. }
  47. public function testDepth()
  48. {
  49. $finder = new Finder();
  50. $this->assertSame($finder, $finder->depth('< 1'));
  51. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  52. $finder = new Finder();
  53. $this->assertSame($finder, $finder->depth('<= 0'));
  54. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  55. $finder = new Finder();
  56. $this->assertSame($finder, $finder->depth('>= 1'));
  57. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
  58. $finder = new Finder();
  59. $finder->depth('< 1')->depth('>= 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 testDate()
  95. {
  96. $finder = new Finder();
  97. $this->assertSame($finder, $finder->files()->date('until last month'));
  98. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
  99. }
  100. public function testExclude()
  101. {
  102. $finder = new Finder();
  103. $this->assertSame($finder, $finder->exclude('foo'));
  104. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  105. }
  106. public function testIgnoreVCS()
  107. {
  108. $finder = new Finder();
  109. $this->assertSame($finder, $finder->ignoreVCS(false));
  110. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  111. $finder = new Finder();
  112. $this->assertSame($finder, $finder->ignoreVCS(true));
  113. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  114. }
  115. public function testIgnoreDotFiles()
  116. {
  117. $finder = new Finder();
  118. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  119. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  120. $finder = new Finder();
  121. $this->assertSame($finder, $finder->ignoreDotFiles(true));
  122. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  123. }
  124. public function testSortByName()
  125. {
  126. $finder = new Finder();
  127. $this->assertSame($finder, $finder->sortByName());
  128. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  129. }
  130. public function testSortByType()
  131. {
  132. $finder = new Finder();
  133. $this->assertSame($finder, $finder->sortByType());
  134. $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  135. }
  136. public function testSort()
  137. {
  138. $finder = new Finder();
  139. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
  140. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  141. }
  142. public function testFilter()
  143. {
  144. $finder = new Finder();
  145. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
  146. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  147. }
  148. public function testFollowLinks()
  149. {
  150. if ('\\' == DIRECTORY_SEPARATOR) {
  151. return;
  152. }
  153. $finder = new Finder();
  154. $this->assertSame($finder, $finder->followLinks());
  155. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  156. }
  157. public function testIn()
  158. {
  159. $finder = new Finder();
  160. try {
  161. $finder->in('foobar');
  162. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  163. } catch (\Exception $e) {
  164. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  165. }
  166. $finder = new Finder();
  167. $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
  168. $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
  169. }
  170. public function testGetIterator()
  171. {
  172. $finder = new Finder();
  173. try {
  174. $finder->getIterator();
  175. $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
  176. } catch (\Exception $e) {
  177. $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
  178. }
  179. $finder = new Finder();
  180. $dirs = array();
  181. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  182. $dirs[] = (string) $dir;
  183. }
  184. $expected = $this->toAbsolute(array('foo', 'toto'));
  185. sort($dirs);
  186. sort($expected);
  187. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  188. $finder = new Finder();
  189. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  190. $finder = new Finder();
  191. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  192. $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
  193. sort($a);
  194. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  195. }
  196. public function testRelativePath()
  197. {
  198. $finder = new Finder();
  199. $finder->in(self::$tmpDir);
  200. $paths = array();
  201. foreach($finder as $file) {
  202. $paths[] = $file->getRelativePath();
  203. }
  204. $ref = array("", "", "", "", "foo");
  205. sort($ref);
  206. sort($paths);
  207. $this->assertEquals($paths, $ref);
  208. }
  209. public function testRelativePathname()
  210. {
  211. $finder = new Finder();
  212. $finder->in(self::$tmpDir)->sortByName();
  213. $paths = array();
  214. foreach($finder as $file) {
  215. $paths[] = $file->getRelativePathname();
  216. }
  217. $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
  218. sort($paths);
  219. sort($ref);
  220. $this->assertEquals($paths, $ref);
  221. }
  222. public function testAppendWithAFinder()
  223. {
  224. $finder = new Finder();
  225. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  226. $finder1 = new Finder();
  227. $finder1->directories()->in(self::$tmpDir);
  228. $finder->append($finder1);
  229. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  230. }
  231. public function testAppendWithAnArray()
  232. {
  233. $finder = new Finder();
  234. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  235. $finder->append($this->toAbsolute(array('foo', 'toto')));
  236. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  237. }
  238. protected function toAbsolute($files)
  239. {
  240. $f = array();
  241. foreach ($files as $file) {
  242. $f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
  243. }
  244. return $f;
  245. }
  246. }