FilesystemTestCase.php 3.5 KB

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