Skip to content

Commit 8e861b7

Browse files
committed
[Filesystem] Introduced workspace directory to limit complexity of tests.
1 parent a91e200 commit 8e861b7

File tree

1 file changed

+80
-90
lines changed

1 file changed

+80
-90
lines changed

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 80 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,234 +18,224 @@
1818
*/
1919
class FilesystemTest extends \PHPUnit_Framework_TestCase
2020
{
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+
2164
public function testCopyCreatesNewFile()
2265
{
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';
2568

2669
file_put_contents($sourceFilePath, 'SOURCE FILE');
2770

28-
$filesystem = new Filesystem();
29-
$filesystem->copy($sourceFilePath, $targetFilePath);
71+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
3072

3173
$this->assertFileExists($targetFilePath);
3274
$this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
33-
34-
unlink($sourceFilePath);
35-
unlink($targetFilePath);
3675
}
3776

3877
public function testCopyOverridesExistingFileIfModified()
3978
{
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';
4281

4382
file_put_contents($sourceFilePath, 'SOURCE FILE');
4483
file_put_contents($targetFilePath, 'TARGET FILE');
4584
touch($targetFilePath, time() - 1000);
4685

47-
$filesystem = new Filesystem();
48-
$filesystem->copy($sourceFilePath, $targetFilePath);
86+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
4987

5088
$this->assertFileExists($targetFilePath);
5189
$this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
52-
53-
unlink($sourceFilePath);
54-
unlink($targetFilePath);
5590
}
5691

5792
public function testCopyDoesNotOverrideExistingFileByDefault()
5893
{
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';
6196

6297
file_put_contents($sourceFilePath, 'SOURCE FILE');
6398
file_put_contents($targetFilePath, 'TARGET FILE');
99+
100+
// make sure both files have the same modification time
64101
$modificationTime = time() - 1000;
65102
touch($sourceFilePath, $modificationTime);
66103
touch($targetFilePath, $modificationTime);
67104

68-
$filesystem = new Filesystem();
69-
$filesystem->copy($sourceFilePath, $targetFilePath);
105+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
70106

71107
$this->assertFileExists($targetFilePath);
72108
$this->assertEquals('TARGET FILE', file_get_contents($targetFilePath));
73-
74-
unlink($sourceFilePath);
75-
unlink($targetFilePath);
76109
}
77110

78111
public function testCopyOverridesExistingFileIfForced()
79112
{
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';
82115

83116
file_put_contents($sourceFilePath, 'SOURCE FILE');
84117
file_put_contents($targetFilePath, 'TARGET FILE');
118+
119+
// make sure both files have the same modification time
85120
$modificationTime = time() - 1000;
86121
touch($sourceFilePath, $modificationTime);
87122
touch($targetFilePath, $modificationTime);
88123

89-
$filesystem = new Filesystem();
90-
$filesystem->copy($sourceFilePath, $targetFilePath, true);
124+
$this->filesystem->copy($sourceFilePath, $targetFilePath, true);
91125

92126
$this->assertFileExists($targetFilePath);
93127
$this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
94-
95-
unlink($sourceFilePath);
96-
unlink($targetFilePath);
97128
}
98129

99130
public function testCopyCreatesTargetDirectoryIfItDoesNotExist()
100131
{
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';
103134
$targetFilePath = $targetFileDirectory.DIRECTORY_SEPARATOR.'copy_target_file';
104135

105136
file_put_contents($sourceFilePath, 'SOURCE FILE');
106137

107-
$filesystem = new Filesystem();
108-
$filesystem->copy($sourceFilePath, $targetFilePath);
138+
$this->filesystem->copy($sourceFilePath, $targetFilePath);
109139

110140
$this->assertTrue(is_dir($targetFileDirectory));
111141
$this->assertFileExists($targetFilePath);
112142
$this->assertEquals('SOURCE FILE', file_get_contents($targetFilePath));
113-
114-
unlink($sourceFilePath);
115-
unlink($targetFilePath);
116-
rmdir($targetFileDirectory);
117143
}
118144

119145
public function testMkdirCreatesDirectoriesRecursively()
120146
{
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';
123150

124-
$filesystem = new Filesystem();
125-
$result = $filesystem->mkdir($subDirectory);
151+
$result = $this->filesystem->mkdir($directory);
126152

127153
$this->assertTrue($result);
128-
$this->assertTrue(is_dir($subDirectory));
129-
130-
rmdir($subDirectory);
131-
rmdir($directory);
154+
$this->assertTrue(is_dir($directory));
132155
}
133156

134157
public function testMkdirCreatesDirectoriesFromArray()
135158
{
136-
$basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time();
159+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
137160
$directories = array(
138161
$basePath.'1', $basePath.'2', $basePath.'3'
139162
);
140163

141-
$filesystem = new Filesystem();
142-
$result = $filesystem->mkdir($directories);
164+
$result = $this->filesystem->mkdir($directories);
143165

144166
$this->assertTrue($result);
145167
$this->assertTrue(is_dir($basePath.'1'));
146168
$this->assertTrue(is_dir($basePath.'2'));
147169
$this->assertTrue(is_dir($basePath.'3'));
148-
149-
rmdir($basePath.'1');
150-
rmdir($basePath.'2');
151-
rmdir($basePath.'3');
152170
}
153171

154172
public function testMkdirCreatesDirectoriesFromTraversableObject()
155173
{
156-
$basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time();
174+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
157175
$directories = new \ArrayObject(array(
158176
$basePath.'1', $basePath.'2', $basePath.'3'
159177
));
160178

161-
$filesystem = new Filesystem();
162-
$result = $filesystem->mkdir($directories);
179+
$result = $this->filesystem->mkdir($directories);
163180

164181
$this->assertTrue($result);
165182
$this->assertTrue(is_dir($basePath.'1'));
166183
$this->assertTrue(is_dir($basePath.'2'));
167184
$this->assertTrue(is_dir($basePath.'3'));
168-
169-
rmdir($basePath.'1');
170-
rmdir($basePath.'2');
171-
rmdir($basePath.'3');
172185
}
173186

174187
public function testMkdirCreatesDirectoriesEvenIfItFailsToCreateOneOfThem()
175188
{
176-
$basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time();
189+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
177190
$directories = array(
178191
$basePath.'1', $basePath.'2', $basePath.'3'
179192
);
180193

181194
// create a file to make that directory cannot be created
182195
file_put_contents($basePath.'2', '');
183196

184-
$filesystem = new Filesystem();
185-
$result = $filesystem->mkdir($directories);
197+
$result = $this->filesystem->mkdir($directories);
186198

187199
$this->assertFalse($result);
188200
$this->assertTrue(is_dir($basePath.'1'));
189201
$this->assertFalse(is_dir($basePath.'2'));
190202
$this->assertTrue(is_dir($basePath.'3'));
191-
192-
rmdir($basePath.'1');
193-
unlink($basePath.'2');
194-
rmdir($basePath.'3');
195203
}
196204

197205
public function testTouchCreatesEmptyFile()
198206
{
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';
204208

205-
$this->assertFileExists($basePath.'1');
209+
$this->filesystem->touch($file);
206210

207-
unlink($basePath.'1');
211+
$this->assertFileExists($file);
208212
}
209213

210214
public function testTouchCreatesEmptyFilesFromArray()
211215
{
212-
$basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time();
216+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
213217
$files = array(
214218
$basePath.'1', $basePath.'2', $basePath.'3'
215219
);
216-
mkdir($basePath);
217220

218-
$filesystem = new Filesystem();
219-
$filesystem->touch($files);
221+
$this->filesystem->touch($files);
220222

221223
$this->assertFileExists($basePath.'1');
222224
$this->assertFileExists($basePath.'2');
223225
$this->assertFileExists($basePath.'3');
224-
225-
unlink($basePath.'1');
226-
unlink($basePath.'2');
227-
unlink($basePath.'3');
228-
rmdir($basePath);
229226
}
230227

231228
public function testTouchCreatesEmptyFilesFromTraversableObject()
232229
{
233-
$basePath = sys_get_temp_dir().DIRECTORY_SEPARATOR.time();
230+
$basePath = $this->workspace.DIRECTORY_SEPARATOR;
234231
$files = new \ArrayObject(array(
235232
$basePath.'1', $basePath.'2', $basePath.'3'
236233
));
237-
mkdir($basePath);
238234

239-
$filesystem = new Filesystem();
240-
$filesystem->touch($files);
235+
$this->filesystem->touch($files);
241236

242237
$this->assertFileExists($basePath.'1');
243238
$this->assertFileExists($basePath.'2');
244239
$this->assertFileExists($basePath.'3');
245-
246-
unlink($basePath.'1');
247-
unlink($basePath.'2');
248-
unlink($basePath.'3');
249-
rmdir($basePath);
250240
}
251241
}

0 commit comments

Comments
 (0)