FilesystemTestCase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\Filesystem\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. class FilesystemTestCase extends TestCase
  14. {
  15. private $umask;
  16. protected $longPathNamesWindows = [];
  17. /**
  18. * @var \Symfony\Component\Filesystem\Filesystem
  19. */
  20. protected $filesystem = null;
  21. /**
  22. * @var string
  23. */
  24. protected $workspace = null;
  25. /**
  26. * @var bool|null Flag for hard links on Windows
  27. */
  28. private static $linkOnWindows = null;
  29. /**
  30. * @var bool|null Flag for symbolic links on Windows
  31. */
  32. private static $symlinkOnWindows = null;
  33. public static function setUpBeforeClass()
  34. {
  35. if ('\\' === \DIRECTORY_SEPARATOR) {
  36. self::$linkOnWindows = true;
  37. $originFile = tempnam(sys_get_temp_dir(), 'li');
  38. $targetFile = tempnam(sys_get_temp_dir(), 'li');
  39. if (true !== @link($originFile, $targetFile)) {
  40. $report = error_get_last();
  41. if (\is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
  42. self::$linkOnWindows = false;
  43. }
  44. } else {
  45. @unlink($targetFile);
  46. }
  47. self::$symlinkOnWindows = true;
  48. $originDir = tempnam(sys_get_temp_dir(), 'sl');
  49. $targetDir = tempnam(sys_get_temp_dir(), 'sl');
  50. if (true !== @symlink($originDir, $targetDir)) {
  51. $report = error_get_last();
  52. if (\is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
  53. self::$symlinkOnWindows = false;
  54. }
  55. } else {
  56. @unlink($targetDir);
  57. }
  58. }
  59. }
  60. protected function setUp()
  61. {
  62. $this->umask = umask(0);
  63. $this->filesystem = new Filesystem();
  64. $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
  65. mkdir($this->workspace, 0777, true);
  66. $this->workspace = realpath($this->workspace);
  67. }
  68. protected function tearDown()
  69. {
  70. if (!empty($this->longPathNamesWindows)) {
  71. foreach ($this->longPathNamesWindows as $path) {
  72. exec('DEL '.$path);
  73. }
  74. $this->longPathNamesWindows = [];
  75. }
  76. $this->filesystem->remove($this->workspace);
  77. umask($this->umask);
  78. }
  79. /**
  80. * @param int $expectedFilePerms Expected file permissions as three digits (i.e. 755)
  81. * @param string $filePath
  82. */
  83. protected function assertFilePermissions($expectedFilePerms, $filePath)
  84. {
  85. $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
  86. $this->assertEquals(
  87. $expectedFilePerms,
  88. $actualFilePerms,
  89. sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
  90. );
  91. }
  92. protected function getFileOwner($filepath)
  93. {
  94. $this->markAsSkippedIfPosixIsMissing();
  95. $infos = stat($filepath);
  96. if ($datas = posix_getpwuid($infos['uid'])) {
  97. return $datas['name'];
  98. }
  99. }
  100. protected function getFileGroup($filepath)
  101. {
  102. $this->markAsSkippedIfPosixIsMissing();
  103. $infos = stat($filepath);
  104. if ($datas = posix_getgrgid($infos['gid'])) {
  105. return $datas['name'];
  106. }
  107. $this->markTestSkipped('Unable to retrieve file group name');
  108. }
  109. protected function markAsSkippedIfLinkIsMissing()
  110. {
  111. if (!\function_exists('link')) {
  112. $this->markTestSkipped('link is not supported');
  113. }
  114. if ('\\' === \DIRECTORY_SEPARATOR && false === self::$linkOnWindows) {
  115. $this->markTestSkipped('link requires "Create hard links" privilege on windows');
  116. }
  117. }
  118. protected function markAsSkippedIfSymlinkIsMissing($relative = false)
  119. {
  120. if ('\\' === \DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
  121. $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
  122. }
  123. // https://bugs.php.net/bug.php?id=69473
  124. if ($relative && '\\' === \DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
  125. $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
  126. }
  127. }
  128. protected function markAsSkippedIfChmodIsMissing()
  129. {
  130. if ('\\' === \DIRECTORY_SEPARATOR) {
  131. $this->markTestSkipped('chmod is not supported on Windows');
  132. }
  133. }
  134. protected function markAsSkippedIfPosixIsMissing()
  135. {
  136. if (!\function_exists('posix_isatty')) {
  137. $this->markTestSkipped('Function posix_isatty is required.');
  138. }
  139. }
  140. }