PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/vendor_full/symfony/tests/Symfony/Tests/Component/Finder/FinderTest.php

https://github.com/l3l0/BehatExamples
PHP | 274 lines | 236 code | 30 blank | 8 comment | 5 complexity | 5685383ea088775036b92eafc891c565 MD5 | raw file
  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\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 testSortByName()
  112. {
  113. $finder = new Finder();
  114. $this->assertSame($finder, $finder->sortByName());
  115. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  116. }
  117. public function testSortByType()
  118. {
  119. $finder = new Finder();
  120. $this->assertSame($finder, $finder->sortByType());
  121. $this->assertIterator($this->toAbsolute(array('foo', 'toto', 'foo/bar.tmp', 'test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  122. }
  123. public function testSort()
  124. {
  125. $finder = new Finder();
  126. $this->assertSame($finder, $finder->sort(function (\SplFileInfo $a, \SplFileInfo $b) { return strcmp($a->getRealpath(), $b->getRealpath()); }));
  127. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  128. }
  129. public function testFilter()
  130. {
  131. $finder = new Finder();
  132. $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; }));
  133. $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
  134. }
  135. public function testFollowLinks()
  136. {
  137. if ('\\' == DIRECTORY_SEPARATOR) {
  138. return;
  139. }
  140. $finder = new Finder();
  141. $this->assertSame($finder, $finder->followLinks());
  142. $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'test.php', 'test.py', 'toto')), $finder->in(self::$tmpDir)->getIterator());
  143. }
  144. public function testIn()
  145. {
  146. $finder = new Finder();
  147. try {
  148. $finder->in('foobar');
  149. $this->fail('->in() throws a \InvalidArgumentException if the directory does not exist');
  150. } catch (\Exception $e) {
  151. $this->assertInstanceOf('InvalidArgumentException', $e, '->in() throws a \InvalidArgumentException if the directory does not exist');
  152. }
  153. $finder = new Finder();
  154. $iterator = $finder->files()->name('*.php')->depth('< 1')->in(array(self::$tmpDir, __DIR__))->getIterator();
  155. $this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php', __DIR__.DIRECTORY_SEPARATOR.'GlobTest.php'), $iterator);
  156. }
  157. public function testGetIterator()
  158. {
  159. $finder = new Finder();
  160. try {
  161. $finder->getIterator();
  162. $this->fail('->getIterator() throws a \LogicException if the in() method has not been called');
  163. } catch (\Exception $e) {
  164. $this->assertInstanceOf('LogicException', $e, '->getIterator() throws a \LogicException if the in() method has not been called');
  165. }
  166. $finder = new Finder();
  167. $dirs = array();
  168. foreach ($finder->directories()->in(self::$tmpDir) as $dir) {
  169. $dirs[] = (string) $dir;
  170. }
  171. $expected = $this->toAbsolute(array('foo', 'toto'));
  172. sort($dirs);
  173. sort($expected);
  174. $this->assertEquals($expected, $dirs, 'implements the \IteratorAggregate interface');
  175. $finder = new Finder();
  176. $this->assertEquals(2, iterator_count($finder->directories()->in(self::$tmpDir)), 'implements the \IteratorAggregate interface');
  177. $finder = new Finder();
  178. $a = iterator_to_array($finder->directories()->in(self::$tmpDir));
  179. $a = array_values(array_map(function ($a) { return (string) $a; }, $a));
  180. sort($a);
  181. $this->assertEquals($expected, $a, 'implements the \IteratorAggregate interface');
  182. }
  183. public function testRelativePath()
  184. {
  185. $finder = new Finder();
  186. $finder->in(self::$tmpDir);
  187. $paths = array();
  188. foreach($finder as $file) {
  189. $paths[] = $file->getRelativePath();
  190. }
  191. $ref = array("", "", "", "", "foo");
  192. sort($ref);
  193. sort($paths);
  194. $this->assertEquals($paths, $ref);
  195. }
  196. public function testRelativePathname()
  197. {
  198. $finder = new Finder();
  199. $finder->in(self::$tmpDir)->sortByName();
  200. $paths = array();
  201. foreach($finder as $file) {
  202. $paths[] = $file->getRelativePathname();
  203. }
  204. $ref = array("test.php", "toto", "test.py", "foo", "foo".DIRECTORY_SEPARATOR."bar.tmp");
  205. sort($paths);
  206. sort($ref);
  207. $this->assertEquals($paths, $ref);
  208. }
  209. protected function toAbsolute($files)
  210. {
  211. $f = array();
  212. foreach ($files as $file) {
  213. $f[] = self::$tmpDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $file);
  214. }
  215. return $f;
  216. }
  217. }