No Description

FilesystemTestCase.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. class FilesystemTestCase extends \PHPUnit_Framework_TestCase
  12. {
  13. private $umask;
  14. /**
  15. * @var string $workspace
  16. */
  17. protected $workspace = null;
  18. protected static $symlinkOnWindows = null;
  19. public static function setUpBeforeClass()
  20. {
  21. if ('\\' === DIRECTORY_SEPARATOR) {
  22. static::$symlinkOnWindows = true;
  23. $originDir = tempnam(sys_get_temp_dir(), 'sl');
  24. $targetDir = tempnam(sys_get_temp_dir(), 'sl');
  25. if (true !== @symlink($originDir, $targetDir)) {
  26. $report = error_get_last();
  27. if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
  28. static::$symlinkOnWindows = false;
  29. }
  30. }
  31. }
  32. }
  33. public function setUp()
  34. {
  35. $this->umask = umask(0);
  36. $this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000);
  37. mkdir($this->workspace, 0777, true);
  38. $this->workspace = realpath($this->workspace);
  39. }
  40. public function tearDown()
  41. {
  42. $this->clean($this->workspace);
  43. umask($this->umask);
  44. }
  45. /**
  46. * @param string $file
  47. */
  48. protected function clean($file)
  49. {
  50. if (is_dir($file) && !is_link($file)) {
  51. $dir = new \FilesystemIterator($file);
  52. foreach ($dir as $childFile) {
  53. $this->clean($childFile);
  54. }
  55. rmdir($file);
  56. } else {
  57. unlink($file);
  58. }
  59. }
  60. /**
  61. * @param int $expectedFilePerms expected file permissions as three digits (i.e. 755)
  62. * @param string $filePath
  63. */
  64. protected function assertFilePermissions($expectedFilePerms, $filePath)
  65. {
  66. $actualFilePerms = (int) substr(sprintf('%o', fileperms($filePath)), -3);
  67. $this->assertEquals(
  68. $expectedFilePerms,
  69. $actualFilePerms,
  70. sprintf('File permissions for %s must be %s. Actual %s', $filePath, $expectedFilePerms, $actualFilePerms)
  71. );
  72. }
  73. protected function getFileOwner($filepath)
  74. {
  75. $this->markAsSkippedIfPosixIsMissing();
  76. $infos = stat($filepath);
  77. if ($datas = posix_getpwuid($infos['uid'])) {
  78. return $datas['name'];
  79. }
  80. }
  81. protected function getFileGroup($filepath)
  82. {
  83. $this->markAsSkippedIfPosixIsMissing();
  84. $infos = stat($filepath);
  85. if ($datas = posix_getgrgid($infos['gid'])) {
  86. return $datas['name'];
  87. }
  88. $this->markTestSkipped('Unable to retrieve file group name');
  89. }
  90. protected function markAsSkippedIfSymlinkIsMissing()
  91. {
  92. if (!function_exists('symlink')) {
  93. $this->markTestSkipped('symlink is not supported');
  94. }
  95. if ('\\' === DIRECTORY_SEPARATOR && false === static::$symlinkOnWindows) {
  96. $this->markTestSkipped('symlink requires "Create symbolic links" privilege on windows');
  97. }
  98. }
  99. protected function markAsSkippedIfChmodIsMissing()
  100. {
  101. if ('\\' === DIRECTORY_SEPARATOR) {
  102. $this->markTestSkipped('chmod is not supported on windows');
  103. }
  104. }
  105. protected function markAsSkippedIfPosixIsMissing()
  106. {
  107. if ('\\' === DIRECTORY_SEPARATOR || !function_exists('posix_isatty')) {
  108. $this->markTestSkipped('Posix is not supported');
  109. }
  110. }
  111. }