bringing the file class to almost 90% coverage

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6926 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
DarkAngelBGE 2008-05-18 14:48:48 +00:00
parent 291781ab5a
commit 45ed8ffeff
2 changed files with 130 additions and 10 deletions

View file

@ -91,6 +91,7 @@ class File extends Object {
if (!is_dir($path)) { if (!is_dir($path)) {
$this->name = basename($path); $this->name = basename($path);
} }
if (!$this->exists()) { if (!$this->exists()) {
if ($create === true) { if ($create === true) {
if ($this->safe($path) && $this->create() === false) { if ($this->safe($path) && $this->create() === false) {
@ -141,6 +142,7 @@ class File extends Object {
return false; return false;
} }
} }
$this->handle = fopen($this->pwd(), $mode); $this->handle = fopen($this->pwd(), $mode);
if (is_resource($this->handle)) { if (is_resource($this->handle)) {
return true; return true;
@ -207,9 +209,9 @@ class File extends Object {
* @return string * @return string
* @access public * @access public
*/ */
function prepare($data) { function prepare($data, $forceWindows = false) {
$lineBreak = "\n"; $lineBreak = "\n";
if (substr(PHP_OS,0,3) == "WIN") { if (substr(PHP_OS,0,3) == "WIN" || $forceWindows === true) {
$lineBreak = "\r\n"; $lineBreak = "\r\n";
} }
return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak)); return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));

View file

@ -66,6 +66,10 @@ class FileTest extends UnitTestCase {
$expecting = md5_file($file); $expecting = md5_file($file);
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
$result = $this->File->md5(true);
$expecting = md5_file($file);
$this->assertEqual($result, $expecting);
$result = $this->File->size(); $result = $this->File->size();
$expecting = filesize($file); $expecting = filesize($file);
$this->assertEqual($result, $expecting); $this->assertEqual($result, $expecting);
@ -148,13 +152,6 @@ class FileTest extends UnitTestCase {
$this->assertTrue($r); $this->assertTrue($r);
$this->assertFalse($handle === $this->File->handle); $this->assertFalse($handle === $this->File->handle);
$this->assertTrue(is_resource($this->File->handle)); $this->assertTrue(is_resource($this->File->handle));
$InvalidFile =& new File('invalid-file.invalid-ext');
$expecting =& new PatternExpectation('/could not open/i');
$this->expectError($expecting);
$InvalidFile->open();
$this->File->close();
} }
function testClose() { function testClose() {
@ -175,6 +172,67 @@ class FileTest extends UnitTestCase {
$this->assertTrue($File->exists()); $this->assertTrue($File->exists());
} }
function testOpeningNonExistantFileCreatesIt() {
$someFile =& new File('some_file.txt', false);
$this->assertTrue($someFile->open());
$this->assertEqual($someFile->read(), '');
$someFile->close();
$someFile->delete();
}
function testPrepare() {
$string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
$this->assertIdentical(File::prepare($string), $expected);
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\nfor\r\n\r\n\r\n\r\n\r\nhere";
$this->assertIdentical(File::prepare($string, true), $expected);
}
function testReadable() {
$someFile =& new File('some_file.txt', false);
$this->assertTrue($someFile->open());
$this->assertTrue($someFile->readable());
$someFile->close();
$someFile->delete();
}
function testWritable() {
$someFile =& new File('some_file.txt', false);
$this->assertTrue($someFile->open());
$this->assertTrue($someFile->writable());
$someFile->close();
$someFile->delete();
}
function testExecutable() {
$someFile =& new File('some_file.txt', false);
$this->assertTrue($someFile->open());
$this->assertFalse($someFile->executable());
$someFile->close();
$someFile->delete();
}
function testLastAccess() {
$someFile =& new File('some_file.txt', false);
$this->assertFalse($someFile->lastAccess());
$this->assertTrue($someFile->open());
$this->assertEqual($someFile->lastAccess(), time());
$someFile->close();
$someFile->delete();
}
function testLastChange() {
$someFile =& new File('some_file.txt', false);
$this->assertFalse($someFile->lastChange());
$this->assertTrue($someFile->open('r+'));
$this->assertEqual($someFile->lastChange(), time());
$someFile->write('something');
$this->assertEqual($someFile->lastChange(), time());
$someFile->close();
$someFile->delete();
}
function testWrite() { function testWrite() {
if (!$tmpFile = $this->_getTmpFile()) { if (!$tmpFile = $this->_getTmpFile()) {
return false; return false;
@ -263,5 +321,65 @@ class FileTest extends UnitTestCase {
} }
return false; return false;
} }
function testGetFullPathIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getFullPath();
$this->assertError();
}
function testGetNameIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getName();
$this->assertError();
}
function testFilenameIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->filename();
$this->assertError();
}
function testGetExtIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getExt();
$this->assertError();
}
function testGetMd5IsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getMd5();
$this->assertError();
}
function testGetSizeIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getSize();
$this->assertError();
}
function testGetOwnerIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getOwner();
$this->assertError();
}
function testGetGroupIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getGroup();
$this->assertError();
}
function testGetChmodIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getChmod();
$this->assertError();
}
function testGetFolderIsDeprecated() {
$someFile =& new File('some_file.txt', false);
$someFile->getFolder();
$this->assertError();
}
} }
?> ?>