2007-05-03 23:25:21 +00:00
|
|
|
<?php
|
|
|
|
/* SVN FILE: $Id$ */
|
|
|
|
/**
|
|
|
|
* Short description for file.
|
|
|
|
*
|
|
|
|
* Long description for file
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
2007-07-09 17:02:55 +00:00
|
|
|
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
|
2008-01-01 22:18:17 +00:00
|
|
|
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-07-09 17:02:55 +00:00
|
|
|
* 1785 E. Sahara Avenue, Suite 490-204
|
|
|
|
* Las Vegas, Nevada 89104
|
2007-05-03 23:25:21 +00:00
|
|
|
*
|
|
|
|
* Licensed under The Open Group Test Suite License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @filesource
|
2008-01-01 22:18:17 +00:00
|
|
|
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
|
2007-07-09 17:02:55 +00:00
|
|
|
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
|
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs
|
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
|
|
|
* @version $Revision$
|
|
|
|
* @modifiedby $LastChangedBy$
|
|
|
|
* @lastmodified $Date$
|
|
|
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
2007-05-03 23:25:21 +00:00
|
|
|
*/
|
2007-07-08 01:17:57 +00:00
|
|
|
uses('file');
|
2007-10-09 18:34:41 +00:00
|
|
|
|
2007-05-03 23:25:21 +00:00
|
|
|
/**
|
|
|
|
* Short description for class.
|
|
|
|
*
|
2007-07-09 17:02:55 +00:00
|
|
|
* @package cake.tests
|
|
|
|
* @subpackage cake.tests.cases.libs
|
2007-05-03 23:25:21 +00:00
|
|
|
*/
|
|
|
|
class FileTest extends UnitTestCase {
|
2007-05-27 00:30:43 +00:00
|
|
|
|
2007-07-08 01:17:57 +00:00
|
|
|
var $File = null;
|
2007-05-27 00:30:43 +00:00
|
|
|
|
|
|
|
function testBasic() {
|
2007-10-09 00:00:46 +00:00
|
|
|
$file = __FILE__;
|
2007-05-27 00:30:43 +00:00
|
|
|
$this->File =& new File($file);
|
|
|
|
|
|
|
|
$result = $this->File->pwd();
|
|
|
|
$expecting = $file;
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$result = $this->File->name;
|
|
|
|
$expecting = basename(__FILE__);
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-05-29 23:38:36 +00:00
|
|
|
|
2007-05-27 16:06:49 +00:00
|
|
|
$result = $this->File->info();
|
2007-05-29 23:38:36 +00:00
|
|
|
$expecting = array('dirname'=> dirname(__FILE__), 'basename'=> basename(__FILE__), 'extension'=> 'php', 'filename'=>'file.test');
|
2007-05-27 16:06:49 +00:00
|
|
|
$this->assertEqual($result, $expecting);
|
2007-05-29 23:38:36 +00:00
|
|
|
|
2007-05-27 00:30:43 +00:00
|
|
|
$result = $this->File->ext();
|
|
|
|
$expecting = 'php';
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-05-29 23:38:36 +00:00
|
|
|
|
2007-08-21 03:39:02 +00:00
|
|
|
$result = $this->File->name();
|
2007-05-27 16:06:49 +00:00
|
|
|
$expecting = 'file.test';
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-05-27 00:30:43 +00:00
|
|
|
|
|
|
|
$result = $this->File->md5();
|
|
|
|
$expecting = md5_file($file);
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$result = $this->File->size();
|
|
|
|
$expecting = filesize($file);
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$result = $this->File->owner();
|
|
|
|
$expecting = fileowner($file);
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$result = $this->File->group();
|
|
|
|
$expecting = filegroup($file);
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
|
|
|
|
$result = $this->File->perms();
|
|
|
|
$expecting = '0644';
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-05-29 23:38:36 +00:00
|
|
|
|
2007-05-27 00:30:43 +00:00
|
|
|
$result = $this->File->Folder();
|
|
|
|
$this->assertIsA($result, 'Folder');
|
|
|
|
|
|
|
|
}
|
2007-10-09 00:00:46 +00:00
|
|
|
|
|
|
|
function testRead() {
|
|
|
|
$result = $this->File->read();
|
|
|
|
$expecting = file_get_contents(__FILE__);
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-10-11 07:19:06 +00:00
|
|
|
$this->assertTrue(!is_resource($this->File->handle));
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:19:06 +00:00
|
|
|
$data = $expecting;
|
|
|
|
$expecting = substr($data, 0, 3);
|
|
|
|
$result = $this->File->read(3);
|
|
|
|
$this->assertEqual($result, $expecting);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:19:06 +00:00
|
|
|
$expecting = substr($data, 3, 3);
|
|
|
|
$result = $this->File->read(3);
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-10-09 00:00:46 +00:00
|
|
|
}
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:49:57 +00:00
|
|
|
function testOffset() {
|
|
|
|
$this->File->close();
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:49:57 +00:00
|
|
|
$result = $this->File->offset();
|
|
|
|
$this->assertFalse($result);
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:49:57 +00:00
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
$success = $this->File->offset(0);
|
|
|
|
$this->assertTrue($success);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$result = $this->File->offset();
|
|
|
|
$expecting = 0;
|
|
|
|
$this->assertIdentical($result, $expecting);
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:49:57 +00:00
|
|
|
$data = file_get_contents(__FILE__);
|
|
|
|
$success = $this->File->offset(5);
|
|
|
|
$expecting = substr($data, 5, 3);
|
|
|
|
$result = $this->File->read(3);
|
|
|
|
$this->assertTrue($success);
|
|
|
|
$this->assertEqual($result, $expecting);
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:49:57 +00:00
|
|
|
$result = $this->File->offset();
|
|
|
|
$expecting = 5+3;
|
|
|
|
$this->assertIdentical($result, $expecting);
|
|
|
|
}
|
2007-10-09 00:00:46 +00:00
|
|
|
|
|
|
|
function testOpen() {
|
|
|
|
$this->File->handle = null;
|
|
|
|
|
|
|
|
$r = $this->File->open();
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($r);
|
|
|
|
|
|
|
|
$handle = $this->File->handle;
|
|
|
|
$r = $this->File->open();
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue($handle === $this->File->handle);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$r = $this->File->open('r', true);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertFalse($handle === $this->File->handle);
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 00:00:46 +00:00
|
|
|
$InvalidFile =& new File('invalid-file.invalid-ext');
|
|
|
|
$expecting =& new PatternExpectation('/could not open/i');
|
|
|
|
$this->expectError($expecting);
|
|
|
|
$InvalidFile->open();
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 00:00:46 +00:00
|
|
|
$this->File->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function testClose() {
|
|
|
|
$this->File->handle = null;
|
2007-10-16 09:48:59 +00:00
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
2007-10-09 00:00:46 +00:00
|
|
|
$this->assertTrue($this->File->close());
|
2007-10-16 09:48:59 +00:00
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
2007-10-09 00:00:46 +00:00
|
|
|
|
|
|
|
$this->File->handle = fopen(__FILE__, 'r');
|
2007-10-16 09:48:59 +00:00
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
2007-10-09 00:00:46 +00:00
|
|
|
$this->assertTrue($this->File->close());
|
2007-10-16 09:48:59 +00:00
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
2007-10-09 00:00:46 +00:00
|
|
|
}
|
|
|
|
|
2007-10-16 09:48:59 +00:00
|
|
|
function testCreate() {
|
|
|
|
$tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp';
|
|
|
|
$File =& new File($tmpFile, true, 0777);
|
|
|
|
$this->assertTrue($File->exists());
|
2007-10-09 00:00:46 +00:00
|
|
|
}
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
function testWrite() {
|
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
$TmpFile =& new File($tmpFile);
|
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
2007-10-11 07:19:06 +00:00
|
|
|
$this->assertFalse(is_resource($TmpFile->handle));
|
|
|
|
|
|
|
|
$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
|
2007-10-09 18:34:41 +00:00
|
|
|
foreach ($testData as $data) {
|
|
|
|
$r = $TmpFile->write($data);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
|
|
|
$this->assertEqual($data, file_get_contents($tmpFile));
|
2007-10-11 07:19:06 +00:00
|
|
|
$this->assertTrue(is_resource($TmpFile->handle));
|
|
|
|
$TmpFile->close();
|
2007-05-27 00:30:43 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
}
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
2007-05-27 00:30:43 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
function testAppend() {
|
2007-10-11 07:19:06 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:19:06 +00:00
|
|
|
$TmpFile =& new File($tmpFile);
|
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-11 07:19:06 +00:00
|
|
|
$fragments = array('CakePHP\'s', ' test suite', ' was here ...', '');
|
|
|
|
$data = null;
|
|
|
|
foreach ($fragments as $fragment) {
|
|
|
|
$r = $TmpFile->append($fragment);
|
|
|
|
$this->assertTrue($r);
|
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
|
|
|
$data = $data.$fragment;
|
|
|
|
$this->assertEqual($data, file_get_contents($tmpFile));
|
|
|
|
$TmpFile->close();
|
|
|
|
}
|
2007-10-09 18:34:41 +00:00
|
|
|
}
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
function testDelete() {
|
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
};
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
if (!file_exists($tmpFile)) {
|
|
|
|
touch($tmpFile);
|
|
|
|
}
|
|
|
|
$TmpFile =& new File($tmpFile);
|
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
2007-10-16 09:48:59 +00:00
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertTrue($result);
|
2007-10-09 18:34:41 +00:00
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
2007-10-16 09:48:59 +00:00
|
|
|
|
|
|
|
$TmpFile =& new File('/this/does/not/exist');
|
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertFalse($result);
|
2007-10-09 18:34:41 +00:00
|
|
|
}
|
2007-05-27 00:30:43 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
function _getTmpFile($paintSkip = true) {
|
|
|
|
$tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp';
|
|
|
|
if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
|
|
|
|
return $tmpFile;
|
|
|
|
};
|
2007-10-16 09:48:59 +00:00
|
|
|
|
2007-10-09 18:34:41 +00:00
|
|
|
if ($paintSkip) {
|
|
|
|
$caller = 'test';
|
|
|
|
if (function_exists('debug_backtrace')) {
|
|
|
|
$trace = debug_backtrace();
|
|
|
|
$caller = $trace[1]['function'].'()';
|
|
|
|
}
|
|
|
|
$assertLine = new SimpleStackTrace(array(__FUNCTION__));
|
|
|
|
$assertLine = $assertLine->traceMethod();
|
|
|
|
$shortPath = substr($tmpFile, strlen(ROOT));
|
|
|
|
|
|
|
|
$message = sprintf(__('[FileTest] Skipping %s because "%s" not writeable!', true), $caller, $shortPath).$assertLine;
|
|
|
|
$this->_reporter->paintSkip($message);
|
|
|
|
}
|
|
|
|
return false;
|
2007-05-27 00:30:43 +00:00
|
|
|
}
|
2007-05-03 23:25:21 +00:00
|
|
|
}
|
|
|
|
?>
|