|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KochTest\Request; |
|
4
|
|
|
|
|
5
|
|
|
// use Koch\Request\FTP; |
|
6
|
|
|
|
|
7
|
|
|
class FTPTest extends \PHPUnit_Framework_TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var FTP |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $object; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Test config data. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config = [ |
|
20
|
|
|
'server' => 'localhost', |
|
21
|
|
|
'username' => 'jens', |
|
22
|
|
|
'password' => 'pw123', /*, |
|
|
|
|
|
|
23
|
|
|
'port' => 21, |
|
24
|
|
|
'passive' => false*/ |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
29
|
|
|
* This method is called before a test is executed. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
//$this->object = new FTP($this->config['server'], $this->config['username'], $this->config['password']); |
|
|
|
|
|
|
34
|
|
|
$this->generator = new \PHPUnit_Framework_MockObject_Generator(); |
|
|
|
|
|
|
35
|
|
|
$mock = $this->generator->getMock( |
|
|
|
|
|
|
36
|
|
|
// classname |
|
37
|
|
|
'FTPMock', |
|
38
|
|
|
// methods |
|
39
|
|
|
[ |
|
40
|
|
|
'upload', |
|
41
|
|
|
'download', |
|
42
|
|
|
'deleteFile', |
|
43
|
|
|
'renameOrMove', |
|
44
|
|
|
'createDirectory', |
|
45
|
|
|
'isDir', |
|
46
|
|
|
'isFile', |
|
47
|
|
|
'deleteDirectory', |
|
48
|
|
|
'setPermissions', |
|
49
|
|
|
'fileSize', |
|
50
|
|
|
'getDirectoryContent', |
|
51
|
|
|
], |
|
52
|
|
|
// constructor args |
|
53
|
|
|
[], |
|
54
|
|
|
// classname |
|
55
|
|
|
'', |
|
56
|
|
|
// invoke (parent)constructor |
|
|
|
|
|
|
57
|
|
|
false |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->object = $mock; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Tears down the fixture, for example, closes a network connection. |
|
65
|
|
|
* This method is called after a test is executed. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function tearDown() |
|
68
|
|
|
{ |
|
69
|
|
|
unset($this->object); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @covers Koch\Request\FTP::upload |
|
74
|
|
|
*/ |
|
75
|
|
|
public function testUpload() |
|
76
|
|
|
{ |
|
77
|
|
|
// setup method mock |
|
78
|
|
|
$this->object->expects($this->any())->method('upload')->will($this->returnValue(true)); |
|
79
|
|
|
|
|
80
|
|
|
$sourceFile = 'file.txt'; |
|
81
|
|
|
$destinationFile = 'file.txt'; |
|
82
|
|
|
$transferMode = '1'; // FTP_ASCII = 1, FTP_BINARY = 2 |
|
83
|
|
|
|
|
84
|
|
|
$result = $this->object->upload($sourceFile, $destinationFile, $transferMode); |
|
85
|
|
|
//var_dump($this->object->errors); |
|
|
|
|
|
|
86
|
|
|
$this->assertTrue($result); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @covers Koch\Request\FTP::download |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function testDownload() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
// setup method mock |
|
95
|
|
|
$this->object->expects($this->any())->method('download')->will($this->returnValue(true)); |
|
96
|
|
|
|
|
97
|
|
|
$this->object->upload('testfile.txt', 'dir1/testfile.txt'); |
|
98
|
|
|
|
|
99
|
|
|
$result = $this->object->download('dir1/testfile.txt', 'testfile_downloaded.txt'); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertTrue($result); |
|
102
|
|
|
//$this->assertTrue(is_file('testfile_downloaded.txt')); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @covers Koch\Request\FTP::deleteFile |
|
107
|
|
|
*/ |
|
108
|
|
|
public function testDeleteFile() |
|
109
|
|
|
{ |
|
110
|
|
|
// setup method mock |
|
111
|
|
|
$this->object->expects($this->any())->method('deleteFile')->will($this->returnValue(true)); |
|
112
|
|
|
$this->object->expects($this->any())->method('isFile')->will($this->returnValue(false)); |
|
113
|
|
|
|
|
114
|
|
|
$this->object->upload('testfile.txt', 'dir1/testfile.txt'); |
|
115
|
|
|
|
|
116
|
|
|
$result = $this->object->deleteFile('dir1/testfile.txt'); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertTrue($result); |
|
119
|
|
|
$this->assertFalse($this->object->isFile('dir1/testfile.txt')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @covers Koch\Request\FTP::renameOrMove |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function testRenameOrMove() |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
// setup method mock |
|
128
|
|
|
$this->object->expects($this->any())->method('renameOrMove')->will($this->returnValue(true)); |
|
129
|
|
|
|
|
130
|
|
|
$this->object->upload('testfile.txt', 'testfile.txt', 1); |
|
131
|
|
|
|
|
132
|
|
|
$result = $this->object->renameOrMove('testfile.txt', 'testfile_renamed.txt'); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertTrue($result); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @covers Koch\Request\FTP::createDirectory |
|
139
|
|
|
*/ |
|
140
|
|
View Code Duplication |
public function testCreateDirectory() |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
$this->object->expects($this->any())->method('createDirectory')->will($this->returnValue(true)); |
|
143
|
|
|
$this->object->expects($this->any())->method('isDir')->will($this->returnValue(true)); |
|
144
|
|
|
|
|
145
|
|
|
$this->object->createDirectory('dir1/dir2/dir3'); |
|
146
|
|
|
|
|
147
|
|
|
$this->assertTrue($this->object->isDir('dir1/dir2/dir3')); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @covers Koch\Request\FTP::deleteDirectory |
|
152
|
|
|
* |
|
153
|
|
|
* @todo Implement testDeleteDirectory(). |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testDeleteDirectory() |
|
156
|
|
|
{ |
|
157
|
|
|
$this->object->expects($this->any())->method('deleteDirectory')->will($this->returnValue(true)); |
|
158
|
|
|
$this->object->expects($this->any())->method('isDir')->will($this->returnValue(false)); |
|
159
|
|
|
|
|
160
|
|
|
$this->object->createDirectory('dir1/dir2/dir3'); |
|
161
|
|
|
|
|
162
|
|
|
$result = $this->object->deleteDirectory('dir1/dir2'); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertTrue($result); |
|
165
|
|
|
$this->assertFalse($this->object->isDir('dir1/dir2/dir3')); |
|
166
|
|
|
$this->assertFalse($this->object->isDir('dir1/dir2')); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return array |
|
|
|
|
|
|
171
|
|
|
*/ |
|
172
|
|
|
public function setPermissionsDataprovider() |
|
173
|
|
|
{ |
|
174
|
|
|
return [ |
|
175
|
|
|
['111', '111'], |
|
176
|
|
|
['110', '110'], |
|
177
|
|
|
['444', '555'], |
|
178
|
|
|
['412', '512'], |
|
179
|
|
|
['641', '751'], |
|
180
|
|
|
['666', '777'], |
|
181
|
|
|
['400', '500'], |
|
182
|
|
|
['040', '050'], |
|
183
|
|
|
['004', '005'], |
|
184
|
|
|
]; |
|
185
|
|
|
} |
|
186
|
|
|
/** |
|
187
|
|
|
* @dataProvider setPermissionsDataprovider |
|
188
|
|
|
* @covers Koch\Request\FTP::setPermissions |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testSetPermissions($set, $expect) |
|
191
|
|
|
{ |
|
192
|
|
|
// setup method mock |
|
193
|
|
|
$this->object->expects($this->any()) |
|
194
|
|
|
->method('setPermissions') |
|
195
|
|
|
->with('testfile.txt', $this->stringContains($set)) |
|
196
|
|
|
->will($this->returnValue($expect)); |
|
197
|
|
|
|
|
198
|
|
|
$this->object->upload('testfile.txt', 'testfile.txt'); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertEquals($this->object->setPermissions('testfile.txt', $set), $expect); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @covers Koch\Request\FTP::isFile |
|
205
|
|
|
*/ |
|
206
|
|
View Code Duplication |
public function testIsFile() |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
$this->object->expects($this->once())->method('upload')->will($this->returnValue(true)); |
|
209
|
|
|
$this->object->expects($this->any())->method('isFile')->will($this->returnValue(true)); |
|
210
|
|
|
|
|
211
|
|
|
$result = $this->object->upload('testfile.txt', 'dir1/testfile.txt'); |
|
212
|
|
|
|
|
213
|
|
|
$this->assertTrue($result); |
|
214
|
|
|
$this->assertTrue($this->object->isFile('dir1/testfile.txt')); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @covers Koch\Request\FTP::fileSize |
|
219
|
|
|
*/ |
|
220
|
|
View Code Duplication |
public function testFileSize() |
|
|
|
|
|
|
221
|
|
|
{ |
|
222
|
|
|
$this->object->expects($this->any())->method('fileSize')->will($this->returnValue(123)); |
|
223
|
|
|
|
|
224
|
|
|
$this->object->upload('testfile.txt', 'testfile.txt', 2); |
|
225
|
|
|
$filesize = $this->object->fileSize('testfile.txt'); |
|
226
|
|
|
|
|
227
|
|
|
$this->assertEquals($filesize, 123); /*filesize('testfile.txt')*/ |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @covers Koch\Request\FTP::isDir |
|
232
|
|
|
* |
|
233
|
|
|
* @todo Implement testIsDir(). |
|
234
|
|
|
*/ |
|
235
|
|
View Code Duplication |
public function testIsDir() |
|
|
|
|
|
|
236
|
|
|
{ |
|
237
|
|
|
$this->object->expects($this->any())->method('createDirectory')->will($this->returnValue(true)); |
|
238
|
|
|
$this->object->expects($this->any())->method('isDir')->will($this->returnValue(true)); |
|
239
|
|
|
|
|
240
|
|
|
$result = $this->object->createDirectory('dir1'); |
|
241
|
|
|
|
|
242
|
|
|
$this->assertTrue($result); |
|
243
|
|
|
$this->assertTrue($this->object->isDir('dir1')); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @covers Koch\Request\FTP::getDirectoryContent |
|
248
|
|
|
*/ |
|
249
|
|
|
public function testGetDirectoryContent() |
|
250
|
|
|
{ |
|
251
|
|
|
$this->object->expects($this->any())->method('getDirectoryContent')->will($this->returnValue(true)); |
|
252
|
|
|
|
|
253
|
|
|
$result = $this->object->getDirectoryContent('dir1/dir2'); |
|
254
|
|
|
$this->assertTrue($result); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
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.