PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Finder/Tests/FinderTest.php

https://github.com/Exercise/symfony
PHP | 333 lines | 289 code | 36 blank | 8 comment | 5 complexity | 8943bd42f380bbd081a4c961f390fafe MD5 | raw file
  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\Component\Finder\Tests;
  11. use Symfony\Component\Finder\Finder;
  12. class FinderTest extends Iterator\RealIteratorTestCase
  13. {
  14. static protected $tmpDir;
  15. static public function setUpBeforeClass()
  16. {
  17. parent::setUpBeforeClass();
  18. self::$tmpDir = sys_get_temp_dir().'/symfony2_finder';
  19. }
  20. public function testCreate()
  21. {
  22. $this->assertInstanceOf('Symfony\Component\Finder\Finder', Finder::create());
  23. }
  24. public function testDirectories()
  25. {
  26. $finder = new Finder();
  27. $this->assertSame($finder, $finder->directories());
  28. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  29. $finder = new Finder();
  30. $finder->directories();
  31. $finder->files();
  32. $finder->directories();
  33. $this->assertIterator($this->toAbsolute(array('foo', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  34. }
  35. public function testFiles()
  36. {
  37. $finder = new Finder();
  38. $this->assertSame($finder, $finder->files());
  39. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  40. $finder = new Finder();
  41. $finder->files();
  42. $finder->directories();
  43. $finder->files();
  44. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  45. }
  46. public function testDepth()
  47. {
  48. $finder = new Finder();
  49. $this->assertSame($finder, $finder->depth('< 1'));
  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('<= 0'));
  53. $this->assertIterator($this->toAbsolute(array('foo', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  54. $finder = new Finder();
  55. $this->assertSame($finder, $finder->depth('>= 1'));
  56. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp')), $finder->in(self::$tmpDir)->getIterator());
  57. $finder = new Finder();
  58. $finder->depth('< 1')->depth('>= 1');
  59. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  60. }
  61. public function testName()
  62. {
  63. $finder = new Finder();
  64. $this->assertSame($finder, $finder->name('*.php'));
  65. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  66. $finder = new Finder();
  67. $finder->name('test.ph*');
  68. $finder->name('test.py');
  69. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  70. }
  71. public function testNotName()
  72. {
  73. $finder = new Finder();
  74. $this->assertSame($finder, $finder->notName('*.php'));
  75. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  76. $finder = new Finder();
  77. $finder->notName('*.php');
  78. $finder->notName('*.py');
  79. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  80. $finder = new Finder();
  81. $finder->name('test.ph*');
  82. $finder->name('test.py');
  83. $finder->notName('*.php');
  84. $finder->notName('*.py');
  85. $this->assertIterator(array(), $finder->in(self::$tmpDir)->getIterator());
  86. }
  87. public function testSize()
  88. {
  89. $finder = new Finder();
  90. $this->assertSame($finder, $finder->files()->size('< 1K')->size('> 500'));
  91. $this->assertIterator($this->toAbsolute(array('test.php')), $finder->in(self::$tmpDir)->getIterator());
  92. }
  93. public function testDate()
  94. {
  95. $finder = new Finder();
  96. $this->assertSame($finder, $finder->files()->date('until last month'));
  97. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php')), $finder->in(self::$tmpDir)->getIterator());
  98. }
  99. public function testExclude()
  100. {
  101. $finder = new Finder();
  102. $this->assertSame($finder, $finder->exclude('foo'));
  103. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  104. }
  105. public function testIgnoreVCS()
  106. {
  107. $finder = new Finder();
  108. $this->assertSame($finder, $finder->ignoreVCS(false));
  109. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  110. $finder = new Finder();
  111. $this->assertSame($finder, $finder->ignoreVCS(true));
  112. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  113. }
  114. public function testIgnoreDotFiles()
  115. {
  116. $finder = new Finder();
  117. $this->assertSame($finder, $finder->ignoreDotFiles(false)->ignoreVCS(false));
  118. $this->assertIterator($this->toAbsolute(array('.git', 'foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  119. $finder = new Finder();
  120. $this->assertSame($finder, $finder->ignoreDotFiles(true));
  121. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  122. }
  123. public function testSortByName()
  124. {
  125. $finder = new Finder();
  126. $this->assertSame($finder, $finder->sortByName());
  127. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  128. }
  129. public function testSortByType()
  130. {
  131. $finder = new Finder();
  132. $this->assertSame($finder, $finder->sortByType());
  133. $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  134. }
  135. public function testSortByAccessedTime()
  136. {
  137. $finder = new Finder();
  138. $this->assertSame($finder, $finder->sortByAccessedTime());
  139. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
  140. }
  141. public function testSortByChangedTime()
  142. {
  143. $finder = new Finder();
  144. $this->assertSame($finder, $finder->sortByChangedTime());
  145. $this->assertIterator($this->toAbsolute(array('toto', 'test.py', 'test.php', 'foo/bar.tmp', 'foo')), $finder->in(self::$tmpDir)->getIterator());
  146. }
  147. public function testSortByModifiedTime()
  148. {
  149. $finder = new Finder();
  150. $this->assertSame($finder, $finder->sortByModifiedTime());
  151. $this->assertIterator($this->toAbsolute(array('foo/bar.tmp', 'test.php', 'toto', 'test.py', 'foo')), $finder->in(self::$tmpDir)->getIterator());
  152. }
  153. public function testSort()
  154. {
  155. $finder = new Finder();
  156. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
  157. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  158. }
  159. public function testFilter()
  160. {
  161. $finder = new Finder();
  162. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
  163. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  164. }
  165. public function testFollowLinks()
  166. {
  167. if ('\\' == DIRECTORY_SEPARATOR) {
  168. return;
  169. }
  170. $finder = new Finder();
  171. $this->assertSame($finder, $finder->followLinks());
  172. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  173. }
  174. public function testIn()
  175. {
  176. $finder = new Finder();
  177. try {
  178. $finder->in('foobar');
  179. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  180. } catch (\Exception $e) {
  181. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  182. }
  183. $finder = new Finder();
  184. $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
  185. $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'bootstrap.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
  186. }
  187. public function testGetIterator()
  188. {
  189. $finder = new Finder();
  190. try {
  191. $finder->getIterator();
  192. $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
  193. } catch (\Exception $e) {
  194. $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
  195. }
  196. $finder = new Finder();
  197. $dirs = array();
  198. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  199. $dirs[] = (string) $dir;
  200. }
  201. $expected = $this->toAbsolute(array('foo', 'toto'));
  202. sort($dirs);
  203. sort($expected);
  204. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  205. $finder = new Finder();
  206. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  207. $finder = new Finder();
  208. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  209. $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
  210. sort($a);
  211. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  212. }
  213. public function testRelativePath()
  214. {
  215. $finder = new Finder();
  216. $finder->in(self::$tmpDir);
  217. $paths = array();
  218. foreach($finder as $file) {
  219. $paths[] = $file->getRelativePath();
  220. }
  221. $ref = array("", "", "", "", "foo");
  222. sort($ref);
  223. sort($paths);
  224. $this->assertEquals($paths, $ref);
  225. }
  226. public function testRelativePathname()
  227. {
  228. $finder = new Finder();
  229. $finder->in(self::$tmpDir)->sortByName();
  230. $paths = array();
  231. foreach($finder as $file) {
  232. $paths[] = $file->getRelativePathname();
  233. }
  234. $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
  235. sort($paths);
  236. sort($ref);
  237. $this->assertEquals($paths, $ref);
  238. }
  239. public function testAppendWithAFinder()
  240. {
  241. $finder = new Finder();
  242. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  243. $finder1 = new Finder();
  244. $finder1->directories()->in(self::$tmpDir);
  245. $finder->append($finder1);
  246. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  247. }
  248. public function testAppendWithAnArray()
  249. {
  250. $finder = new Finder();
  251. $finder->files()->in(self::$tmpDir.DIRECTORY_SEPARATOR.'foo');
  252. $finder->append($this->toAbsolute(array('foo', 'toto')));
  253. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator());
  254. }
  255. protected function toAbsolute($files)
  256. {
  257. $f = array();
  258. foreach ($files as $file) {
  259. $f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
  260. }
  261. return $f;
  262. }
  263. }