blob: ce04b6a6d24f6c0f86d6e62f3dd3101d40dcdc98 [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 {
dchill427ecc5cd2012-10-12 16:25:51 -04007 $this->table = new Mock_Libraries_Table();
8 $this->ci_instance_var('table', $this->table);
Greg Aker57fb5182011-04-21 14:58:26 -05009 }
10
Greg Aker57fb5182011-04-21 14:58:26 -050011 // Setter Methods
12 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030013
Eric Barnes68286a42011-04-21 22:00:33 -040014 public function test_set_template()
Greg Aker57fb5182011-04-21 14:58:26 -050015 {
16 $this->assertFalse($this->table->set_template('not an array'));
Andrey Andreevc1862882012-06-09 23:16:58 +030017
18 $template = array('a' => 'b');
19
Greg Aker57fb5182011-04-21 14:58:26 -050020 $this->table->set_template($template);
21 $this->assertEquals($template, $this->table->template);
22 }
Andrey Andreevc1862882012-06-09 23:16:58 +030023
Eric Barnes68286a42011-04-21 22:00:33 -040024 public function test_set_empty()
Greg Aker57fb5182011-04-21 14:58:26 -050025 {
26 $this->table->set_empty('nada');
27 $this->assertEquals('nada', $this->table->empty_cells);
28 }
Andrey Andreevc1862882012-06-09 23:16:58 +030029
Eric Barnes68286a42011-04-21 22:00:33 -040030 public function test_set_caption()
Greg Aker57fb5182011-04-21 14:58:26 -050031 {
32 $this->table->set_caption('awesome cap');
33 $this->assertEquals('awesome cap', $this->table->caption);
34 }
Andrey Andreevc1862882012-06-09 23:16:58 +030035
Greg Aker57fb5182011-04-21 14:58:26 -050036 /*
37 * @depends testPrepArgs
38 */
Eric Barnes68286a42011-04-21 22:00:33 -040039 public function test_set_heading()
Greg Aker57fb5182011-04-21 14:58:26 -050040 {
41 // uses _prep_args internally, so we'll just do a quick
42 // check to verify that func_get_args and prep_args are
43 // being called.
Andrey Andreevc1862882012-06-09 23:16:58 +030044
Greg Aker57fb5182011-04-21 14:58:26 -050045 $this->table->set_heading('name', 'color', 'size');
Andrey Andreevc1862882012-06-09 23:16:58 +030046
Greg Aker57fb5182011-04-21 14:58:26 -050047 $this->assertEquals(
48 array(
49 array('data' => 'name'),
50 array('data' => 'color'),
51 array('data' => 'size')
52 ),
53 $this->table->heading
54 );
55 }
Andrey Andreevc1862882012-06-09 23:16:58 +030056
Greg Aker57fb5182011-04-21 14:58:26 -050057 /*
58 * @depends testPrepArgs
59 */
Eric Barnes68286a42011-04-21 22:00:33 -040060 public function test_add_row()
Greg Aker57fb5182011-04-21 14:58:26 -050061 {
62 // uses _prep_args internally, so we'll just do a quick
63 // check to verify that func_get_args and prep_args are
64 // being called.
Andrey Andreevc1862882012-06-09 23:16:58 +030065
Greg Aker57fb5182011-04-21 14:58:26 -050066 $this->table->add_row('my', 'pony', 'sings');
67 $this->table->add_row('your', 'pony', 'stinks');
68 $this->table->add_row('my pony', '>', 'your pony');
Andrey Andreevc1862882012-06-09 23:16:58 +030069
Greg Aker57fb5182011-04-21 14:58:26 -050070 $this->assertEquals(count($this->table->rows), 3);
Andrey Andreevc1862882012-06-09 23:16:58 +030071
Greg Aker57fb5182011-04-21 14:58:26 -050072 $this->assertEquals(
73 array(
74 array('data' => 'your'),
75 array('data' => 'pony'),
76 array('data' => 'stinks')
77 ),
78 $this->table->rows[1]
79 );
80 }
Andrey Andreevc1862882012-06-09 23:16:58 +030081
Greg Aker57fb5182011-04-21 14:58:26 -050082 // Uility Methods
83 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030084
Eric Barnes68286a42011-04-21 22:00:33 -040085 public function test_prep_args()
Greg Aker57fb5182011-04-21 14:58:26 -050086 {
87 $expected = array(
88 array('data' => 'name'),
89 array('data' => 'color'),
90 array('data' => 'size')
91 );
Andrey Andreevc1862882012-06-09 23:16:58 +030092
Greg Aker57fb5182011-04-21 14:58:26 -050093 $this->assertEquals(
94 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +070095 $this->table->prep_args(array('name', 'color', 'size'))
tiyowane39728c2012-03-10 02:10:44 +040096 );
Taufan Aditya30b34d02012-03-28 16:35:48 +070097
Greg Aker57fb5182011-04-21 14:58:26 -050098 // with cell attributes
Greg Aker57fb5182011-04-21 14:58:26 -050099 // need to add that new argument row to our expected outcome
100 $expected[] = array('data' => 'weight', 'class' => 'awesome');
tiyowane39728c2012-03-10 02:10:44 +0400101
Greg Aker57fb5182011-04-21 14:58:26 -0500102 $this->assertEquals(
103 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +0700104 $this->table->prep_args(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')))
tiyowane39728c2012-03-10 02:10:44 +0400105 );
Greg Aker57fb5182011-04-21 14:58:26 -0500106 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300107
Eric Barnes68286a42011-04-21 22:00:33 -0400108 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500109 {
Greg Aker57fb5182011-04-21 14:58:26 -0500110 $keys = array(
111 'table_open',
112 'thead_open', 'thead_close',
113 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
114 'tbody_open', 'tbody_close',
115 'row_start', 'row_end', 'cell_start', 'cell_end',
116 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
117 'table_close'
118 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300119
Greg Aker57fb5182011-04-21 14:58:26 -0500120 foreach ($keys as $key)
121 {
Taufan Adityad61b7722012-03-28 16:07:58 +0700122 $this->assertArrayHasKey($key, $this->table->default_template());
Greg Aker57fb5182011-04-21 14:58:26 -0500123 }
124 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300125
Eric Barnes68286a42011-04-21 22:00:33 -0400126 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500127 {
128 $this->assertFalse($this->table->set_template('invalid_junk'));
Andrey Andreevc1862882012-06-09 23:16:58 +0300129
Greg Aker57fb5182011-04-21 14:58:26 -0500130 // non default key
131 $this->table->set_template(array('nonsense' => 'foo'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700132 $this->table->compile_template();
Andrey Andreevc1862882012-06-09 23:16:58 +0300133
Greg Aker57fb5182011-04-21 14:58:26 -0500134 $this->assertArrayHasKey('nonsense', $this->table->template);
135 $this->assertEquals('foo', $this->table->template['nonsense']);
Andrey Andreevc1862882012-06-09 23:16:58 +0300136
Greg Aker57fb5182011-04-21 14:58:26 -0500137 // override default
138 $this->table->set_template(array('table_close' => '</table junk>'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700139 $this->table->compile_template();
Andrey Andreevc1862882012-06-09 23:16:58 +0300140
Greg Aker57fb5182011-04-21 14:58:26 -0500141 $this->assertArrayHasKey('table_close', $this->table->template);
142 $this->assertEquals('</table junk>', $this->table->template['table_close']);
143 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300144
Eric Barnes68286a42011-04-21 22:00:33 -0400145 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500146 {
147 // Test bogus parameters
148 $this->assertFalse($this->table->make_columns('invalid_junk'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700149 $this->assertFalse($this->table->make_columns(array()));
150 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Andrey Andreevc1862882012-06-09 23:16:58 +0300151
Greg Aker57fb5182011-04-21 14:58:26 -0500152 // Now on to the actual column creation
Andrey Andreevc1862882012-06-09 23:16:58 +0300153
Greg Aker57fb5182011-04-21 14:58:26 -0500154 $five_values = array(
155 'Laura', 'Red', '15',
156 'Katie', 'Blue'
157 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300158
Greg Aker57fb5182011-04-21 14:58:26 -0500159 // No column count - no changes to the array
160 $this->assertEquals(
161 $five_values,
162 $this->table->make_columns($five_values)
163 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300164
Greg Aker57fb5182011-04-21 14:58:26 -0500165 // Column count of 3 leaves us with one &nbsp;
166 $this->assertEquals(
167 array(
168 array('Laura', 'Red', '15'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300169 array('Katie', 'Blue', '&nbsp;')
Greg Aker57fb5182011-04-21 14:58:26 -0500170 ),
171 $this->table->make_columns($five_values, 3)
172 );
Greg Aker57fb5182011-04-21 14:58:26 -0500173 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300174
Eric Barnes68286a42011-04-21 22:00:33 -0400175 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500176 {
177 $this->table->set_heading('Name', 'Color', 'Size');
Andrey Andreevc1862882012-06-09 23:16:58 +0300178
Greg Aker57fb5182011-04-21 14:58:26 -0500179 // Make columns changes auto_heading
180 $rows = $this->table->make_columns(array(
181 'Laura', 'Red', '15',
182 'Katie', 'Blue'
183 ), 3);
Andrey Andreevc1862882012-06-09 23:16:58 +0300184
Greg Aker57fb5182011-04-21 14:58:26 -0500185 foreach ($rows as $row)
186 {
187 $this->table->add_row($row);
188 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300189
Greg Aker57fb5182011-04-21 14:58:26 -0500190 $this->assertFalse($this->table->auto_heading);
191 $this->assertEquals(count($this->table->heading), 3);
192 $this->assertEquals(count($this->table->rows), 2);
Andrey Andreevc1862882012-06-09 23:16:58 +0300193
Greg Aker57fb5182011-04-21 14:58:26 -0500194 $this->table->clear();
Andrey Andreevc1862882012-06-09 23:16:58 +0300195
Greg Aker57fb5182011-04-21 14:58:26 -0500196 $this->assertTrue($this->table->auto_heading);
197 $this->assertEmpty($this->table->heading);
198 $this->assertEmpty($this->table->rows);
199 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300200
Eric Barnes68286a42011-04-21 22:00:33 -0400201 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500202 {
Taufan Adityad44d7202012-03-28 16:24:23 +0700203 $this->assertFalse($this->table->set_from_array('bogus'));
204 $this->assertFalse($this->table->set_from_array(NULL));
Andrey Andreevc1862882012-06-09 23:16:58 +0300205
Greg Aker57fb5182011-04-21 14:58:26 -0500206 $data = array(
207 array('name', 'color', 'number'),
208 array('Laura', 'Red', '22'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300209 array('Katie', 'Blue')
Greg Aker57fb5182011-04-21 14:58:26 -0500210 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300211
Taufan Adityad44d7202012-03-28 16:24:23 +0700212 $this->table->set_from_array($data, FALSE);
Greg Aker57fb5182011-04-21 14:58:26 -0500213 $this->assertEmpty($this->table->heading);
Andrey Andreevc1862882012-06-09 23:16:58 +0300214
Greg Aker57fb5182011-04-21 14:58:26 -0500215 $this->table->clear();
Andrey Andreevc1862882012-06-09 23:16:58 +0300216
217 $this->table->set_from_array($data);
218 $this->assertEquals(count($this->table->rows), 2);
219
220 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500221 array('data' => 'name'),
222 array('data' => 'color'),
223 array('data' => 'number')
224 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300225
226 $this->assertEquals($expected, $this->table->heading);
227
228 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500229 array('data' => 'Katie'),
230 array('data' => 'Blue'),
231 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300232
233 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500234 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300235
236 public function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500237 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700238 // Make a stub of query instance
239 $query = new CI_TestCase();
240 $query->list_fields = function(){
241 return array('name', 'email');
242 };
243 $query->result_array = function(){
244 return array(
245 array('name' => 'John Doe', 'email' => 'john@doe.com'),
246 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
247 );
248 };
249 $query->num_rows = function(){
250 return 2;
251 };
252
Andrey Andreevc1862882012-06-09 23:16:58 +0300253 $this->table->set_from_object($query);
254
255 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700256 array('data' => 'name'),
257 array('data' => 'email')
258 );
259
Andrey Andreevc1862882012-06-09 23:16:58 +0300260 $this->assertEquals($expected, $this->table->heading);
261
262 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700263 'name' => array('data' => 'Foo Bar'),
264 'email' => array('data' => 'foo@bar.com'),
265 );
266
Andrey Andreevc1862882012-06-09 23:16:58 +0300267 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500268 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300269
270 public function test_generate()
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700271 {
272 // Prepare the data
273 $data = array(
274 array('Name', 'Color', 'Size'),
275 array('Fred', 'Blue', 'Small'),
276 array('Mary', 'Red', 'Large'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300277 array('John', 'Green', 'Medium')
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700278 );
279
280 $table = $this->table->generate($data);
281
282 // Test the table header
283 $this->assertTrue(strpos($table, '<th>Name</th>') !== FALSE);
284 $this->assertTrue(strpos($table, '<th>Color</th>') !== FALSE);
285 $this->assertTrue(strpos($table, '<th>Size</th>') !== FALSE);
286
287 // Test the first entry
288 $this->assertTrue(strpos($table, '<td>Fred</td>') !== FALSE);
289 $this->assertTrue(strpos($table, '<td>Blue</td>') !== FALSE);
290 $this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE);
291 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300292
Greg Aker57fb5182011-04-21 14:58:26 -0500293}