LockHandlerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Exception\IOException;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. use Symfony\Component\Filesystem\LockHandler;
  15. class LockHandlerTest extends TestCase
  16. {
  17. /**
  18. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  19. * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
  20. */
  21. public function testConstructWhenRepositoryDoesNotExist()
  22. {
  23. if (!getenv('USER') || 'root' === getenv('USER')) {
  24. $this->markTestSkipped('This test will fail if run under superuser');
  25. }
  26. new LockHandler('lock', '/a/b/c/d/e');
  27. }
  28. /**
  29. * @expectedException \Symfony\Component\Filesystem\Exception\IOException
  30. * @expectedExceptionMessage The directory "/" is not writable.
  31. */
  32. public function testConstructWhenRepositoryIsNotWriteable()
  33. {
  34. if (!getenv('USER') || 'root' === getenv('USER')) {
  35. $this->markTestSkipped('This test will fail if run under superuser');
  36. }
  37. new LockHandler('lock', '/');
  38. }
  39. public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
  40. {
  41. // skip test on Windows; PHP can't easily set file as unreadable on Windows
  42. if ('\\' === \DIRECTORY_SEPARATOR) {
  43. $this->markTestSkipped('This test cannot run on Windows.');
  44. }
  45. if (!getenv('USER') || 'root' === getenv('USER')) {
  46. $this->markTestSkipped('This test will fail if run under superuser');
  47. }
  48. $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
  49. $e = null;
  50. $wrongMessage = null;
  51. try {
  52. mkdir($lockPath);
  53. $lockHandler = new LockHandler('lock', $lockPath);
  54. chmod($lockPath, 0444);
  55. $lockHandler->lock();
  56. } catch (IOException $e) {
  57. if (false === strpos($e->getMessage(), 'Permission denied')) {
  58. $wrongMessage = $e->getMessage();
  59. } else {
  60. $this->addToAssertionCount(1);
  61. }
  62. } catch (\Exception $e) {
  63. } catch (\Throwable $e) {
  64. }
  65. if (is_dir($lockPath)) {
  66. $fs = new Filesystem();
  67. $fs->remove($lockPath);
  68. }
  69. $this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', \get_class($e)));
  70. $this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
  71. }
  72. public function testConstructSanitizeName()
  73. {
  74. $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
  75. $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
  76. // ensure the file does not exist before the lock
  77. @unlink($file);
  78. $lock->lock();
  79. $this->assertFileExists($file);
  80. $lock->release();
  81. }
  82. public function testLockRelease()
  83. {
  84. $name = 'symfony-test-filesystem.lock';
  85. $l1 = new LockHandler($name);
  86. $l2 = new LockHandler($name);
  87. $this->assertTrue($l1->lock());
  88. $this->assertFalse($l2->lock());
  89. $l1->release();
  90. $this->assertTrue($l2->lock());
  91. $l2->release();
  92. }
  93. public function testLockTwice()
  94. {
  95. $name = 'symfony-test-filesystem.lock';
  96. $lockHandler = new LockHandler($name);
  97. $this->assertTrue($lockHandler->lock());
  98. $this->assertTrue($lockHandler->lock());
  99. $lockHandler->release();
  100. }
  101. public function testLockIsReleased()
  102. {
  103. $name = 'symfony-test-filesystem.lock';
  104. $l1 = new LockHandler($name);
  105. $l2 = new LockHandler($name);
  106. $this->assertTrue($l1->lock());
  107. $this->assertFalse($l2->lock());
  108. $l1 = null;
  109. $this->assertTrue($l2->lock());
  110. $l2->release();
  111. }
  112. }