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