blob: 61678afc7fb632c6f092a6a4764a9f650cbe3f62 [file] [log] [blame]
Greg Aker57fb5182011-04-21 14:58:26 -05001<?php
2
Taufan Adityaac5373a2012-03-28 16:03:38 +07003class Table_test extends CI_TestCase {
Greg Aker57fb5182011-04-21 14:58:26 -05004
Eric Barnes68286a42011-04-21 22:00:33 -04005 public function set_up()
Greg Aker57fb5182011-04-21 14:58:26 -05006 {
7 $obj = new StdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->table = new Mock_Libraries_Table();
Greg Aker57fb5182011-04-21 14:58:26 -05009
10 $this->ci_instance($obj);
11
12 $this->table = $obj->table;
13 }
14
15
16 // Setter Methods
17 // --------------------------------------------------------------------
18
Eric Barnes68286a42011-04-21 22:00:33 -040019 public function test_set_template()
Greg Aker57fb5182011-04-21 14:58:26 -050020 {
21 $this->assertFalse($this->table->set_template('not an array'));
22
23 $template = array(
24 'a' => 'b'
25 );
26
27 $this->table->set_template($template);
28 $this->assertEquals($template, $this->table->template);
29 }
30
Eric Barnes68286a42011-04-21 22:00:33 -040031 public function test_set_empty()
Greg Aker57fb5182011-04-21 14:58:26 -050032 {
33 $this->table->set_empty('nada');
34 $this->assertEquals('nada', $this->table->empty_cells);
35 }
36
Eric Barnes68286a42011-04-21 22:00:33 -040037 public function test_set_caption()
Greg Aker57fb5182011-04-21 14:58:26 -050038 {
39 $this->table->set_caption('awesome cap');
40 $this->assertEquals('awesome cap', $this->table->caption);
41 }
42
43
44 /*
45 * @depends testPrepArgs
46 */
Eric Barnes68286a42011-04-21 22:00:33 -040047 public function test_set_heading()
Greg Aker57fb5182011-04-21 14:58:26 -050048 {
49 // uses _prep_args internally, so we'll just do a quick
50 // check to verify that func_get_args and prep_args are
51 // being called.
52
53 $this->table->set_heading('name', 'color', 'size');
54
55 $this->assertEquals(
56 array(
57 array('data' => 'name'),
58 array('data' => 'color'),
59 array('data' => 'size')
60 ),
61 $this->table->heading
62 );
63 }
64
65
66 /*
67 * @depends testPrepArgs
68 */
Eric Barnes68286a42011-04-21 22:00:33 -040069 public function test_add_row()
Greg Aker57fb5182011-04-21 14:58:26 -050070 {
71 // uses _prep_args internally, so we'll just do a quick
72 // check to verify that func_get_args and prep_args are
73 // being called.
74
75 $this->table->add_row('my', 'pony', 'sings');
76 $this->table->add_row('your', 'pony', 'stinks');
77 $this->table->add_row('my pony', '>', 'your pony');
78
79 $this->assertEquals(count($this->table->rows), 3);
80
81 $this->assertEquals(
82 array(
83 array('data' => 'your'),
84 array('data' => 'pony'),
85 array('data' => 'stinks')
86 ),
87 $this->table->rows[1]
88 );
89 }
90
91
92 // Uility Methods
93 // --------------------------------------------------------------------
94
Eric Barnes68286a42011-04-21 22:00:33 -040095 public function test_prep_args()
Greg Aker57fb5182011-04-21 14:58:26 -050096 {
97 $expected = array(
98 array('data' => 'name'),
99 array('data' => 'color'),
100 array('data' => 'size')
101 );
102
103 // test what would be discreet args,
104 // basically means a single array as the calling method
105 // will use func_get_args()
tiyowane39728c2012-03-10 02:10:44 +0400106
107 $reflectionOfTable = new ReflectionClass($this->table);
108 $method = $reflectionOfTable->getMethod('_prep_args');
109
110 $method->setAccessible(true);
111
Greg Aker57fb5182011-04-21 14:58:26 -0500112 $this->assertEquals(
113 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400114 $method->invokeArgs(
115 $this->table, array(array('name', 'color', 'size'), 'discreet')
116 )
117 );
Greg Aker57fb5182011-04-21 14:58:26 -0500118
119 // test what would be a single array argument. Again, nested
120 // due to func_get_args on calling methods
121 $this->assertEquals(
122 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400123 $method->invokeArgs(
124 $this->table, array(array('name', 'color', 'size'), 'array')
125 )
126 );
Greg Aker57fb5182011-04-21 14:58:26 -0500127
128
129 // with cell attributes
130
131 // need to add that new argument row to our expected outcome
132 $expected[] = array('data' => 'weight', 'class' => 'awesome');
tiyowane39728c2012-03-10 02:10:44 +0400133
Greg Aker57fb5182011-04-21 14:58:26 -0500134 $this->assertEquals(
135 $expected,
tiyowane39728c2012-03-10 02:10:44 +0400136 $method->invokeArgs(
137 $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes')
138 )
139 );
Greg Aker57fb5182011-04-21 14:58:26 -0500140 }
141
Eric Barnes68286a42011-04-21 22:00:33 -0400142 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500143 {
Greg Aker57fb5182011-04-21 14:58:26 -0500144 $keys = array(
145 'table_open',
146 'thead_open', 'thead_close',
147 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
148 'tbody_open', 'tbody_close',
149 'row_start', 'row_end', 'cell_start', 'cell_end',
150 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
151 'table_close'
152 );
153
154 foreach ($keys as $key)
155 {
Taufan Adityad61b7722012-03-28 16:07:58 +0700156 $this->assertArrayHasKey($key, $this->table->default_template());
Greg Aker57fb5182011-04-21 14:58:26 -0500157 }
158 }
159
Eric Barnes68286a42011-04-21 22:00:33 -0400160 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500161 {
tiyowane39728c2012-03-10 02:10:44 +0400162 $reflectionOfTable = new ReflectionClass($this->table);
163 $method = $reflectionOfTable->getMethod('_compile_template');
164
165 $method->setAccessible(true);
166
Greg Aker57fb5182011-04-21 14:58:26 -0500167 $this->assertFalse($this->table->set_template('invalid_junk'));
168
169 // non default key
170 $this->table->set_template(array('nonsense' => 'foo'));
tiyowane39728c2012-03-10 02:10:44 +0400171 $method->invoke($this->table);
Greg Aker57fb5182011-04-21 14:58:26 -0500172
173 $this->assertArrayHasKey('nonsense', $this->table->template);
174 $this->assertEquals('foo', $this->table->template['nonsense']);
175
176 // override default
177 $this->table->set_template(array('table_close' => '</table junk>'));
tiyowane39728c2012-03-10 02:10:44 +0400178 $method->invoke($this->table);
Greg Aker57fb5182011-04-21 14:58:26 -0500179
180 $this->assertArrayHasKey('table_close', $this->table->template);
181 $this->assertEquals('</table junk>', $this->table->template['table_close']);
182 }
183
Eric Barnes68286a42011-04-21 22:00:33 -0400184 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500185 {
186 // Test bogus parameters
187 $this->assertFalse($this->table->make_columns('invalid_junk'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700188 $this->assertFalse($this->table->make_columns(array()));
189 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Greg Aker57fb5182011-04-21 14:58:26 -0500190
191
192 // Now on to the actual column creation
193
194 $five_values = array(
195 'Laura', 'Red', '15',
196 'Katie', 'Blue'
197 );
198
199 // No column count - no changes to the array
200 $this->assertEquals(
201 $five_values,
202 $this->table->make_columns($five_values)
203 );
204
205 // Column count of 3 leaves us with one &nbsp;
206 $this->assertEquals(
207 array(
208 array('Laura', 'Red', '15'),
209 array('Katie', 'Blue', '&nbsp;')
210 ),
211 $this->table->make_columns($five_values, 3)
212 );
Greg Aker57fb5182011-04-21 14:58:26 -0500213 }
214
Eric Barnes68286a42011-04-21 22:00:33 -0400215 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500216 {
217 $this->table->set_heading('Name', 'Color', 'Size');
218
219 // Make columns changes auto_heading
220 $rows = $this->table->make_columns(array(
221 'Laura', 'Red', '15',
222 'Katie', 'Blue'
223 ), 3);
224
225 foreach ($rows as $row)
226 {
227 $this->table->add_row($row);
228 }
229
230 $this->assertFalse($this->table->auto_heading);
231 $this->assertEquals(count($this->table->heading), 3);
232 $this->assertEquals(count($this->table->rows), 2);
233
234 $this->table->clear();
235
236 $this->assertTrue($this->table->auto_heading);
237 $this->assertEmpty($this->table->heading);
238 $this->assertEmpty($this->table->rows);
239 }
240
241
Eric Barnes68286a42011-04-21 22:00:33 -0400242 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500243 {
tiyowane39728c2012-03-10 02:10:44 +0400244 $reflectionOfTable = new ReflectionClass($this->table);
245 $method = $reflectionOfTable->getMethod('_set_from_array');
246
247 $method->setAccessible(true);
248
249 $this->assertFalse($method->invokeArgs($this->table, array('bogus')));
250 $this->assertFalse($method->invoke($this->table, array()));
Greg Aker57fb5182011-04-21 14:58:26 -0500251
252 $data = array(
253 array('name', 'color', 'number'),
254 array('Laura', 'Red', '22'),
255 array('Katie', 'Blue')
256 );
257
tiyowane39728c2012-03-10 02:10:44 +0400258 $method->invokeArgs($this->table, array($data, FALSE));
Greg Aker57fb5182011-04-21 14:58:26 -0500259 $this->assertEmpty($this->table->heading);
260
261 $this->table->clear();
262
263 $expected_heading = array(
264 array('data' => 'name'),
265 array('data' => 'color'),
266 array('data' => 'number')
267 );
268
269 $expected_second = array(
270 array('data' => 'Katie'),
271 array('data' => 'Blue'),
272 );
273
tiyowane39728c2012-03-10 02:10:44 +0400274 $method->invokeArgs($this->table, array($data));
Greg Aker57fb5182011-04-21 14:58:26 -0500275 $this->assertEquals(count($this->table->rows), 2);
276
277 $this->assertEquals(
278 $expected_heading,
279 $this->table->heading
280 );
281
282 $this->assertEquals(
283 $expected_second,
284 $this->table->rows[1]
285 );
286 }
287
Eric Barnes68286a42011-04-21 22:00:33 -0400288 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500289 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700290 $reflectionOfTable = new ReflectionClass($this->table);
291 $method = $reflectionOfTable->getMethod('_set_from_object');
292
293 $method->setAccessible(true);
294
295 // Make a stub of query instance
296 $query = new CI_TestCase();
297 $query->list_fields = function(){
298 return array('name', 'email');
299 };
300 $query->result_array = function(){
301 return array(
302 array('name' => 'John Doe', 'email' => 'john@doe.com'),
303 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
304 );
305 };
306 $query->num_rows = function(){
307 return 2;
308 };
309
310 $expected_heading = array(
311 array('data' => 'name'),
312 array('data' => 'email')
313 );
314
315 $expected_second = array(
316 'name' => array('data' => 'Foo Bar'),
317 'email' => array('data' => 'foo@bar.com'),
318 );
319
320 $method->invokeArgs($this->table, array($query));
321
322 $this->assertEquals(
323 $expected_heading,
324 $this->table->heading
325 );
326
327 $this->assertEquals(
328 $expected_second,
329 $this->table->rows[1]
330 );
Greg Aker57fb5182011-04-21 14:58:26 -0500331 }
332
333 // Test main generate method
334 // --------------------------------------------------------------------
335}