4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Filesystem\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Filesystem\Exception\IOException;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Symfony\Component\Filesystem\LockHandler;
22 class LockHandlerTest extends TestCase
25 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
26 * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
28 public function testConstructWhenRepositoryDoesNotExist()
30 if (!getenv('USER') || 'root' === getenv('USER')) {
31 $this->markTestSkipped('This test will fail if run under superuser');
33 new LockHandler('lock', '/a/b/c/d/e');
37 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
38 * @expectedExceptionMessage The directory "/" is not writable.
40 public function testConstructWhenRepositoryIsNotWriteable()
42 if (!getenv('USER') || 'root' === getenv('USER')) {
43 $this->markTestSkipped('This test will fail if run under superuser');
45 new LockHandler('lock', '/');
48 public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
50 // skip test on Windows; PHP can't easily set file as unreadable on Windows
51 if ('\\' === \DIRECTORY_SEPARATOR) {
52 $this->markTestSkipped('This test cannot run on Windows.');
55 if (!getenv('USER') || 'root' === getenv('USER')) {
56 $this->markTestSkipped('This test will fail if run under superuser');
59 $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
66 $lockHandler = new LockHandler('lock', $lockPath);
68 chmod($lockPath, 0444);
71 } catch (IOException $e) {
72 if (false === strpos($e->getMessage(), 'Permission denied')) {
73 $wrongMessage = $e->getMessage();
75 $this->addToAssertionCount(1);
77 } catch (\Exception $e) {
78 } catch (\Throwable $e) {
81 if (is_dir($lockPath)) {
82 $fs = new Filesystem();
83 $fs->remove($lockPath);
86 $this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', \get_class($e)));
87 $this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
90 public function testConstructSanitizeName()
92 $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
94 $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
95 // ensure the file does not exist before the lock
100 $this->assertFileExists($file);
105 public function testLockRelease()
107 $name = 'symfony-test-filesystem.lock';
109 $l1 = new LockHandler($name);
110 $l2 = new LockHandler($name);
112 $this->assertTrue($l1->lock());
113 $this->assertFalse($l2->lock());
117 $this->assertTrue($l2->lock());
121 public function testLockTwice()
123 $name = 'symfony-test-filesystem.lock';
125 $lockHandler = new LockHandler($name);
127 $this->assertTrue($lockHandler->lock());
128 $this->assertTrue($lockHandler->lock());
130 $lockHandler->release();
133 public function testLockIsReleased()
135 $name = 'symfony-test-filesystem.lock';
137 $l1 = new LockHandler($name);
138 $l2 = new LockHandler($name);
140 $this->assertTrue($l1->lock());
141 $this->assertFalse($l2->lock());
145 $this->assertTrue($l2->lock());