1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gaufrette\Functional\Adapter; |
4
|
|
|
|
5
|
|
|
use Gaufrette\Filesystem; |
6
|
|
|
use Gaufrette\Adapter\Local\Local; |
7
|
|
|
|
8
|
|
|
class LocalTest extends FunctionalTestCase |
9
|
|
|
{ |
10
|
|
|
private $directory; |
11
|
|
|
|
12
|
|
|
public function setUp() |
13
|
|
|
{ |
14
|
|
|
$this->directory = sprintf('%s/filesystem', str_replace('\\', '/', __DIR__)); |
15
|
|
|
|
16
|
|
|
if (!file_exists($this->directory)) { |
17
|
|
|
mkdir($this->directory); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
// var_dump($this->directory); |
|
|
|
|
21
|
|
|
$this->filesystem = new Filesystem(new Local($this->directory)); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function tearDown() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->filesystem = null; |
27
|
|
|
|
28
|
|
|
if (file_exists($this->directory)) { |
29
|
|
|
$iterator = new \RecursiveIteratorIterator( |
30
|
|
|
new \RecursiveDirectoryIterator( |
31
|
|
|
$this->directory, |
32
|
|
|
\FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS |
33
|
|
|
), |
34
|
|
|
\RecursiveIteratorIterator::CHILD_FIRST |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
foreach ($iterator as $item) { |
38
|
|
|
if ($item->isDir()) { |
39
|
|
|
rmdir(strval($item)); |
40
|
|
|
} else { |
41
|
|
|
unlink(strval($item)); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @test |
49
|
|
|
* @group functional |
50
|
|
|
*/ |
51
|
|
|
public function shouldWorkWithSyslink() |
52
|
|
|
{ |
53
|
|
|
$dirname = sprintf( |
54
|
|
|
'%s/adapters/aaa', |
55
|
|
|
__DIR__ |
56
|
|
|
); |
57
|
|
|
$linkname = sprintf( |
58
|
|
|
'%s/adapters/../../../../link', |
59
|
|
|
__DIR__ |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
@mkdir($dirname); |
|
|
|
|
63
|
|
|
@unlink($linkname); |
|
|
|
|
64
|
|
|
symlink($dirname, $linkname); |
65
|
|
|
|
66
|
|
|
$this->filesystem = new Filesystem(new Local($linkname)); |
67
|
|
|
$this->filesystem->write('test.txt', 'abc 123'); |
68
|
|
|
|
69
|
|
|
$this->assertSame('abc 123', $this->filesystem->read('test.txt')); |
70
|
|
|
$this->filesystem->delete('test.txt'); |
71
|
|
|
@unlink($linkname); |
|
|
|
|
72
|
|
|
@rmdir($dirname); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
* @covers Gaufrette\Adapter\Local |
78
|
|
|
* @group functional |
79
|
|
|
*/ |
80
|
|
|
public function shouldListingOnlyGivenDirectory() |
81
|
|
|
{ |
82
|
|
|
$dirname = sprintf( |
83
|
|
|
'%s/localDir', |
84
|
|
|
$this->directory |
85
|
|
|
); |
86
|
|
|
@mkdir($dirname); |
|
|
|
|
87
|
|
|
|
88
|
|
|
$this->filesystem = new Filesystem(new Local($this->directory)); |
89
|
|
|
$this->filesystem->write('aaa.txt', 'some content'); |
90
|
|
|
$this->filesystem->write('localDir/test.txt', 'some content'); |
91
|
|
|
|
92
|
|
|
$dirs = $this->filesystem->listKeys('localDir/test'); |
93
|
|
|
$this->assertEmpty($dirs['dirs']); |
94
|
|
|
$this->assertCount(1, $dirs['keys']); |
95
|
|
|
$this->assertEquals('localDir/test.txt', $dirs['keys'][0]); |
96
|
|
|
|
97
|
|
|
$dirs = $this->filesystem->listKeys(); |
98
|
|
|
|
99
|
|
|
$this->assertCount(1, $dirs['dirs']); |
100
|
|
|
$this->assertEquals('localDir', $dirs['dirs'][0]); |
101
|
|
|
$this->assertCount(2, $dirs['keys']); |
102
|
|
|
$this->assertEquals('aaa.txt', $dirs['keys'][0]); |
103
|
|
|
$this->assertEquals('localDir/test.txt', $dirs['keys'][1]); |
104
|
|
|
|
105
|
|
|
@unlink($dirname.DIRECTORY_SEPARATOR.'test.txt'); |
|
|
|
|
106
|
|
|
@unlink($this->directory.DIRECTORY_SEPARATOR.'aaa.txt'); |
|
|
|
|
107
|
|
|
@rmdir($dirname); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @test |
112
|
|
|
* @covers Gaufrette\Adapter\Local |
113
|
|
|
* @group functional |
114
|
|
|
*/ |
115
|
|
|
public function shouldListingAllKeys() |
116
|
|
|
{ |
117
|
|
|
$dirname = sprintf( |
118
|
|
|
'%s/localDir', |
119
|
|
|
$this->directory |
120
|
|
|
); |
121
|
|
|
@mkdir($dirname); |
|
|
|
|
122
|
|
|
|
123
|
|
|
$this->filesystem = new Filesystem(new Local($this->directory)); |
124
|
|
|
$this->filesystem->write('aaa.txt', 'some content'); |
125
|
|
|
$this->filesystem->write('localDir/dir1/dir2/dir3/test.txt', 'some content'); |
126
|
|
|
|
127
|
|
|
$keys = $this->filesystem->keys(); |
128
|
|
|
$dirs = $this->filesystem->listKeys(); |
129
|
|
|
$this->assertCount(6, $keys); |
130
|
|
|
$this->assertCount(4, $dirs['dirs']); |
131
|
|
|
$this->assertEquals('localDir/dir1/dir2/dir3/test.txt', $dirs['keys'][1]); |
132
|
|
|
|
133
|
|
|
foreach ($dirs['keys'] as $item) { |
134
|
|
|
@unlink($item); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
$reversed = array_reverse($dirs['dirs']); |
137
|
|
|
foreach ($reversed as $item) { |
138
|
|
|
@rmdir($item); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @test |
144
|
|
|
* @group functional |
145
|
|
|
*/ |
146
|
|
|
public function shouldBeAbleToClearCache() |
147
|
|
|
{ |
148
|
|
|
$dirname = sprintf( |
149
|
|
|
'%s/adapters/bbb', |
150
|
|
|
__DIR__ |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
@mkdir($dirname); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$this->filesystem = new Filesystem(new Local($dirname)); |
156
|
|
|
|
157
|
|
|
$this->filesystem->get('test.txt', true); |
158
|
|
|
$this->filesystem->write('test.txt', '123', true); |
159
|
|
|
|
160
|
|
|
$this->filesystem->get('test2.txt', true); |
161
|
|
|
$this->filesystem->write('test2.txt', '123', true); |
162
|
|
|
|
163
|
|
|
$fsReflection = new \ReflectionClass($this->filesystem); |
164
|
|
|
|
165
|
|
|
$fsIsFileInRegister = $fsReflection->getMethod('isFileInRegister'); |
166
|
|
|
$fsIsFileInRegister->setAccessible(true); |
167
|
|
|
|
168
|
|
|
$this->assertTrue($fsIsFileInRegister->invoke($this->filesystem, 'test.txt')); |
169
|
|
|
$this->filesystem->removeFromRegister('test.txt'); |
170
|
|
|
$this->assertFalse($fsIsFileInRegister->invoke($this->filesystem, 'test.txt')); |
171
|
|
|
|
172
|
|
|
$this->filesystem->clearFileRegister(); |
173
|
|
|
$fsRegister = $fsReflection->getProperty('fileRegister'); |
174
|
|
|
$fsRegister->setAccessible(true); |
175
|
|
|
$this->assertEquals(0, count($fsRegister->getValue($this->filesystem))); |
176
|
|
|
|
177
|
|
|
$this->filesystem->delete('test.txt'); |
178
|
|
|
$this->filesystem->delete('test2.txt'); |
179
|
|
|
@rmdir($dirname); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.