|
18 | 18 | */
|
19 | 19 | class FilesystemTest extends \PHPUnit_Framework_TestCase
|
20 | 20 | {
|
| 21 | + /** |
| 22 | + * @var string $workspace |
| 23 | + */ |
| 24 | + private $workspace = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Symfony\Component\Filesystem\Filesystem $filesystem |
| 28 | + */ |
| 29 | + private $filesystem = null; |
| 30 | + |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + $this->filesystem = new Filesystem(); |
| 34 | + $this->workspace = sys_get_temp_dir().DIRECTORY_SEPARATOR.time().rand(0, 1000); |
| 35 | + mkdir($this->workspace, 0777, true); |
| 36 | + } |
| 37 | + |
| 38 | + public function tearDown() |
| 39 | + { |
| 40 | + $this->clean($this->workspace); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string $file |
| 45 | + */ |
| 46 | + private function clean($file) |
| 47 | + { |
| 48 | + if (!file_exists($file)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (is_dir($file) && !is_link($file)) { |
| 53 | + $dir = new \FilesystemIterator($file); |
| 54 | + foreach ($dir as $childFile) { |
| 55 | + $this->clean($childFile); |
| 56 | + } |
| 57 | + |
| 58 | + rmdir($file); |
| 59 | + } else { |
| 60 | + unlink($file); |
| 61 | + } |
| 62 | + } |
| 63 | + |
21 | 64 | public function testCopyCreatesNewFile()
|
22 | 65 | {
|
23 |
| - $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
24 |
| - $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 66 | + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; |
| 67 | + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; |
25 | 68 |
|
26 | 69 | file_put_contents($sourceFilePath, 'SOURCE FILE');
|
27 | 70 |
|
28 |
| - $filesystem = new Filesystem(); |
29 |
| - $filesystem->copy($sourceFilePath, $targetFilePath); |
| 71 | + $this->filesystem->copy($sourceFilePath, $targetFilePath); |
30 | 72 |
|
31 | 73 | $this->assertFileExists($targetFilePath);
|
32 | 74 | $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
|
33 |
| - |
34 |
| - unlink($sourceFilePath); |
35 |
| - unlink($targetFilePath); |
36 | 75 | }
|
37 | 76 |
|
38 | 77 | public function testCopyOverridesExistingFileIfModified()
|
39 | 78 | {
|
40 |
| - $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
41 |
| - $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 79 | + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; |
| 80 | + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; |
42 | 81 |
|
43 | 82 | file_put_contents($sourceFilePath, 'SOURCE FILE');
|
44 | 83 | file_put_contents($targetFilePath, 'TARGET FILE');
|
45 | 84 | touch($targetFilePath, time() - 1000);
|
46 | 85 |
|
47 |
| - $filesystem = new Filesystem(); |
48 |
| - $filesystem->copy($sourceFilePath, $targetFilePath); |
| 86 | + $this->filesystem->copy($sourceFilePath, $targetFilePath); |
49 | 87 |
|
50 | 88 | $this->assertFileExists($targetFilePath);
|
51 | 89 | $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
|
52 |
| - |
53 |
| - unlink($sourceFilePath); |
54 |
| - unlink($targetFilePath); |
55 | 90 | }
|
56 | 91 |
|
57 | 92 | public function testCopyDoesNotOverrideExistingFileByDefault()
|
58 | 93 | {
|
59 |
| - $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
60 |
| - $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 94 | + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; |
| 95 | + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; |
61 | 96 |
|
62 | 97 | file_put_contents($sourceFilePath, 'SOURCE FILE');
|
63 | 98 | file_put_contents($targetFilePath, 'TARGET FILE');
|
| 99 | + |
| 100 | + // make sure both files have the same modification time |
64 | 101 | $modificationTime = time() - 1000;
|
65 | 102 | touch($sourceFilePath, $modificationTime);
|
66 | 103 | touch($targetFilePath, $modificationTime);
|
67 | 104 |
|
68 |
| - $filesystem = new Filesystem(); |
69 |
| - $filesystem->copy($sourceFilePath, $targetFilePath); |
| 105 | + $this->filesystem->copy($sourceFilePath, $targetFilePath); |
70 | 106 |
|
71 | 107 | $this->assertFileExists($targetFilePath);
|
72 | 108 | $this->assertEquals('TARGET FILE', file_get_contents($targetFilePath));
|
73 |
| - |
74 |
| - unlink($sourceFilePath); |
75 |
| - unlink($targetFilePath); |
76 | 109 | }
|
77 | 110 |
|
78 | 111 | public function testCopyOverridesExistingFileIfForced()
|
79 | 112 | {
|
80 |
| - $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
81 |
| - $targetFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_target_file'; |
| 113 | + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; |
| 114 | + $targetFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_target_file'; |
82 | 115 |
|
83 | 116 | file_put_contents($sourceFilePath, 'SOURCE FILE');
|
84 | 117 | file_put_contents($targetFilePath, 'TARGET FILE');
|
| 118 | + |
| 119 | + // make sure both files have the same modification time |
85 | 120 | $modificationTime = time() - 1000;
|
86 | 121 | touch($sourceFilePath, $modificationTime);
|
87 | 122 | touch($targetFilePath, $modificationTime);
|
88 | 123 |
|
89 |
| - $filesystem = new Filesystem(); |
90 |
| - $filesystem->copy($sourceFilePath, $targetFilePath, true); |
| 124 | + $this->filesystem->copy($sourceFilePath, $targetFilePath, true); |
91 | 125 |
|
92 | 126 | $this->assertFileExists($targetFilePath);
|
93 | 127 | $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
|
94 |
| - |
95 |
| - unlink($sourceFilePath); |
96 |
| - unlink($targetFilePath); |
97 | 128 | }
|
98 | 129 |
|
99 | 130 | public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
|
100 | 131 | {
|
101 |
| - $sourceFilePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'copy_source_file'; |
102 |
| - $targetFileDirectory = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 132 | + $sourceFilePath = $this->workspace.DIRECTORY_SEPARATOR.'copy_source_file'; |
| 133 | + $targetFileDirectory = $this->workspace.DIRECTORY_SEPARATOR.'directory'; |
103 | 134 | $targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file';
|
104 | 135 |
|
105 | 136 | file_put_contents($sourceFilePath, 'SOURCE FILE');
|
106 | 137 |
|
107 |
| - $filesystem = new Filesystem(); |
108 |
| - $filesystem->copy($sourceFilePath, $targetFilePath); |
| 138 | + $this->filesystem->copy($sourceFilePath, $targetFilePath); |
109 | 139 |
|
110 | 140 | $this->assertTrue(is_dir($targetFileDirectory));
|
111 | 141 | $this->assertFileExists($targetFilePath);
|
112 | 142 | $this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
|
113 |
| - |
114 |
| - unlink($sourceFilePath); |
115 |
| - unlink($targetFilePath); |
116 |
| - rmdir($targetFileDirectory); |
117 | 143 | }
|
118 | 144 |
|
119 | 145 | public function testMkdirCreatesDirectoriesRecursively()
|
120 | 146 | {
|
121 |
| - $directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
122 |
| - $subDirectory = $directory.DIRECTORY_SEPARATOR.'sub_directory'; |
| 147 | + $directory = $this->workspace |
| 148 | + .DIRECTORY_SEPARATOR.'directory' |
| 149 | + .DIRECTORY_SEPARATOR.'sub_directory'; |
123 | 150 |
|
124 |
| - $filesystem = new Filesystem(); |
125 |
| - $result = $filesystem->mkdir($subDirectory); |
| 151 | + $result = $this->filesystem->mkdir($directory); |
126 | 152 |
|
127 | 153 | $this->assertTrue($result);
|
128 |
| - $this->assertTrue(is_dir($subDirectory)); |
129 |
| - |
130 |
| - rmdir($subDirectory); |
131 |
| - rmdir($directory); |
| 154 | + $this->assertTrue(is_dir($directory)); |
132 | 155 | }
|
133 | 156 |
|
134 | 157 | public function testMkdirCreatesDirectoriesFromArray()
|
135 | 158 | {
|
136 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 159 | + $basePath = $this->workspace.DIRECTORY_SEPARATOR; |
137 | 160 | $directories = array(
|
138 | 161 | $basePath.'1', $basePath.'2', $basePath.'3'
|
139 | 162 | );
|
140 | 163 |
|
141 |
| - $filesystem = new Filesystem(); |
142 |
| - $result = $filesystem->mkdir($directories); |
| 164 | + $result = $this->filesystem->mkdir($directories); |
143 | 165 |
|
144 | 166 | $this->assertTrue($result);
|
145 | 167 | $this->assertTrue(is_dir($basePath.'1'));
|
146 | 168 | $this->assertTrue(is_dir($basePath.'2'));
|
147 | 169 | $this->assertTrue(is_dir($basePath.'3'));
|
148 |
| - |
149 |
| - rmdir($basePath.'1'); |
150 |
| - rmdir($basePath.'2'); |
151 |
| - rmdir($basePath.'3'); |
152 | 170 | }
|
153 | 171 |
|
154 | 172 | public function testMkdirCreatesDirectoriesFromTraversableObject()
|
155 | 173 | {
|
156 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 174 | + $basePath = $this->workspace.DIRECTORY_SEPARATOR; |
157 | 175 | $directories = new \ArrayObject(array(
|
158 | 176 | $basePath.'1', $basePath.'2', $basePath.'3'
|
159 | 177 | ));
|
160 | 178 |
|
161 |
| - $filesystem = new Filesystem(); |
162 |
| - $result = $filesystem->mkdir($directories); |
| 179 | + $result = $this->filesystem->mkdir($directories); |
163 | 180 |
|
164 | 181 | $this->assertTrue($result);
|
165 | 182 | $this->assertTrue(is_dir($basePath.'1'));
|
166 | 183 | $this->assertTrue(is_dir($basePath.'2'));
|
167 | 184 | $this->assertTrue(is_dir($basePath.'3'));
|
168 |
| - |
169 |
| - rmdir($basePath.'1'); |
170 |
| - rmdir($basePath.'2'); |
171 |
| - rmdir($basePath.'3'); |
172 | 185 | }
|
173 | 186 |
|
174 | 187 | public function testMkdirCreatesDirectoriesEvenIfItFailsToCreateOneOfThem()
|
175 | 188 | {
|
176 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 189 | + $basePath = $this->workspace.DIRECTORY_SEPARATOR; |
177 | 190 | $directories = array(
|
178 | 191 | $basePath.'1', $basePath.'2', $basePath.'3'
|
179 | 192 | );
|
180 | 193 |
|
181 | 194 | // create a file to make that directory cannot be created
|
182 | 195 | file_put_contents($basePath.'2', '');
|
183 | 196 |
|
184 |
| - $filesystem = new Filesystem(); |
185 |
| - $result = $filesystem->mkdir($directories); |
| 197 | + $result = $this->filesystem->mkdir($directories); |
186 | 198 |
|
187 | 199 | $this->assertFalse($result);
|
188 | 200 | $this->assertTrue(is_dir($basePath.'1'));
|
189 | 201 | $this->assertFalse(is_dir($basePath.'2'));
|
190 | 202 | $this->assertTrue(is_dir($basePath.'3'));
|
191 |
| - |
192 |
| - rmdir($basePath.'1'); |
193 |
| - unlink($basePath.'2'); |
194 |
| - rmdir($basePath.'3'); |
195 | 203 | }
|
196 | 204 |
|
197 | 205 | public function testTouchCreatesEmptyFile()
|
198 | 206 | {
|
199 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
200 |
| - $file = $basePath.'1'; |
201 |
| - |
202 |
| - $filesystem = new Filesystem(); |
203 |
| - $filesystem->touch($file); |
| 207 | + $file = $this->workspace.DIRECTORY_SEPARATOR.'1'; |
204 | 208 |
|
205 |
| - $this->assertFileExists($basePath.'1'); |
| 209 | + $this->filesystem->touch($file); |
206 | 210 |
|
207 |
| - unlink($basePath.'1'); |
| 211 | + $this->assertFileExists($file); |
208 | 212 | }
|
209 | 213 |
|
210 | 214 | public function testTouchCreatesEmptyFilesFromArray()
|
211 | 215 | {
|
212 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 216 | + $basePath = $this->workspace.DIRECTORY_SEPARATOR; |
213 | 217 | $files = array(
|
214 | 218 | $basePath.'1', $basePath.'2', $basePath.'3'
|
215 | 219 | );
|
216 |
| - mkdir($basePath); |
217 | 220 |
|
218 |
| - $filesystem = new Filesystem(); |
219 |
| - $filesystem->touch($files); |
| 221 | + $this->filesystem->touch($files); |
220 | 222 |
|
221 | 223 | $this->assertFileExists($basePath.'1');
|
222 | 224 | $this->assertFileExists($basePath.'2');
|
223 | 225 | $this->assertFileExists($basePath.'3');
|
224 |
| - |
225 |
| - unlink($basePath.'1'); |
226 |
| - unlink($basePath.'2'); |
227 |
| - unlink($basePath.'3'); |
228 |
| - rmdir($basePath); |
229 | 226 | }
|
230 | 227 |
|
231 | 228 | public function testTouchCreatesEmptyFilesFromTraversableObject()
|
232 | 229 | {
|
233 |
| - $basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time(); |
| 230 | + $basePath = $this->workspace.DIRECTORY_SEPARATOR; |
234 | 231 | $files = new \ArrayObject(array(
|
235 | 232 | $basePath.'1', $basePath.'2', $basePath.'3'
|
236 | 233 | ));
|
237 |
| - mkdir($basePath); |
238 | 234 |
|
239 |
| - $filesystem = new Filesystem(); |
240 |
| - $filesystem->touch($files); |
| 235 | + $this->filesystem->touch($files); |
241 | 236 |
|
242 | 237 | $this->assertFileExists($basePath.'1');
|
243 | 238 | $this->assertFileExists($basePath.'2');
|
244 | 239 | $this->assertFileExists($basePath.'3');
|
245 |
| - |
246 |
| - unlink($basePath.'1'); |
247 |
| - unlink($basePath.'2'); |
248 |
| - unlink($basePath.'3'); |
249 |
| - rmdir($basePath); |
250 | 240 | }
|
251 | 241 | }
|
0 commit comments