blob: 045216b1647872b15e170d0b6362e64e4347c277 [file] [log] [blame]
Greg Aker57fb5182011-04-21 14:58:26 -05001<?php
2
3require BASEPATH.'libraries/Table.php';
4
5class Table_test extends CI_TestCase
6{
7
Eric Barnes68286a42011-04-21 22:00:33 -04008 public function set_up()
Greg Aker57fb5182011-04-21 14:58:26 -05009 {
10 $obj = new StdClass;
11 $obj->table = new CI_table();
12
13 $this->ci_instance($obj);
14
15 $this->table = $obj->table;
16 }
17
18
19 // Setter Methods
20 // --------------------------------------------------------------------
21
Eric Barnes68286a42011-04-21 22:00:33 -040022 public function test_set_template()
Greg Aker57fb5182011-04-21 14:58:26 -050023 {
24 $this->assertFalse($this->table->set_template('not an array'));
25
26 $template = array(
27 'a' => 'b'
28 );
29
30 $this->table->set_template($template);
31 $this->assertEquals($template, $this->table->template);
32 }
33
Eric Barnes68286a42011-04-21 22:00:33 -040034 public function test_set_empty()
Greg Aker57fb5182011-04-21 14:58:26 -050035 {
36 $this->table->set_empty('nada');
37 $this->assertEquals('nada', $this->table->empty_cells);
38 }
39
Eric Barnes68286a42011-04-21 22:00:33 -040040 public function test_set_caption()
Greg Aker57fb5182011-04-21 14:58:26 -050041 {
42 $this->table->set_caption('awesome cap');
43 $this->assertEquals('awesome cap', $this->table->caption);
44 }
45
46
47 /*
48 * @depends testPrepArgs
49 */
Eric Barnes68286a42011-04-21 22:00:33 -040050 public function test_set_heading()
Greg Aker57fb5182011-04-21 14:58:26 -050051 {
52 // uses _prep_args internally, so we'll just do a quick
53 // check to verify that func_get_args and prep_args are
54 // being called.
55
56 $this->table->set_heading('name', 'color', 'size');
57
58 $this->assertEquals(
59 array(
60 array('data' => 'name'),
61 array('data' => 'color'),
62 array('data' => 'size')
63 ),
64 $this->table->heading
65 );
66 }
67
68
69 /*
70 * @depends testPrepArgs
71 */
Eric Barnes68286a42011-04-21 22:00:33 -040072 public function test_add_row()
Greg Aker57fb5182011-04-21 14:58:26 -050073 {
74 // uses _prep_args internally, so we'll just do a quick
75 // check to verify that func_get_args and prep_args are
76 // being called.
77
78 $this->table->add_row('my', 'pony', 'sings');
79 $this->table->add_row('your', 'pony', 'stinks');
80 $this->table->add_row('my pony', '>', 'your pony');
81
82 $this->assertEquals(count($this->table->rows), 3);
83
84 $this->assertEquals(
85 array(
86 array('data' => 'your'),
87 array('data' => 'pony'),
88 array('data' => 'stinks')
89 ),
90 $this->table->rows[1]
91 );
92 }
93
94
95 // Uility Methods
96 // --------------------------------------------------------------------
97
Eric Barnes68286a42011-04-21 22:00:33 -040098 public function test_prep_args()
Greg Aker57fb5182011-04-21 14:58:26 -050099 {
100 $expected = array(
101 array('data' => 'name'),
102 array('data' => 'color'),
103 array('data' => 'size')
104 );
105
106 // test what would be discreet args,
107 // basically means a single array as the calling method
108 // will use func_get_args()
tiyowane39728c2012-03-10 02:10:44 +0400109
110 $reflectionOfTable = new ReflectionClass($this->table);
111 $method = $reflectionOfTable->getMethod('_prep_args');
112
113 $method->setAccessible(true);
114
Greg Aker57fb5182011-04-21 14:58:26 -0500115 $this->assertEquals(
116 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400117 $method->invokeArgs(
118 $this->table, array(array('name', 'color', 'size'), 'discreet')
119 )
120 );
Greg Aker57fb5182011-04-21 14:58:26 -0500121
122 // test what would be a single array argument. Again, nested
123 // due to func_get_args on calling methods
124 $this->assertEquals(
125 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400126 $method->invokeArgs(
127 $this->table, array(array('name', 'color', 'size'), 'array')
128 )
129 );
Greg Aker57fb5182011-04-21 14:58:26 -0500130
131
132 // with cell attributes
133
134 // need to add that new argument row to our expected outcome
135 $expected[] = array('data' => 'weight', 'class' => 'awesome');
tiyowane39728c2012-03-10 02:10:44 +0400136
Greg Aker57fb5182011-04-21 14:58:26 -0500137 $this->assertEquals(
138 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400139 $method->invokeArgs(
140 $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes')
141 )
142 );
Greg Aker57fb5182011-04-21 14:58:26 -0500143 }
144
Eric Barnes68286a42011-04-21 22:00:33 -0400145 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500146 {
tiyowane39728c2012-03-10 02:10:44 +0400147 $reflectionOfTable = new ReflectionClass($this->table);
148 $method = $reflectionOfTable->getMethod('_default_template');
149
150 $method->setAccessible(true);
151
152 $deft_template = $method->invoke($this->table);
Greg Aker57fb5182011-04-21 14:58:26 -0500153 $keys = array(
154 'table_open',
155 'thead_open', 'thead_close',
156 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
157 'tbody_open', 'tbody_close',
158 'row_start', 'row_end', 'cell_start', 'cell_end',
159 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
160 'table_close'
161 );
162
163 foreach ($keys as $key)
164 {
165 $this->assertArrayHasKey($key, $deft_template);
166 }
167 }
168
Eric Barnes68286a42011-04-21 22:00:33 -0400169 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500170 {
tiyowane39728c2012-03-10 02:10:44 +0400171 $reflectionOfTable = new ReflectionClass($this->table);
172 $method = $reflectionOfTable->getMethod('_compile_template');
173
174 $method->setAccessible(true);
175
Greg Aker57fb5182011-04-21 14:58:26 -0500176 $this->assertFalse($this->table->set_template('invalid_junk'));
177
178 // non default key
179 $this->table->set_template(array('nonsense' => 'foo'));
tiyowane39728c2012-03-10 02:10:44 +0400180 $method->invoke($this->table);
Greg Aker57fb5182011-04-21 14:58:26 -0500181
182 $this->assertArrayHasKey('nonsense', $this->table->template);
183 $this->assertEquals('foo', $this->table->template['nonsense']);
184
185 // override default
186 $this->table->set_template(array('table_close' => '</table junk>'));
tiyowane39728c2012-03-10 02:10:44 +0400187 $method->invoke($this->table);
Greg Aker57fb5182011-04-21 14:58:26 -0500188
189 $this->assertArrayHasKey('table_close', $this->table->template);
190 $this->assertEquals('</table junk>', $this->table->template['table_close']);
191 }
192
Eric Barnes68286a42011-04-21 22:00:33 -0400193 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500194 {
195 // Test bogus parameters
196 $this->assertFalse($this->table->make_columns('invalid_junk'));
197 $this->assertFalse( $this->table->make_columns(array()));
198 // $this->assertFalse(
199 // $this->table->make_columns(array('one', 'two')),
200 // '2.5' // not an integer!
201 // );
202
203
204 // Now on to the actual column creation
205
206 $five_values = array(
207 'Laura', 'Red', '15',
208 'Katie', 'Blue'
209 );
210
211 // No column count - no changes to the array
212 $this->assertEquals(
213 $five_values,
214 $this->table->make_columns($five_values)
215 );
216
217 // Column count of 3 leaves us with one &nbsp;
218 $this->assertEquals(
219 array(
220 array('Laura', 'Red', '15'),
221 array('Katie', 'Blue', '&nbsp;')
222 ),
223 $this->table->make_columns($five_values, 3)
224 );
225
226 $this->markTestSkipped('Look at commented assertFalse above');
227 }
228
Eric Barnes68286a42011-04-21 22:00:33 -0400229 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500230 {
231 $this->table->set_heading('Name', 'Color', 'Size');
232
233 // Make columns changes auto_heading
234 $rows = $this->table->make_columns(array(
235 'Laura', 'Red', '15',
236 'Katie', 'Blue'
237 ), 3);
238
239 foreach ($rows as $row)
240 {
241 $this->table->add_row($row);
242 }
243
244 $this->assertFalse($this->table->auto_heading);
245 $this->assertEquals(count($this->table->heading), 3);
246 $this->assertEquals(count($this->table->rows), 2);
247
248 $this->table->clear();
249
250 $this->assertTrue($this->table->auto_heading);
251 $this->assertEmpty($this->table->heading);
252 $this->assertEmpty($this->table->rows);
253 }
254
255
Eric Barnes68286a42011-04-21 22:00:33 -0400256 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500257 {
tiyowane39728c2012-03-10 02:10:44 +0400258 $reflectionOfTable = new ReflectionClass($this->table);
259 $method = $reflectionOfTable->getMethod('_set_from_array');
260
261 $method->setAccessible(true);
262
263 $this->assertFalse($method->invokeArgs($this->table, array('bogus')));
264 $this->assertFalse($method->invoke($this->table, array()));
Greg Aker57fb5182011-04-21 14:58:26 -0500265
266 $data = array(
267 array('name', 'color', 'number'),
268 array('Laura', 'Red', '22'),
269 array('Katie', 'Blue')
270 );
271
tiyowane39728c2012-03-10 02:10:44 +0400272 $method->invokeArgs($this->table, array($data, FALSE));
Greg Aker57fb5182011-04-21 14:58:26 -0500273 $this->assertEmpty($this->table->heading);
274
275 $this->table->clear();
276
277 $expected_heading = array(
278 array('data' => 'name'),
279 array('data' => 'color'),
280 array('data' => 'number')
281 );
282
283 $expected_second = array(
284 array('data' => 'Katie'),
285 array('data' => 'Blue'),
286 );
287
tiyowane39728c2012-03-10 02:10:44 +0400288 $method->invokeArgs($this->table, array($data));
Greg Aker57fb5182011-04-21 14:58:26 -0500289 $this->assertEquals(count($this->table->rows), 2);
290
291 $this->assertEquals(
292 $expected_heading,
293 $this->table->heading
294 );
295
296 $this->assertEquals(
297 $expected_second,
298 $this->table->rows[1]
299 );
300 }
301
Eric Barnes68286a42011-04-21 22:00:33 -0400302 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500303 {
304 $this->markTestSkipped('Not yet implemented.');
305 }
306
307 // Test main generate method
308 // --------------------------------------------------------------------
309}