Added support for $bytes parameter in File::read

Re-factored write function
Improved test coverage of File class

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5747 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2007-10-11 07:19:06 +00:00
parent f2ec5b3908
commit 6dc279c7b7
2 changed files with 41 additions and 19 deletions

View file

@ -143,13 +143,14 @@ class File extends Object{
* @return string Contents
* @access public
*/
function read($bytes = false, $mode = 'rb') {
function read($bytes = false, $mode = 'rb', $forceMode = false) {
if (!is_int($bytes)) {
$contents = file_get_contents($this->pwd());
return $contents;
}
// TODO: Implement support for $bytes parameter
$this->open($mode, $forceMode);
return fread($this->handle, $bytes);
}
/**
* Append given data string to this File.
@ -169,19 +170,11 @@ class File extends Object{
* @return boolean Success
* @access public
*/
function write($data, $mode = 'w') {
// TODO: Refactor to use File::open() instead
$file = $this->pwd();
if (!($handle = fopen($file, $mode))) {
trigger_error(sprintf(__("[File] Could not open %s with mode %s!", true), $file, $mode), E_USER_WARNING);
function write($data, $mode = 'w', $forceMode = false) {
if (!$this->open($mode, $forceMode)) {
return false;
}
if (false === fwrite($handle, $data)) {
return false;
}
if (!fclose($handle)) {
if (false === fwrite($this->handle, $data)) {
return false;
}
return true;

View file

@ -93,10 +93,17 @@ class FileTest extends UnitTestCase {
$result = $this->File->read();
$expecting = file_get_contents(__FILE__);
$this->assertEqual($result, $expecting);
$this->assertTrue(!is_resource($this->File->handle));
// $expecting = substr($expecting, 0, 3);
// $result = $this->File->read(3);
// $this->assertEqual($result, $expecting);
$data = $expecting;
$expecting = substr($data, 0, 3);
$result = $this->File->read(3);
$this->assertEqual($result, $expecting);
$this->assertTrue(is_resource($this->File->handle));
$expecting = substr($data, 3, 3);
$result = $this->File->read(3);
$this->assertEqual($result, $expecting);
}
function testOpen() {
@ -157,21 +164,43 @@ class FileTest extends UnitTestCase {
$TmpFile =& new File($tmpFile);
$this->assertFalse(file_exists($tmpFile));
$testData = array('CakePHP\'s test suite was here ...', '');
$this->assertFalse(is_resource($TmpFile->handle));
$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
foreach ($testData as $data) {
$r = $TmpFile->write($data);
$this->assertTrue($r);
$this->assertTrue(file_exists($tmpFile));
$this->assertEqual($data, file_get_contents($tmpFile));
$this->assertTrue(is_resource($TmpFile->handle));
$TmpFile->close();
}
unlink($tmpFile);
}
function testAppend() {
// TODO: Test the append function
if (!$tmpFile = $this->_getTmpFile()) {
return false;
};
if (file_exists($tmpFile)) {
unlink($tmpFile);
}
$TmpFile =& new File($tmpFile);
$this->assertFalse(file_exists($tmpFile));
$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();
}
}
function testDelete() {