PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Filesystem/Tests/FilesystemTestCase.php

http://github.com/symfony/symfony
PHP | 180 lines | 148 code | 12 blank | 20 comment | 6 complexity | 216f6e15241164d6893f21309c4aa8ff MD5 | raw file
  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 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(): void
  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(): void
  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(): void
  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 getFileOwnerId($filepath)
  93. {
  94. $this->markAsSkippedIfPosixIsMissing();
  95. $infos = stat($filepath);
  96. return $infos['uid'];
  97. }
  98. protected function getFileOwner($filepath)
  99. {
  100. $this->markAsSkippedIfPosixIsMissing();
  101. return ($datas = posix_getpwuid($this->getFileOwnerId($filepath))) ? $datas['name'] : null;
  102. }
  103. protected function getFileGroupId($filepath)
  104. {
  105. $this->markAsSkippedIfPosixIsMissing();
  106. $infos = stat($filepath);
  107. return $infos['gid'];
  108. }
  109. protected function getFileGroup($filepath)
  110. {
  111. $this->markAsSkippedIfPosixIsMissing();
  112. if ($datas = posix_getgrgid($this->getFileGroupId($filepath))) {
  113. return $datas['name'];
  114. }
  115. $this->markTestSkipped('Unable to retrieve file group name');
  116. }
  117. protected function markAsSkippedIfLinkIsMissing()
  118. {
  119. if (!\function_exists('link')) {
  120. $this->markTestSkipped('link is not supported');
  121. }
  122. if ('\\' === \DIRECTORY_SEPARATOR && false === self::$linkOnWindows) {
  123. $this->markTestSkipped('link requires "Create hard links" privilege on windows');
  124. }
  125. }
  126. protected function markAsSkippedIfSymlinkIsMissing($relative = false)
  127. {
  128. if ('\\' === \DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
  129. $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
  130. }
  131. // https://bugs.php.net/69473
  132. if ($relative && '\\' === \DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
  133. $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
  134. }
  135. }
  136. protected function markAsSkippedIfChmodIsMissing()
  137. {
  138. if ('\\' === \DIRECTORY_SEPARATOR) {
  139. $this->markTestSkipped('chmod is not supported on Windows');
  140. }
  141. }
  142. protected function markAsSkippedIfPosixIsMissing()
  143. {
  144. if (!\function_exists('posix_isatty')) {
  145. $this->markTestSkipped('Function posix_isatty is required.');
  146. }
  147. }
  148. }