blob: 0208a465acccb811252f4c51c5b6ad489c716147 [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'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700197 $this->assertFalse($this->table->make_columns(array()));
198 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Greg Aker57fb5182011-04-21 14:58:26 -0500199
200
201 // Now on to the actual column creation
202
203 $five_values = array(
204 'Laura', 'Red', '15',
205 'Katie', 'Blue'
206 );
207
208 // No column count - no changes to the array
209 $this->assertEquals(
210 $five_values,
211 $this->table->make_columns($five_values)
212 );
213
214 // Column count of 3 leaves us with one &nbsp;
215 $this->assertEquals(
216 array(
217 array('Laura', 'Red', '15'),
218 array('Katie', 'Blue', '&nbsp;')
219 ),
220 $this->table->make_columns($five_values, 3)
221 );
Greg Aker57fb5182011-04-21 14:58:26 -0500222 }
223
Eric Barnes68286a42011-04-21 22:00:33 -0400224 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500225 {
226 $this->table->set_heading('Name', 'Color', 'Size');
227
228 // Make columns changes auto_heading
229 $rows = $this->table->make_columns(array(
230 'Laura', 'Red', '15',
231 'Katie', 'Blue'
232 ), 3);
233
234 foreach ($rows as $row)
235 {
236 $this->table->add_row($row);
237 }
238
239 $this->assertFalse($this->table->auto_heading);
240 $this->assertEquals(count($this->table->heading), 3);
241 $this->assertEquals(count($this->table->rows), 2);
242
243 $this->table->clear();
244
245 $this->assertTrue($this->table->auto_heading);
246 $this->assertEmpty($this->table->heading);
247 $this->assertEmpty($this->table->rows);
248 }
249
250
Eric Barnes68286a42011-04-21 22:00:33 -0400251 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500252 {
tiyowane39728c2012-03-10 02:10:44 +0400253 $reflectionOfTable = new ReflectionClass($this->table);
254 $method = $reflectionOfTable->getMethod('_set_from_array');
255
256 $method->setAccessible(true);
257
258 $this->assertFalse($method->invokeArgs($this->table, array('bogus')));
259 $this->assertFalse($method->invoke($this->table, array()));
Greg Aker57fb5182011-04-21 14:58:26 -0500260
261 $data = array(
262 array('name', 'color', 'number'),
263 array('Laura', 'Red', '22'),
264 array('Katie', 'Blue')
265 );
266
tiyowane39728c2012-03-10 02:10:44 +0400267 $method->invokeArgs($this->table, array($data, FALSE));
Greg Aker57fb5182011-04-21 14:58:26 -0500268 $this->assertEmpty($this->table->heading);
269
270 $this->table->clear();
271
272 $expected_heading = array(
273 array('data' => 'name'),
274 array('data' => 'color'),
275 array('data' => 'number')
276 );
277
278 $expected_second = array(
279 array('data' => 'Katie'),
280 array('data' => 'Blue'),
281 );
282
tiyowane39728c2012-03-10 02:10:44 +0400283 $method->invokeArgs($this->table, array($data));
Greg Aker57fb5182011-04-21 14:58:26 -0500284 $this->assertEquals(count($this->table->rows), 2);
285
286 $this->assertEquals(
287 $expected_heading,
288 $this->table->heading
289 );
290
291 $this->assertEquals(
292 $expected_second,
293 $this->table->rows[1]
294 );
295 }
296
Eric Barnes68286a42011-04-21 22:00:33 -0400297 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500298 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700299 $reflectionOfTable = new ReflectionClass($this->table);
300 $method = $reflectionOfTable->getMethod('_set_from_object');
301
302 $method->setAccessible(true);
303
304 // Make a stub of query instance
305 $query = new CI_TestCase();
306 $query->list_fields = function(){
307 return array('name', 'email');
308 };
309 $query->result_array = function(){
310 return array(
311 array('name' => 'John Doe', 'email' => 'john@doe.com'),
312 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
313 );
314 };
315 $query->num_rows = function(){
316 return 2;
317 };
318
319 $expected_heading = array(
320 array('data' => 'name'),
321 array('data' => 'email')
322 );
323
324 $expected_second = array(
325 'name' => array('data' => 'Foo Bar'),
326 'email' => array('data' => 'foo@bar.com'),
327 );
328
329 $method->invokeArgs($this->table, array($query));
330
331 $this->assertEquals(
332 $expected_heading,
333 $this->table->heading
334 );
335
336 $this->assertEquals(
337 $expected_second,
338 $this->table->rows[1]
339 );
Greg Aker57fb5182011-04-21 14:58:26 -0500340 }
341
342 // Test main generate method
343 // --------------------------------------------------------------------
344}