Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/file.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * FileTest file | |
4 * | |
5 * PHP versions 4 and 5 | |
6 * | |
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing> | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs | |
17 * @since CakePHP(tm) v 1.2.0.4206 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Core', 'File'); | |
21 | |
22 /** | |
23 * FileTest class | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.libs | |
27 */ | |
28 class FileTest extends CakeTestCase { | |
29 | |
30 /** | |
31 * File property | |
32 * | |
33 * @var mixed null | |
34 * @access public | |
35 */ | |
36 var $File = null; | |
37 | |
38 /** | |
39 * testBasic method | |
40 * | |
41 * @access public | |
42 * @return void | |
43 */ | |
44 function testBasic() { | |
45 $file = __FILE__; | |
46 $this->File =& new File($file); | |
47 | |
48 $result = $this->File->pwd(); | |
49 $expecting = $file; | |
50 $this->assertEqual($result, $expecting); | |
51 | |
52 $result = $this->File->name; | |
53 $expecting = basename(__FILE__); | |
54 $this->assertEqual($result, $expecting); | |
55 | |
56 $result = $this->File->info(); | |
57 $expecting = array( | |
58 'dirname' => dirname(__FILE__), 'basename' => basename(__FILE__), | |
59 'extension' => 'php', 'filename' =>'file.test' | |
60 ); | |
61 $this->assertEqual($result, $expecting); | |
62 | |
63 $result = $this->File->ext(); | |
64 $expecting = 'php'; | |
65 $this->assertEqual($result, $expecting); | |
66 | |
67 $result = $this->File->name(); | |
68 $expecting = 'file.test'; | |
69 $this->assertEqual($result, $expecting); | |
70 | |
71 $result = $this->File->md5(); | |
72 $expecting = md5_file($file); | |
73 $this->assertEqual($result, $expecting); | |
74 | |
75 $result = $this->File->md5(true); | |
76 $expecting = md5_file($file); | |
77 $this->assertEqual($result, $expecting); | |
78 | |
79 $result = $this->File->size(); | |
80 $expecting = filesize($file); | |
81 $this->assertEqual($result, $expecting); | |
82 | |
83 $result = $this->File->owner(); | |
84 $expecting = fileowner($file); | |
85 $this->assertEqual($result, $expecting); | |
86 | |
87 $result = $this->File->group(); | |
88 $expecting = filegroup($file); | |
89 $this->assertEqual($result, $expecting); | |
90 | |
91 $result = $this->File->Folder(); | |
92 $this->assertIsA($result, 'Folder'); | |
93 | |
94 $this->skipIf(DIRECTORY_SEPARATOR === '\\', '%s File permissions tests not supported on Windows'); | |
95 $result = $this->File->perms(); | |
96 $expecting = '0644'; | |
97 $this->assertEqual($result, $expecting); | |
98 } | |
99 | |
100 /** | |
101 * testRead method | |
102 * | |
103 * @access public | |
104 * @return void | |
105 */ | |
106 function testRead() { | |
107 $file = __FILE__; | |
108 $this->File =& new File($file); | |
109 | |
110 $result = $this->File->read(); | |
111 $expecting = file_get_contents(__FILE__); | |
112 $this->assertEqual($result, $expecting); | |
113 $this->assertTrue(!is_resource($this->File->handle)); | |
114 | |
115 $this->File->lock = true; | |
116 $result = $this->File->read(); | |
117 $expecting = file_get_contents(__FILE__); | |
118 $this->assertEqual($result, trim($expecting)); | |
119 $this->File->lock = null; | |
120 | |
121 $data = $expecting; | |
122 $expecting = substr($data, 0, 3); | |
123 $result = $this->File->read(3); | |
124 $this->assertEqual($result, $expecting); | |
125 $this->assertTrue(is_resource($this->File->handle)); | |
126 | |
127 $expecting = substr($data, 3, 3); | |
128 $result = $this->File->read(3); | |
129 $this->assertEqual($result, $expecting); | |
130 } | |
131 | |
132 /** | |
133 * testOffset method | |
134 * | |
135 * @access public | |
136 * @return void | |
137 */ | |
138 function testOffset() { | |
139 $this->File->close(); | |
140 | |
141 $result = $this->File->offset(); | |
142 $this->assertFalse($result); | |
143 | |
144 $this->assertFalse(is_resource($this->File->handle)); | |
145 $success = $this->File->offset(0); | |
146 $this->assertTrue($success); | |
147 $this->assertTrue(is_resource($this->File->handle)); | |
148 | |
149 $result = $this->File->offset(); | |
150 $expecting = 0; | |
151 $this->assertIdentical($result, $expecting); | |
152 | |
153 $data = file_get_contents(__FILE__); | |
154 $success = $this->File->offset(5); | |
155 $expecting = substr($data, 5, 3); | |
156 $result = $this->File->read(3); | |
157 $this->assertTrue($success); | |
158 $this->assertEqual($result, $expecting); | |
159 | |
160 $result = $this->File->offset(); | |
161 $expecting = 5+3; | |
162 $this->assertIdentical($result, $expecting); | |
163 } | |
164 | |
165 /** | |
166 * testOpen method | |
167 * | |
168 * @access public | |
169 * @return void | |
170 */ | |
171 function testOpen() { | |
172 $this->File->handle = null; | |
173 | |
174 $r = $this->File->open(); | |
175 $this->assertTrue(is_resource($this->File->handle)); | |
176 $this->assertTrue($r); | |
177 | |
178 $handle = $this->File->handle; | |
179 $r = $this->File->open(); | |
180 $this->assertTrue($r); | |
181 $this->assertTrue($handle === $this->File->handle); | |
182 $this->assertTrue(is_resource($this->File->handle)); | |
183 | |
184 $r = $this->File->open('r', true); | |
185 $this->assertTrue($r); | |
186 $this->assertFalse($handle === $this->File->handle); | |
187 $this->assertTrue(is_resource($this->File->handle)); | |
188 } | |
189 | |
190 /** | |
191 * testClose method | |
192 * | |
193 * @access public | |
194 * @return void | |
195 */ | |
196 function testClose() { | |
197 $this->File->handle = null; | |
198 $this->assertFalse(is_resource($this->File->handle)); | |
199 $this->assertTrue($this->File->close()); | |
200 $this->assertFalse(is_resource($this->File->handle)); | |
201 | |
202 $this->File->handle = fopen(__FILE__, 'r'); | |
203 $this->assertTrue(is_resource($this->File->handle)); | |
204 $this->assertTrue($this->File->close()); | |
205 $this->assertFalse(is_resource($this->File->handle)); | |
206 } | |
207 | |
208 /** | |
209 * testCreate method | |
210 * | |
211 * @access public | |
212 * @return void | |
213 */ | |
214 function testCreate() { | |
215 $tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp'; | |
216 $File =& new File($tmpFile, true, 0777); | |
217 $this->assertTrue($File->exists()); | |
218 } | |
219 | |
220 /** | |
221 * testOpeningNonExistantFileCreatesIt method | |
222 * | |
223 * @access public | |
224 * @return void | |
225 */ | |
226 function testOpeningNonExistantFileCreatesIt() { | |
227 $someFile =& new File(TMP . 'some_file.txt', false); | |
228 $this->assertTrue($someFile->open()); | |
229 $this->assertEqual($someFile->read(), ''); | |
230 $someFile->close(); | |
231 $someFile->delete(); | |
232 } | |
233 | |
234 /** | |
235 * testPrepare method | |
236 * | |
237 * @access public | |
238 * @return void | |
239 */ | |
240 function testPrepare() { | |
241 $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere"; | |
242 if (DS == '\\') { | |
243 $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; | |
244 $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; | |
245 } else { | |
246 $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere"; | |
247 } | |
248 $this->assertIdentical(File::prepare($string), $expected); | |
249 | |
250 $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n"; | |
251 $expected .= "for\r\n\r\n\r\n\r\n\r\nhere"; | |
252 $this->assertIdentical(File::prepare($string, true), $expected); | |
253 } | |
254 | |
255 /** | |
256 * testReadable method | |
257 * | |
258 * @access public | |
259 * @return void | |
260 */ | |
261 function testReadable() { | |
262 $someFile =& new File(TMP . 'some_file.txt', false); | |
263 $this->assertTrue($someFile->open()); | |
264 $this->assertTrue($someFile->readable()); | |
265 $someFile->close(); | |
266 $someFile->delete(); | |
267 } | |
268 | |
269 /** | |
270 * testWritable method | |
271 * | |
272 * @access public | |
273 * @return void | |
274 */ | |
275 function testWritable() { | |
276 $someFile =& new File(TMP . 'some_file.txt', false); | |
277 $this->assertTrue($someFile->open()); | |
278 $this->assertTrue($someFile->writable()); | |
279 $someFile->close(); | |
280 $someFile->delete(); | |
281 } | |
282 | |
283 /** | |
284 * testExecutable method | |
285 * | |
286 * @access public | |
287 * @return void | |
288 */ | |
289 function testExecutable() { | |
290 $someFile =& new File(TMP . 'some_file.txt', false); | |
291 $this->assertTrue($someFile->open()); | |
292 $this->assertFalse($someFile->executable()); | |
293 $someFile->close(); | |
294 $someFile->delete(); | |
295 } | |
296 | |
297 /** | |
298 * testLastAccess method | |
299 * | |
300 * @access public | |
301 * @return void | |
302 */ | |
303 function testLastAccess() { | |
304 $someFile =& new File(TMP . 'some_file.txt', false); | |
305 $this->assertFalse($someFile->lastAccess()); | |
306 $this->assertTrue($someFile->open()); | |
307 $this->assertEqual($someFile->lastAccess(), time()); | |
308 $someFile->close(); | |
309 $someFile->delete(); | |
310 } | |
311 | |
312 /** | |
313 * testLastChange method | |
314 * | |
315 * @access public | |
316 * @return void | |
317 */ | |
318 function testLastChange() { | |
319 $someFile =& new File(TMP . 'some_file.txt', false); | |
320 $this->assertFalse($someFile->lastChange()); | |
321 $this->assertTrue($someFile->open('r+')); | |
322 $this->assertEqual($someFile->lastChange(), time()); | |
323 $someFile->write('something'); | |
324 $this->assertEqual($someFile->lastChange(), time()); | |
325 $someFile->close(); | |
326 $someFile->delete(); | |
327 } | |
328 | |
329 /** | |
330 * testWrite method | |
331 * | |
332 * @access public | |
333 * @return void | |
334 */ | |
335 function testWrite() { | |
336 if (!$tmpFile = $this->_getTmpFile()) { | |
337 return false; | |
338 }; | |
339 if (file_exists($tmpFile)) { | |
340 unlink($tmpFile); | |
341 } | |
342 | |
343 $TmpFile =& new File($tmpFile); | |
344 $this->assertFalse(file_exists($tmpFile)); | |
345 $this->assertFalse(is_resource($TmpFile->handle)); | |
346 | |
347 $testData = array('CakePHP\'s', ' test suite', ' was here ...', ''); | |
348 foreach ($testData as $data) { | |
349 $r = $TmpFile->write($data); | |
350 $this->assertTrue($r); | |
351 $this->assertTrue(file_exists($tmpFile)); | |
352 $this->assertEqual($data, file_get_contents($tmpFile)); | |
353 $this->assertTrue(is_resource($TmpFile->handle)); | |
354 $TmpFile->close(); | |
355 | |
356 } | |
357 unlink($tmpFile); | |
358 } | |
359 | |
360 /** | |
361 * testAppend method | |
362 * | |
363 * @access public | |
364 * @return void | |
365 */ | |
366 function testAppend() { | |
367 if (!$tmpFile = $this->_getTmpFile()) { | |
368 return false; | |
369 }; | |
370 if (file_exists($tmpFile)) { | |
371 unlink($tmpFile); | |
372 } | |
373 | |
374 $TmpFile =& new File($tmpFile); | |
375 $this->assertFalse(file_exists($tmpFile)); | |
376 | |
377 $fragments = array('CakePHP\'s', ' test suite', ' was here ...', ''); | |
378 $data = null; | |
379 foreach ($fragments as $fragment) { | |
380 $r = $TmpFile->append($fragment); | |
381 $this->assertTrue($r); | |
382 $this->assertTrue(file_exists($tmpFile)); | |
383 $data = $data.$fragment; | |
384 $this->assertEqual($data, file_get_contents($tmpFile)); | |
385 $TmpFile->close(); | |
386 } | |
387 } | |
388 | |
389 /** | |
390 * testDelete method | |
391 * | |
392 * @access public | |
393 * @return void | |
394 */ | |
395 function testDelete() { | |
396 if (!$tmpFile = $this->_getTmpFile()) { | |
397 return false; | |
398 }; | |
399 | |
400 if (!file_exists($tmpFile)) { | |
401 touch($tmpFile); | |
402 } | |
403 $TmpFile =& new File($tmpFile); | |
404 $this->assertTrue(file_exists($tmpFile)); | |
405 $result = $TmpFile->delete(); | |
406 $this->assertTrue($result); | |
407 $this->assertFalse(file_exists($tmpFile)); | |
408 | |
409 $TmpFile =& new File('/this/does/not/exist'); | |
410 $result = $TmpFile->delete(); | |
411 $this->assertFalse($result); | |
412 } | |
413 | |
414 /** | |
415 * testCopy method | |
416 * | |
417 * @access public | |
418 * @return void | |
419 */ | |
420 function testCopy() { | |
421 $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp'; | |
422 $file = __FILE__; | |
423 $this->File =& new File($file); | |
424 $result = $this->File->copy($dest); | |
425 $this->assertTrue($result); | |
426 | |
427 $result = $this->File->copy($dest, true); | |
428 $this->assertTrue($result); | |
429 | |
430 $result = $this->File->copy($dest, false); | |
431 $this->assertFalse($result); | |
432 | |
433 $this->File->close(); | |
434 unlink($dest); | |
435 | |
436 $TmpFile =& new File('/this/does/not/exist'); | |
437 $result = $TmpFile->copy($dest); | |
438 $this->assertFalse($result); | |
439 | |
440 $TmpFile->close(); | |
441 } | |
442 | |
443 /** | |
444 * getTmpFile method | |
445 * | |
446 * @param bool $paintSkip | |
447 * @access protected | |
448 * @return void | |
449 */ | |
450 function _getTmpFile($paintSkip = true) { | |
451 $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp'; | |
452 if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) { | |
453 return $tmpFile; | |
454 }; | |
455 | |
456 if ($paintSkip) { | |
457 $caller = 'test'; | |
458 if (function_exists('debug_backtrace')) { | |
459 $trace = debug_backtrace(); | |
460 $caller = $trace[1]['function'] . '()'; | |
461 } | |
462 $assertLine = new SimpleStackTrace(array(__FUNCTION__)); | |
463 $assertLine = $assertLine->traceMethod(); | |
464 $shortPath = substr($tmpFile, strlen(ROOT)); | |
465 | |
466 $message = '[FileTest] Skipping %s because "%s" not writeable!'; | |
467 $message = sprintf(__($message, true), $caller, $shortPath) . $assertLine; | |
468 $this->_reporter->paintSkip($message); | |
469 } | |
470 return false; | |
471 } | |
472 } |