RealIteratorTestCase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = array(
  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. );
  32. self::$files = self::toAbsolute(self::$files);
  33. if (is_dir(self::$tmpDir)) {
  34. self::tearDownAfterClass();
  35. } else {
  36. mkdir(self::$tmpDir);
  37. }
  38. foreach (self::$files as $file) {
  39. if (\DIRECTORY_SEPARATOR === $file[\strlen($file) - 1]) {
  40. mkdir($file);
  41. } else {
  42. touch($file);
  43. }
  44. }
  45. file_put_contents(self::toAbsolute('test.php'), str_repeat(' ', 800));
  46. file_put_contents(self::toAbsolute('test.py'), str_repeat(' ', 2000));
  47. touch(self::toAbsolute('foo/bar.tmp'), strtotime('2005-10-15'));
  48. touch(self::toAbsolute('test.php'), strtotime('2005-10-15'));
  49. }
  50. public static function tearDownAfterClass()
  51. {
  52. $paths = new \RecursiveIteratorIterator(
  53. new \RecursiveDirectoryIterator(self::$tmpDir, \RecursiveDirectoryIterator::SKIP_DOTS),
  54. \RecursiveIteratorIterator::CHILD_FIRST
  55. );
  56. foreach ($paths as $path) {
  57. if ($path->isDir()) {
  58. if ($path->isLink()) {
  59. @unlink($path);
  60. } else {
  61. @rmdir($path);
  62. }
  63. } else {
  64. @unlink($path);
  65. }
  66. }
  67. }
  68. protected static function toAbsolute($files = null)
  69. {
  70. /*
  71. * Without the call to setUpBeforeClass() property can be null.
  72. */
  73. if (!self::$tmpDir) {
  74. self::$tmpDir = realpath(sys_get_temp_dir()).\DIRECTORY_SEPARATOR.'symfony_finder';
  75. }
  76. if (\is_array($files)) {
  77. $f = array();
  78. foreach ($files as $file) {
  79. if (\is_array($file)) {
  80. $f[] = self::toAbsolute($file);
  81. } else {
  82. $f[] = self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $file);
  83. }
  84. }
  85. return $f;
  86. }
  87. if (\is_string($files)) {
  88. return self::$tmpDir.\DIRECTORY_SEPARATOR.str_replace('/', \DIRECTORY_SEPARATOR, $files);
  89. }
  90. return self::$tmpDir;
  91. }
  92. protected static function toAbsoluteFixtures($files)
  93. {
  94. $f = array();
  95. foreach ($files as $file) {
  96. $f[] = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.$file);
  97. }
  98. return $f;
  99. }
  100. }