mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
f2ec5b3908
commit
6dc279c7b7
2 changed files with 41 additions and 19 deletions
|
@ -143,13 +143,14 @@ class File extends Object{
|
||||||
* @return string Contents
|
* @return string Contents
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function read($bytes = false, $mode = 'rb') {
|
function read($bytes = false, $mode = 'rb', $forceMode = false) {
|
||||||
if (!is_int($bytes)) {
|
if (!is_int($bytes)) {
|
||||||
$contents = file_get_contents($this->pwd());
|
$contents = file_get_contents($this->pwd());
|
||||||
return $contents;
|
return $contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement support for $bytes parameter
|
$this->open($mode, $forceMode);
|
||||||
|
return fread($this->handle, $bytes);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Append given data string to this File.
|
* Append given data string to this File.
|
||||||
|
@ -169,19 +170,11 @@ class File extends Object{
|
||||||
* @return boolean Success
|
* @return boolean Success
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function write($data, $mode = 'w') {
|
function write($data, $mode = 'w', $forceMode = false) {
|
||||||
// TODO: Refactor to use File::open() instead
|
if (!$this->open($mode, $forceMode)) {
|
||||||
$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);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (false === fwrite($this->handle, $data)) {
|
||||||
if (false === fwrite($handle, $data)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!fclose($handle)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -93,10 +93,17 @@ class FileTest extends UnitTestCase {
|
||||||
$result = $this->File->read();
|
$result = $this->File->read();
|
||||||
$expecting = file_get_contents(__FILE__);
|
$expecting = file_get_contents(__FILE__);
|
||||||
$this->assertEqual($result, $expecting);
|
$this->assertEqual($result, $expecting);
|
||||||
|
$this->assertTrue(!is_resource($this->File->handle));
|
||||||
|
|
||||||
// $expecting = substr($expecting, 0, 3);
|
$data = $expecting;
|
||||||
// $result = $this->File->read(3);
|
$expecting = substr($data, 0, 3);
|
||||||
// $this->assertEqual($result, $expecting);
|
$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() {
|
function testOpen() {
|
||||||
|
@ -157,21 +164,43 @@ class FileTest extends UnitTestCase {
|
||||||
|
|
||||||
$TmpFile =& new File($tmpFile);
|
$TmpFile =& new File($tmpFile);
|
||||||
$this->assertFalse(file_exists($tmpFile));
|
$this->assertFalse(file_exists($tmpFile));
|
||||||
|
$this->assertFalse(is_resource($TmpFile->handle));
|
||||||
$testData = array('CakePHP\'s test suite was here ...', '');
|
|
||||||
|
$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
|
||||||
foreach ($testData as $data) {
|
foreach ($testData as $data) {
|
||||||
$r = $TmpFile->write($data);
|
$r = $TmpFile->write($data);
|
||||||
|
|
||||||
$this->assertTrue($r);
|
$this->assertTrue($r);
|
||||||
$this->assertTrue(file_exists($tmpFile));
|
$this->assertTrue(file_exists($tmpFile));
|
||||||
$this->assertEqual($data, file_get_contents($tmpFile));
|
$this->assertEqual($data, file_get_contents($tmpFile));
|
||||||
|
$this->assertTrue(is_resource($TmpFile->handle));
|
||||||
|
$TmpFile->close();
|
||||||
|
|
||||||
}
|
}
|
||||||
unlink($tmpFile);
|
unlink($tmpFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testAppend() {
|
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() {
|
function testDelete() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue