e0b2281e0552a22918733f324a7597ba47418469
[yaffs-website] / vendor / symfony / filesystem / Tests / LockHandlerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Filesystem\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Filesystem\Exception\IOException;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Symfony\Component\Filesystem\LockHandler;
18
19 /**
20  * @group legacy
21  */
22 class LockHandlerTest extends TestCase
23 {
24     /**
25      * @expectedException \Symfony\Component\Filesystem\Exception\IOException
26      * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
27      */
28     public function testConstructWhenRepositoryDoesNotExist()
29     {
30         if (!getenv('USER') || 'root' === getenv('USER')) {
31             $this->markTestSkipped('This test will fail if run under superuser');
32         }
33         new LockHandler('lock', '/a/b/c/d/e');
34     }
35
36     /**
37      * @expectedException \Symfony\Component\Filesystem\Exception\IOException
38      * @expectedExceptionMessage The directory "/" is not writable.
39      */
40     public function testConstructWhenRepositoryIsNotWriteable()
41     {
42         if (!getenv('USER') || 'root' === getenv('USER')) {
43             $this->markTestSkipped('This test will fail if run under superuser');
44         }
45         new LockHandler('lock', '/');
46     }
47
48     public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
49     {
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.');
53         }
54
55         $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
56         $e = null;
57         $wrongMessage = null;
58
59         try {
60             mkdir($lockPath);
61
62             $lockHandler = new LockHandler('lock', $lockPath);
63
64             chmod($lockPath, 0444);
65
66             $lockHandler->lock();
67         } catch (IOException $e) {
68             if (false === strpos($e->getMessage(), 'Permission denied')) {
69                 $wrongMessage = $e->getMessage();
70             } else {
71                 $this->addToAssertionCount(1);
72             }
73         } catch (\Exception $e) {
74         } catch (\Throwable $e) {
75         }
76
77         if (is_dir($lockPath)) {
78             $fs = new Filesystem();
79             $fs->remove($lockPath);
80         }
81
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));
84     }
85
86     public function testConstructSanitizeName()
87     {
88         $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
89
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
92         @unlink($file);
93
94         $lock->lock();
95
96         $this->assertFileExists($file);
97
98         $lock->release();
99     }
100
101     public function testLockRelease()
102     {
103         $name = 'symfony-test-filesystem.lock';
104
105         $l1 = new LockHandler($name);
106         $l2 = new LockHandler($name);
107
108         $this->assertTrue($l1->lock());
109         $this->assertFalse($l2->lock());
110
111         $l1->release();
112
113         $this->assertTrue($l2->lock());
114         $l2->release();
115     }
116
117     public function testLockTwice()
118     {
119         $name = 'symfony-test-filesystem.lock';
120
121         $lockHandler = new LockHandler($name);
122
123         $this->assertTrue($lockHandler->lock());
124         $this->assertTrue($lockHandler->lock());
125
126         $lockHandler->release();
127     }
128
129     public function testLockIsReleased()
130     {
131         $name = 'symfony-test-filesystem.lock';
132
133         $l1 = new LockHandler($name);
134         $l2 = new LockHandler($name);
135
136         $this->assertTrue($l1->lock());
137         $this->assertFalse($l2->lock());
138
139         $l1 = null;
140
141         $this->assertTrue($l2->lock());
142         $l2->release();
143     }
144 }