scratch – Blame information for rev 126

Subversion Repositories:
Rev:
Rev Author Line No. Line
126 office 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 class LockHandlerTest extends TestCase
20 {
21 /**
22 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
23 * @expectedExceptionMessage Failed to create "/a/b/c/d/e": mkdir(): Permission denied.
24 */
25 public function testConstructWhenRepositoryDoesNotExist()
26 {
27 if (!getenv('USER') || 'root' === getenv('USER')) {
28 $this->markTestSkipped('This test will fail if run under superuser');
29 }
30 new LockHandler('lock', '/a/b/c/d/e');
31 }
32  
33 /**
34 * @expectedException \Symfony\Component\Filesystem\Exception\IOException
35 * @expectedExceptionMessage The directory "/" is not writable.
36 */
37 public function testConstructWhenRepositoryIsNotWriteable()
38 {
39 if (!getenv('USER') || 'root' === getenv('USER')) {
40 $this->markTestSkipped('This test will fail if run under superuser');
41 }
42 new LockHandler('lock', '/');
43 }
44  
45 public function testErrorHandlingInLockIfLockPathBecomesUnwritable()
46 {
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.');
50 }
51  
52 $lockPath = sys_get_temp_dir().'/'.uniqid('', true);
53 $e = null;
54 $wrongMessage = null;
55  
56 try {
57 mkdir($lockPath);
58  
59 $lockHandler = new LockHandler('lock', $lockPath);
60  
61 chmod($lockPath, 0444);
62  
63 $lockHandler->lock();
64 } catch (IOException $e) {
65 if (false === strpos($e->getMessage(), 'Permission denied')) {
66 $wrongMessage = $e->getMessage();
67 } else {
68 $this->addToAssertionCount(1);
69 }
70 } catch (\Exception $e) {
71 } catch (\Throwable $e) {
72 }
73  
74 if (is_dir($lockPath)) {
75 $fs = new Filesystem();
76 $fs->remove($lockPath);
77 }
78  
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));
81 }
82  
83 public function testConstructSanitizeName()
84 {
85 $lock = new LockHandler('<?php echo "% hello word ! %" ?>');
86  
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
89 @unlink($file);
90  
91 $lock->lock();
92  
93 $this->assertFileExists($file);
94  
95 $lock->release();
96 }
97  
98 public function testLockRelease()
99 {
100 $name = 'symfony-test-filesystem.lock';
101  
102 $l1 = new LockHandler($name);
103 $l2 = new LockHandler($name);
104  
105 $this->assertTrue($l1->lock());
106 $this->assertFalse($l2->lock());
107  
108 $l1->release();
109  
110 $this->assertTrue($l2->lock());
111 $l2->release();
112 }
113  
114 public function testLockTwice()
115 {
116 $name = 'symfony-test-filesystem.lock';
117  
118 $lockHandler = new LockHandler($name);
119  
120 $this->assertTrue($lockHandler->lock());
121 $this->assertTrue($lockHandler->lock());
122  
123 $lockHandler->release();
124 }
125  
126 public function testLockIsReleased()
127 {
128 $name = 'symfony-test-filesystem.lock';
129  
130 $l1 = new LockHandler($name);
131 $l2 = new LockHandler($name);
132  
133 $this->assertTrue($l1->lock());
134 $this->assertFalse($l2->lock());
135  
136 $l1 = null;
137  
138 $this->assertTrue($l2->lock());
139 $l2->release();
140 }
141 }