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