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 $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
62 $lockHandler = new LockHandler('lock', $lockPath);
64 chmod($lockPath, 0444);
67 } catch (IOException $e) {
68 if (false === strpos($e->getMessage(), 'Permission denied')) {
69 $wrongMessage = $e->getMessage();
71 $this->addToAssertionCount(1);
73 } catch (\Exception $e) {
74 } catch (\Throwable $e) {
77 if (is_dir($lockPath)) {
78 $fs = new Filesystem();
79 $fs->remove($lockPath);
82 $this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', get_class($e)));
83 $this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
86 public function testConstructSanitizeName()
88 $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
90 $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
91 // ensure the file does not exist before the lock
96 $this->assertFileExists($file);
101 public function testLockRelease()
103 $name = 'symfony-test-filesystem.lock';
105 $l1 = new LockHandler($name);
106 $l2 = new LockHandler($name);
108 $this->assertTrue($l1->lock());
109 $this->assertFalse($l2->lock());
113 $this->assertTrue($l2->lock());
117 public function testLockTwice()
119 $name = 'symfony-test-filesystem.lock';
121 $lockHandler = new LockHandler($name);
123 $this->assertTrue($lockHandler->lock());
124 $this->assertTrue($lockHandler->lock());
126 $lockHandler->release();
129 public function testLockIsReleased()
131 $name = 'symfony-test-filesystem.lock';
133 $l1 = new LockHandler($name);
134 $l2 = new LockHandler($name);
136 $this->assertTrue($l1->lock());
137 $this->assertFalse($l2->lock());
141 $this->assertTrue($l2->lock());