FinderTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  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));
  132. }
  133. public function testFollowLinks()
  134. {
  135. if ('\\' == DIRECTORY_SEPARATOR)
  136. {
  137. return;
  138. }
  139. $finder = new Finder();
  140. $this->assertSame($finder, $finder->followLinks());
  141. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir));
  142. }
  143. public function testIn()
  144. {
  145. $finder = new Finder();
  146. try
  147. {
  148. $finder->in('foobar');
  149. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  150. }
  151. catch (\Exception $e)
  152. {
  153. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  154. }
  155. $finder = new Finder();
  156. $iterator = $finder->files()->name('*.php')->maxDepth(0)->in(array(self::$tmpDir, __DIR__));
  157. $this->assertIterator(array(self::$tmpDir.'test.php', __DIR__.'/FinderTest.php', __DIR__.'/GlobTest.php', __DIR__.'/NumberCompareTest.php'), $iterator);
  158. }
  159. protected function toAbsolute($files)
  160. {
  161. $f = array();
  162. foreach ($files as $file)
  163. {
  164. $f[] = self::$tmpDir.$file;
  165. }
  166. return $f;
  167. }
  168. }