RealIteratorTestCase.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Iterator;
  11. abstract class RealIteratorTestCase extends IteratorTestCase
  12. {
  13. protected static $tmpDir;
  14. protected static $files;
  15. public static function setUpBeforeClass()
  16. {
  17. self::$tmpDir = realpath(sys_get_temp_dir()).\DIRECTORY_SEPARATOR.'symfony_finder';
  18. self::$files = [
  19. '.git/',
  20. '.foo/',
  21. '.foo/.bar',
  22. '.foo/bar',
  23. '.bar',
  24. 'test.py',
  25. 'foo/',
  26. 'foo/bar.tmp',
  27. 'test.php',
  28. 'toto/',
  29. 'toto/.git/',
  30. 'foo bar',
  31. 'qux_0_1.php',
  32. 'qux_2_0.php',
  33. 'qux_10_2.php',
  34. 'qux_12_0.php',
  35. 'qux_1000_1.php',
  36. 'qux_1002_0.php',
  37. 'qux/',
  38. 'qux/baz_1_2.py',
  39. 'qux/baz_100_1.py',
  40. ];
  41. self::$files = self::toAbsolute(self::$files);
  42. if (is_dir(self::$tmpDir)) {
  43. self::tearDownAfterClass();
  44. } else {
  45. mkdir(self::$tmpDir);
  46. }
  47. foreach (self::$files as $file) {
  48. if (\DIRECTORY_SEPARATOR === $file[\strlen($file) - 1]) {
  49. mkdir($file);
  50. } else {
  51. touch($file);
  52. }
  53. }
  54. file_put_contents(self::toAbsolute('test.php'), str_repeat(' ', 800));
  55. file_put_contents(self::toAbsolute('test.py'), str_repeat(' ', 2000));
  56. file_put_contents(self::toAbsolute('.gitignore'), '*.php');
  57. touch(self::toAbsolute('foo/bar.tmp'), strtotime('2005-10-15'));
  58. touch(self::toAbsolute('test.php'), strtotime('2005-10-15'));
  59. }
  60. public static function tearDownAfterClass()
  61. {
  62. $paths = new \RecursiveIteratorIterator(
  63. new \RecursiveDirectoryIterator(self::$tmpDir, \RecursiveDirectoryIterator::SKIP_DOTS),
  64. \RecursiveIteratorIterator::CHILD_FIRST
  65. );
  66. foreach ($paths as $path) {
  67. if ($path->isDir()) {
  68. if ($path->isLink()) {
  69. @unlink($path);
  70. } else {
  71. @rmdir($path);
  72. }
  73. } else {
  74. @unlink($path);
  75. }
  76. }
  77. }
  78. protected static function toAbsolute($files = null)
  79. {
  80. /*
  81. * Without the call to setUpBeforeClass() property can be null.
  82. */
  83. if (!self::$tmpDir) {
  84. self::$tmpDir = realpath(sys_get_temp_dir()).\DIRECTORY_SEPARATOR.'symfony_finder';
  85. }
  86. if (\is_array($files)) {
  87. $f = [];
  88. foreach ($files as $file) {
  89. if (\is_array($file)) {
  90. $f[] = self::toAbsolute($file);
  91. } else {
  92. $f[] = self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $file);
  93. }
  94. }
  95. return $f;
  96. }
  97. if (\is_string($files)) {
  98. return self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $files);
  99. }
  100. return self::$tmpDir;
  101. }
  102. protected static function toAbsoluteFixtures($files)
  103. {
  104. $f = [];
  105. foreach ($files as $file) {
  106. $f[] = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.$file);
  107. }
  108. return $f;
  109. }
  110. }