2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* FileTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:55:24 +00:00
|
|
|
App::uses('File', 'Utility');
|
2011-04-17 16:03:43 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* FileTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-07-21 02:40:58 +00:00
|
|
|
class FileTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* File property
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @var mixed null
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $File = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
/**
|
|
|
|
* setup the test case
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-05-31 02:46:42 +00:00
|
|
|
parent::setUp();
|
|
|
|
$file = __FILE__;
|
|
|
|
$this->File = new File($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown method
|
2010-05-31 02:46:42 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-12-04 21:27:51 +00:00
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2010-05-31 02:46:42 +00:00
|
|
|
$this->File->close();
|
|
|
|
unset($this->File);
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testBasic method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBasic() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$file = __FILE__;
|
|
|
|
|
|
|
|
$result = $this->File->pwd();
|
|
|
|
$expecting = $file;
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->name;
|
|
|
|
$expecting = basename(__FILE__);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->info();
|
2008-10-15 12:42:03 +00:00
|
|
|
$expecting = array(
|
|
|
|
'dirname' => dirname(__FILE__), 'basename' => basename(__FILE__),
|
2011-09-18 16:24:56 +00:00
|
|
|
'extension' => 'php', 'filename' =>'FileTest', 'filesize' => filesize($file)
|
2008-10-15 12:42:03 +00:00
|
|
|
);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->ext();
|
|
|
|
$expecting = 'php';
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->name();
|
2011-04-12 02:53:44 +00:00
|
|
|
$expecting = 'FileTest';
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->md5();
|
|
|
|
$expecting = md5_file($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->md5(true);
|
|
|
|
$expecting = md5_file($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->size();
|
|
|
|
$expecting = filesize($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->owner();
|
|
|
|
$expecting = fileowner($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->group();
|
|
|
|
$expecting = filegroup($file);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->Folder();
|
2011-11-16 17:56:34 +00:00
|
|
|
$this->assertInstanceOf('Folder', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2011-05-31 00:49:46 +00:00
|
|
|
$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
|
|
|
|
|
2008-11-08 03:47:09 +00:00
|
|
|
$result = $this->File->perms();
|
2011-10-01 02:10:16 +00:00
|
|
|
$expecting = decoct(0644 & ~umask());
|
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testRead method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRead() {
|
2010-05-29 16:47:31 +00:00
|
|
|
$file = __FILE__;
|
2010-11-13 04:05:44 +00:00
|
|
|
$this->File = new File($file);
|
2010-05-29 16:47:31 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $this->File->read();
|
|
|
|
$expecting = file_get_contents(__FILE__);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(!is_resource($this->File->handle));
|
|
|
|
|
2008-10-15 12:42:03 +00:00
|
|
|
$this->File->lock = true;
|
|
|
|
$result = $this->File->read();
|
|
|
|
$expecting = file_get_contents(__FILE__);
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($result, trim($expecting));
|
2008-10-15 12:42:03 +00:00
|
|
|
$this->File->lock = null;
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$data = $expecting;
|
|
|
|
$expecting = substr($data, 0, 3);
|
|
|
|
$result = $this->File->read(3);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$expecting = substr($data, 3, 3);
|
|
|
|
$result = $this->File->read(3);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOffset method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOffset() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->File->close();
|
|
|
|
|
|
|
|
$result = $this->File->offset();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$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;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, $expecting);
|
2008-05-30 11:40:08 +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);
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals($expecting, $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
$result = $this->File->offset();
|
|
|
|
$expecting = 5+3;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame($result, $expecting);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOpen method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOpen() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$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));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testClose method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testClose() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->File->handle = null;
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($this->File->close());
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
|
|
|
|
$this->File->handle = fopen(__FILE__, 'r');
|
|
|
|
$this->assertTrue(is_resource($this->File->handle));
|
|
|
|
$this->assertTrue($this->File->close());
|
|
|
|
$this->assertFalse(is_resource($this->File->handle));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCreate method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCreate() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$tmpFile = TMP.'tests'.DS.'cakephp.file.test.tmp';
|
2010-05-31 02:46:42 +00:00
|
|
|
$File = new File($tmpFile, true, 0777);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($File->exists());
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testOpeningNonExistantFileCreatesIt method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testOpeningNonExistantFileCreatesIt() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
2011-10-01 02:10:16 +00:00
|
|
|
$this->assertEquals('', $someFile->read());
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testPrepare method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testPrepare() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
|
2008-11-08 03:47:09 +00:00
|
|
|
if (DS == '\\') {
|
|
|
|
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
|
|
|
|
$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
|
|
|
|
} else {
|
|
|
|
$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
|
|
|
|
}
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame(File::prepare($string), $expected);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2008-10-15 12:42:03 +00:00
|
|
|
$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
|
|
|
|
$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertSame(File::prepare($string, true), $expected);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertTrue($someFile->readable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testWritable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWritable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertTrue($someFile->writable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testExecutable method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testExecutable() {
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue($someFile->open());
|
|
|
|
$this->assertFalse($someFile->executable());
|
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLastAccess method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLastAccess() {
|
2011-12-05 16:11:41 +00:00
|
|
|
$ts = time();
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($someFile->lastAccess());
|
|
|
|
$this->assertTrue($someFile->open());
|
2011-12-05 16:11:41 +00:00
|
|
|
$this->assertTrue($someFile->lastAccess() >= $ts);
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testLastChange method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testLastChange() {
|
2011-12-05 16:13:22 +00:00
|
|
|
$ts = time();
|
2010-05-31 02:46:42 +00:00
|
|
|
$someFile = new File(TMP . 'some_file.txt', false);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse($someFile->lastChange());
|
|
|
|
$this->assertTrue($someFile->open('r+'));
|
2011-12-05 16:13:22 +00:00
|
|
|
$this->assertTrue($someFile->lastChange() >= $ts);
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->write('something');
|
2011-12-05 16:13:22 +00:00
|
|
|
$this->assertTrue($someFile->lastChange() >= $ts);
|
2008-05-30 11:40:08 +00:00
|
|
|
$someFile->close();
|
|
|
|
$someFile->delete();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testWrite method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWrite() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
|
|
|
$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));
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, file_get_contents($tmpFile));
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(is_resource($TmpFile->handle));
|
|
|
|
$TmpFile->close();
|
|
|
|
|
|
|
|
}
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testAppend method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAppend() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
if (file_exists($tmpFile)) {
|
|
|
|
unlink($tmpFile);
|
|
|
|
}
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$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;
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals($data, file_get_contents($tmpFile));
|
2008-05-30 11:40:08 +00:00
|
|
|
$TmpFile->close();
|
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testDelete method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDelete() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
2011-08-29 02:19:53 +00:00
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
|
|
if (!file_exists($tmpFile)) {
|
|
|
|
touch($tmpFile);
|
|
|
|
}
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File($tmpFile);
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(file_exists($tmpFile));
|
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertFalse(file_exists($tmpFile));
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File('/this/does/not/exist');
|
2008-05-30 11:40:08 +00:00
|
|
|
$result = $TmpFile->delete();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2011-08-29 02:19:53 +00:00
|
|
|
/**
|
|
|
|
* Windows has issues unlinking files if there are
|
|
|
|
* active filehandles open.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function testDeleteAfterRead() {
|
|
|
|
if (!$tmpFile = $this->_getTmpFile()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!file_exists($tmpFile)) {
|
|
|
|
touch($tmpFile);
|
|
|
|
}
|
2011-10-20 23:10:09 +00:00
|
|
|
$File =& new File($tmpFile);
|
|
|
|
$File->read();
|
|
|
|
$this->assertTrue($File->delete());
|
2011-08-29 02:19:53 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 03:27:31 +00:00
|
|
|
/**
|
|
|
|
* testCopy method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCopy() {
|
2009-10-09 03:27:31 +00:00
|
|
|
$dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
|
|
|
|
$file = __FILE__;
|
2010-05-31 02:46:42 +00:00
|
|
|
$this->File = new File($file);
|
2009-10-09 03:27:31 +00:00
|
|
|
$result = $this->File->copy($dest);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $this->File->copy($dest, true);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$result = $this->File->copy($dest, false);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$this->File->close();
|
|
|
|
unlink($dest);
|
|
|
|
|
2010-05-31 02:46:42 +00:00
|
|
|
$TmpFile = new File('/this/does/not/exist');
|
2009-10-09 03:27:31 +00:00
|
|
|
$result = $TmpFile->copy($dest);
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$TmpFile->close();
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
|
|
|
* getTmpFile method
|
2008-09-12 05:11:34 +00:00
|
|
|
*
|
|
|
|
* @param bool $paintSkip
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
function _getTmpFile($paintSkip = true) {
|
2010-05-29 16:47:31 +00:00
|
|
|
$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
|
|
|
|
return $tmpFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($paintSkip) {
|
2011-06-21 21:20:04 +00:00
|
|
|
$trace = debug_backtrace();
|
|
|
|
$caller = $trace[0]['function'];
|
|
|
|
$shortPath = dirname($tmpFile);
|
|
|
|
|
|
|
|
$message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
|
|
|
|
$this->markTestSkipped($message);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-17 16:03:43 +00:00
|
|
|
}
|