Pull merge.
[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         if (!getenv('USER') || 'root' === getenv('USER')) {
56             $this->markTestSkipped('This test will fail if run under superuser');
57         }
58
59         $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
60         $e = null;
61         $wrongMessage = null;
62
63         try {
64             mkdir($lockPath);
65
66             $lockHandler = new LockHandler('lock', $lockPath);
67
68             chmod($lockPath, 0444);
69
70             $lockHandler->lock();
71         } catch (IOException $e) {
72             if (false === strpos($e->getMessage(), 'Permission denied')) {
73                 $wrongMessage = $e->getMessage();
74             } else {
75                 $this->addToAssertionCount(1);
76             }
77         } catch (\Exception $e) {
78         } catch (\Throwable $e) {
79         }
80
81         if (is_dir($lockPath)) {
82             $fs = new Filesystem();
83             $fs->remove($lockPath);
84         }
85
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));
88     }
89
90     public function testConstructSanitizeName()
91     {
92         $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
93
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
96         @unlink($file);
97
98         $lock->lock();
99
100         $this->assertFileExists($file);
101
102         $lock->release();
103     }
104
105     public function testLockRelease()
106     {
107         $name = 'symfony-test-filesystem.lock';
108
109         $l1 = new LockHandler($name);
110         $l2 = new LockHandler($name);
111
112         $this->assertTrue($l1->lock());
113         $this->assertFalse($l2->lock());
114
115         $l1->release();
116
117         $this->assertTrue($l2->lock());
118         $l2->release();
119     }
120
121     public function testLockTwice()
122     {
123         $name = 'symfony-test-filesystem.lock';
124
125         $lockHandler = new LockHandler($name);
126
127         $this->assertTrue($lockHandler->lock());
128         $this->assertTrue($lockHandler->lock());
129
130         $lockHandler->release();
131     }
132
133     public function testLockIsReleased()
134     {
135         $name = 'symfony-test-filesystem.lock';
136
137         $l1 = new LockHandler($name);
138         $l2 = new LockHandler($name);
139
140         $this->assertTrue($l1->lock());
141         $this->assertFalse($l2->lock());
142
143         $l1 = null;
144
145         $this->assertTrue($l2->lock());
146         $l2->release();
147     }
148 }