RealIteratorTestCase.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. touch(self::toAbsolute('foo/bar.tmp'), strtotime('2005-10-15'));
  57. touch(self::toAbsolute('test.php'), strtotime('2005-10-15'));
  58. }
  59. public static function tearDownAfterClass()
  60. {
  61. $paths = new \RecursiveIteratorIterator(
  62. new \RecursiveDirectoryIterator(self::$tmpDir, \RecursiveDirectoryIterator::SKIP_DOTS),
  63. \RecursiveIteratorIterator::CHILD_FIRST
  64. );
  65. foreach ($paths as $path) {
  66. if ($path->isDir()) {
  67. if ($path->isLink()) {
  68. @unlink($path);
  69. } else {
  70. @rmdir($path);
  71. }
  72. } else {
  73. @unlink($path);
  74. }
  75. }
  76. }
  77. protected static function toAbsolute($files = null)
  78. {
  79. /*
  80. * Without the call to setUpBeforeClass() property can be null.
  81. */
  82. if (!self::$tmpDir) {
  83. self::$tmpDir = realpath(sys_get_temp_dir()).\DIRECTORY_SEPARATOR.'symfony_finder';
  84. }
  85. if (\is_array($files)) {
  86. $f = [];
  87. foreach ($files as $file) {
  88. if (\is_array($file)) {
  89. $f[] = self::toAbsolute($file);
  90. } else {
  91. $f[] = self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $file);
  92. }
  93. }
  94. return $f;
  95. }
  96. if (\is_string($files)) {
  97. return self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $files);
  98. }
  99. return self::$tmpDir;
  100. }
  101. protected static function toAbsoluteFixtures($files)
  102. {
  103. $f = [];
  104. foreach ($files as $file) {
  105. $f[] = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.$file);
  106. }
  107. return $f;
  108. }
  109. }