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;
19 class LockHandlerTest extends TestCase
22 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
23 * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
25 public function testConstructWhenRepositoryDoesNotExist()
27 if (!getenv('USER') || 'root' === getenv('USER')) {
28 $this->markTestSkipped('This test will fail if run under superuser');
30 new LockHandler('lock', '/a/b/c/d/e');
34 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
35 * @expectedExceptionMessage The directory "/" is not writable.
37 public function testConstructWhenRepositoryIsNotWriteable()
39 if (!getenv('USER') || 'root' === getenv('USER')) {
40 $this->markTestSkipped('This test will fail if run under superuser');
42 new LockHandler('lock', '/');
45 public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
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.');
52 $lockPath = sys_get_temp_dir().'/'.uniqid();
59 $lockHandler = new LockHandler('lock', $lockPath);
61 chmod($lockPath, 0444);
64 } catch (IOException $e) {
65 if (false === strpos($e->getMessage(), 'Permission denied')) {
66 $wrongMessage = $e->getMessage();
68 $this->addToAssertionCount(1);
70 } catch (\Exception $e) {
71 } catch (\Throwable $e) {
74 if (is_dir($lockPath)) {
75 $fs = new Filesystem();
76 $fs->remove($lockPath);
79 $this->assertInstanceOf('Symfony\Component\Filesystem\Exception\IOException', $e, sprintf('Expected IOException to be thrown, got %s instead.', get_class($e)));
80 $this->assertNull($wrongMessage, sprintf('Expected exception message to contain "Permission denied", got "%s" instead.', $wrongMessage));
83 public function testConstructSanitizeName()
85 $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
87 $file = sprintf('%s/sf.-php-echo-hello-word-.4b3d9d0d27ddef3a78a64685dda3a963e478659a9e5240feaf7b4173a8f28d5f.lock', sys_get_temp_dir());
88 // ensure the file does not exist before the lock
93 $this->assertFileExists($file);
98 public function testLockRelease()
100 $name = 'symfony-test-filesystem.lock';
102 $l1 = new LockHandler($name);
103 $l2 = new LockHandler($name);
105 $this->assertTrue($l1->lock());
106 $this->assertFalse($l2->lock());
110 $this->assertTrue($l2->lock());
114 public function testLockTwice()
116 $name = 'symfony-test-filesystem.lock';
118 $lockHandler = new LockHandler($name);
120 $this->assertTrue($lockHandler->lock());
121 $this->assertTrue($lockHandler->lock());
123 $lockHandler->release();
126 public function testLockIsReleased()
128 $name = 'symfony-test-filesystem.lock';
130 $l1 = new LockHandler($name);
131 $l2 = new LockHandler($name);
133 $this->assertTrue($l1->lock());
134 $this->assertFalse($l2->lock());
138 $this->assertTrue($l2->lock());