blob: f505a43fc824b7e1a6aa543bcc4197594b97b3a2 [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 /*
Andrey Andreev1b4e5e12014-02-13 01:14:28 +020037 * @depends test_prep_args
Greg Aker57fb5182011-04-21 14:58:26 -050038 */
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 /*
Andrey Andreev1b4e5e12014-02-13 01:14:28 +020058 * @depends test_prep_args
Greg Aker57fb5182011-04-21 14:58:26 -050059 */
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
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020070 $this->assertCount(3, $this->table->rows);
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);
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200191 $this->assertCount(3, $this->table->heading);
192 $this->assertCount(2, $this->table->rows);
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 {
Greg Aker57fb5182011-04-21 14:58:26 -0500203 $data = array(
204 array('name', 'color', 'number'),
205 array('Laura', 'Red', '22'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300206 array('Katie', 'Blue')
Greg Aker57fb5182011-04-21 14:58:26 -0500207 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300208
Andrey Andreev05983fc2014-02-11 16:51:43 +0200209 $this->table->auto_heading = FALSE;
210 $this->table->set_from_array($data);
Greg Aker57fb5182011-04-21 14:58:26 -0500211 $this->assertEmpty($this->table->heading);
Andrey Andreevc1862882012-06-09 23:16:58 +0300212
Greg Aker57fb5182011-04-21 14:58:26 -0500213 $this->table->clear();
Andrey Andreevc1862882012-06-09 23:16:58 +0300214
215 $this->table->set_from_array($data);
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200216 $this->assertCount(2, $this->table->rows);
Andrey Andreevc1862882012-06-09 23:16:58 +0300217
218 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500219 array('data' => 'name'),
220 array('data' => 'color'),
221 array('data' => 'number')
222 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300223
224 $this->assertEquals($expected, $this->table->heading);
225
226 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500227 array('data' => 'Katie'),
228 array('data' => 'Blue'),
229 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300230
231 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500232 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300233
234 public function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500235 {
Andrey Andreev05983fc2014-02-11 16:51:43 +0200236 // This needs to be passed by reference to CI_DB_result::__construct()
237 $dummy = new stdClass();
238 $dummy->conn_id = NULL;
239 $dummy->result_id = NULL;
Taufan Aditya8749bc72012-03-11 05:43:45 +0700240
Andrey Andreev05983fc2014-02-11 16:51:43 +0200241 $db_result = new DB_result_dummy($dummy);
242
243 $this->table->set_from_db_result($db_result);
Andrey Andreevc1862882012-06-09 23:16:58 +0300244
245 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700246 array('data' => 'name'),
247 array('data' => 'email')
248 );
249
Andrey Andreevc1862882012-06-09 23:16:58 +0300250 $this->assertEquals($expected, $this->table->heading);
251
252 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700253 'name' => array('data' => 'Foo Bar'),
254 'email' => array('data' => 'foo@bar.com'),
255 );
256
Andrey Andreevc1862882012-06-09 23:16:58 +0300257 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500258 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300259
260 public function test_generate()
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700261 {
262 // Prepare the data
263 $data = array(
264 array('Name', 'Color', 'Size'),
265 array('Fred', 'Blue', 'Small'),
266 array('Mary', 'Red', 'Large'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300267 array('John', 'Green', 'Medium')
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700268 );
269
270 $table = $this->table->generate($data);
271
272 // Test the table header
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200273 $this->assertContains('<th>Name</th>', $table);
274 $this->assertContains('<th>Color</th>', $table);
275 $this->assertContains('<th>Size</th>', $table);
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700276
277 // Test the first entry
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200278 $this->assertContains('<td>Fred</td>', $table);
279 $this->assertContains('<td>Blue</td>', $table);
280 $this->assertContains('<td>Small</td>', $table);
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700281 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300282
Andrey Andreev05983fc2014-02-11 16:51:43 +0200283}
284
285// We need this for the _set_from_db_result() test
286class DB_result_dummy extends CI_DB_result
287{
288 public function list_fields()
289 {
290 return array('name', 'email');
291 }
292
293 public function result_array()
294 {
295 return array(
296 array('name' => 'John Doe', 'email' => 'john@doe.com'),
297 array('name' => 'Foo Bar', 'email' => 'foo@bar.com')
298 );
299 }
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200300}