Greg Aker | d92277d | 2011-04-22 12:56:22 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | require_once 'vfsStream/vfsStream.php'; |
| 4 | require BASEPATH.'helpers/file_helper.php'; |
| 5 | |
| 6 | class File_helper_Test extends CI_TestCase |
| 7 | { |
| 8 | public function set_up() |
| 9 | { |
| 10 | vfsStreamWrapper::register(); |
| 11 | vfsStreamWrapper::setRoot(new vfsStreamDirectory('testDir')); |
| 12 | |
| 13 | $this->_test_dir = vfsStreamWrapper::getRoot(); |
| 14 | } |
| 15 | |
| 16 | // -------------------------------------------------------------------- |
| 17 | |
| 18 | public function test_read_file() |
| 19 | { |
| 20 | $this->assertFalse(read_file('does_not_exist')); |
| 21 | |
| 22 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 23 | |
| 24 | $file = vfsStream::newFile('my_file.txt')->withContent($content) |
| 25 | ->at($this->_test_dir); |
| 26 | |
| 27 | $this->assertEquals($content, read_file(vfsStream::url('my_file.txt'))); |
| 28 | } |
| 29 | |
| 30 | // -------------------------------------------------------------------- |
| 31 | |
| 32 | public function test_octal_permissions() |
| 33 | { |
| 34 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 35 | |
| 36 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
| 37 | ->lastModified(time() - 86400) |
| 38 | ->at($this->_test_dir); |
| 39 | |
| 40 | $this->assertEquals('777', octal_permissions($file->getPermissions())); |
| 41 | } |
| 42 | |
| 43 | // -------------------------------------------------------------------- |
| 44 | |
| 45 | /** |
| 46 | * More tests should happen here, since I'm not hitting the whole function. |
| 47 | */ |
| 48 | public function test_symbolic_permissions() |
| 49 | { |
| 50 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 51 | |
| 52 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
| 53 | ->lastModified(time() - 86400) |
| 54 | ->at($this->_test_dir); |
| 55 | |
| 56 | $this->assertEquals('urwxrwxrwx', symbolic_permissions($file->getPermissions())); |
| 57 | } |
| 58 | |
| 59 | // -------------------------------------------------------------------- |
| 60 | |
| 61 | public function test_get_mime_by_extension() |
| 62 | { |
| 63 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 64 | |
| 65 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
| 66 | ->lastModified(time() - 86400) |
| 67 | ->at($this->_test_dir); |
| 68 | |
| 69 | $this->assertEquals('text/plain', |
| 70 | get_mime_by_extension(vfsStream::url('my_file.txt'))); |
| 71 | |
| 72 | // Test a mime with an array, such as png |
| 73 | $file = vfsStream::newFile('foo.png')->at($this->_test_dir); |
| 74 | |
| 75 | $this->assertEquals('image/png', get_mime_by_extension(vfsStream::url('foo.png'))); |
| 76 | |
| 77 | // Test a file not in the mimes array |
| 78 | $file = vfsStream::newFile('foo.blarfengar')->at($this->_test_dir); |
| 79 | |
| 80 | $this->assertFalse(get_mime_by_extension(vfsStream::url('foo.blarfengar'))); |
| 81 | } |
| 82 | |
| 83 | // -------------------------------------------------------------------- |
| 84 | |
| 85 | public function test_get_file_info() |
| 86 | { |
| 87 | // Test Bad File |
| 88 | $this->assertFalse(get_file_info('i_am_bad_boo')); |
| 89 | |
| 90 | // Test the rest |
| 91 | |
| 92 | // First pass in an array |
| 93 | $vals = array( |
| 94 | 'name', 'server_path', 'size', 'date', |
| 95 | 'readable', 'writable', 'executable', 'fileperms' |
| 96 | ); |
| 97 | |
| 98 | $this->_test_get_file_info($vals); |
| 99 | |
| 100 | // Test passing in vals as a string. |
| 101 | $vals = 'name, server_path, size, date, readable, writable, executable, fileperms'; |
| 102 | $this->_test_get_file_info($vals); |
| 103 | } |
| 104 | |
| 105 | private function _test_get_file_info($vals) |
| 106 | { |
| 107 | $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 108 | $last_modified = time() - 86400; |
| 109 | |
| 110 | $file = vfsStream::newFile('my_file.txt', 0777)->withContent($content) |
| 111 | ->lastModified($last_modified) |
| 112 | ->at($this->_test_dir); |
| 113 | |
| 114 | $ret_values = array( |
| 115 | 'name' => 'my_file.txt', |
| 116 | 'server_path' => 'vfs://my_file.txt', |
| 117 | 'size' => 57, |
| 118 | 'date' => $last_modified, |
| 119 | 'readable' => TRUE, |
| 120 | 'writable' => TRUE, |
| 121 | 'executable' => TRUE, |
| 122 | 'fileperms' => 33279 |
| 123 | ); |
| 124 | |
| 125 | $info = get_file_info(vfsStream::url('my_file.txt'), $vals); |
| 126 | |
| 127 | foreach ($info as $k => $v) |
| 128 | { |
| 129 | $this->assertEquals($ret_values[$k], $v); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | // Skipping for now, as it's not implemented in vfsStreamWrapper |
| 136 | // flock(): vfsStreamWrapper::stream_lock is not implemented! |
| 137 | |
| 138 | // public function test_write_file() |
| 139 | // { |
| 140 | // if ( ! defined('FOPEN_WRITE_CREATE_DESTRUCTIVE')) |
| 141 | // { |
| 142 | // define('FOPEN_WRITE_CREATE_DESTRUCTIVE', 'wb'); |
| 143 | // } |
| 144 | // |
| 145 | // $content = 'Jack and Jill went up the mountain to fight a billy goat.'; |
| 146 | // |
| 147 | // $file = vfsStream::newFile('write.txt', 0777)->withContent('') |
| 148 | // ->lastModified(time() - 86400) |
| 149 | // ->at($this->_test_dir); |
| 150 | // |
| 151 | // $this->assertTrue(write_file(vfsStream::url('write.txt'), $content)); |
| 152 | // |
| 153 | // } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | } |