|
12 | 12 | namespace Symfony\Component\Filesystem\Tests;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\Filesystem\Exception\IOException; |
| 16 | +use Symfony\Component\Filesystem\Filesystem; |
15 | 17 | use Symfony\Component\Filesystem\LockHandler;
|
16 | 18 |
|
17 | 19 | class LockHandlerTest extends TestCase
|
@@ -40,6 +42,49 @@ public function testConstructWhenRepositoryIsNotWriteable()
|
40 | 42 | new LockHandler('lock', '/');
|
41 | 43 | }
|
42 | 44 |
|
| 45 | + public function testErrorHandlingInLockIfLockPathBecomesUnwritable() |
| 46 | + { |
| 47 | + // skip test on Windows; PHP can't easily set file as unreadable on Windows |
| 48 | + if ('\\' === DIRECTORY_SEPARATOR) { |
| 49 | + $this->markTestSkipped('This test cannot run on Windows.'); |
| 50 | + } |
| 51 | + |
| 52 | + $lockPath = sys_get_temp_dir().'/'.uniqid(); |
| 53 | + $e = null; |
| 54 | + $wrongMessage = null; |
| 55 | + |
| 56 | + try { |
| 57 | + mkdir($lockPath); |
| 58 | + |
| 59 | + $lockHandler = new LockHandler('lock', $lockPath); |
| 60 | + |
| 61 | + chmod($lockPath, 0444); |
| 62 | + |
| 63 | + $lockHandler->lock(); |
| 64 | + } catch (IOException $e) { |
| 65 | + if (false === strpos($e->getMessage(), 'Permission denied')) { |
| 66 | + $wrongMessage = $e->getMessage(); |
| 67 | + } else { |
| 68 | + $this->addToAssertionCount(1); |
| 69 | + } |
| 70 | + } catch (\Exception $e) { |
| 71 | + } catch (\Throwable $e) { |
| 72 | + } |
| 73 | + |
| 74 | + if (is_dir($lockPath)) { |
| 75 | + $fs = new Filesystem(); |
| 76 | + $fs->remove($lockPath); |
| 77 | + } |
| 78 | + |
| 79 | + if (!$e instanceof IOException) { |
| 80 | + $this->fail(sprintf('Expected IOException to be thrown, got %s instead.', get_class($e))); |
| 81 | + } |
| 82 | + |
| 83 | + if (null !== $wrongMessage) { |
| 84 | + $this->fail(sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage)); |
| 85 | + } |
| 86 | + } |
| 87 | + |
43 | 88 | public function testConstructSanitizeName()
|
44 | 89 | {
|
45 | 90 | $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
|
|
0 commit comments