FilesystemTestCase.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 = array();
  17. /**
  18. * @var \Symfony\Component\Filesystem\Filesystem
  19. */
  20. protected $filesystem = null;
  21. /**
  22. * @var string
  23. */
  24. protected $workspace = null;
  25. private static $symlinkOnWindows = null;
  26. public static function setUpBeforeClass()
  27. {
  28. if ('\\' === \DIRECTORY_SEPARATOR && null === self::$symlinkOnWindows) {
  29. $target = tempnam(sys_get_temp_dir(), 'sl');
  30. $link = sys_get_temp_dir().'/sl'.microtime(true).mt_rand();
  31. self::$symlinkOnWindows = @symlink($target, $link) && is_link($link);
  32. @unlink($link);
  33. unlink($target);
  34. }
  35. }
  36. protected function setUp()
  37. {
  38. $this->umask = umask(0);
  39. $this->filesystem = new Filesystem();
  40. $this->workspace = sys_get_temp_dir().'/'.microtime(true).'.'.mt_rand();
  41. mkdir($this->workspace, 0777, true);
  42. $this->workspace = realpath($this->workspace);
  43. }
  44. protected function tearDown()
  45. {
  46. if (!empty($this->longPathNamesWindows)) {
  47. foreach ($this->longPathNamesWindows as $path) {
  48. exec('DEL '.$path);
  49. }
  50. $this->longPathNamesWindows = array();
  51. }
  52. $this->filesystem->remove($this->workspace);
  53. umask($this->umask);
  54. }
  55. /**
  56. * @param int $expectedFilePerms Expected file permissions as three digits (i.e. 755)
  57. * @param string $filePath
  58. */
  59. protected function assertFilePermissions($expectedFilePerms, $filePath)
  60. {
  61. $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
  62. $this->assertEquals(
  63. $expectedFilePerms,
  64. $actualFilePerms,
  65. sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
  66. );
  67. }
  68. protected function getFileOwner($filepath)
  69. {
  70. $this->markAsSkippedIfPosixIsMissing();
  71. $infos = stat($filepath);
  72. if ($datas = posix_getpwuid($infos['uid'])) {
  73. return $datas['name'];
  74. }
  75. }
  76. protected function getFileGroup($filepath)
  77. {
  78. $this->markAsSkippedIfPosixIsMissing();
  79. $infos = stat($filepath);
  80. if ($datas = posix_getgrgid($infos['gid'])) {
  81. return $datas['name'];
  82. }
  83. $this->markTestSkipped('Unable to retrieve file group name');
  84. }
  85. protected function markAsSkippedIfSymlinkIsMissing($relative = false)
  86. {
  87. if ('\\' === \DIRECTORY_SEPARATOR && false === self::$symlinkOnWindows) {
  88. $this->markTestSkipped('symlink requires "Create symbolic links" privilege on Windows');
  89. }
  90. // https://bugs.php.net/bug.php?id=69473
  91. if ($relative && '\\' === \DIRECTORY_SEPARATOR && 1 === PHP_ZTS) {
  92. $this->markTestSkipped('symlink does not support relative paths on thread safe Windows PHP versions');
  93. }
  94. }
  95. protected function markAsSkippedIfChmodIsMissing()
  96. {
  97. if ('\\' === \DIRECTORY_SEPARATOR) {
  98. $this->markTestSkipped('chmod is not supported on Windows');
  99. }
  100. }
  101. protected function markAsSkippedIfPosixIsMissing()
  102. {
  103. if (!\function_exists('posix_isatty')) {
  104. $this->markTestSkipped('Function posix_isatty is required.');
  105. }
  106. }
  107. }