blob: edfc83dd00d3bfd71d8b5c7959d68d1947554ed4 [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 {
Andrey Andreevc1862882012-06-09 23:16:58 +03007 $obj = new stdClass;
Taufan Adityaac5373a2012-03-28 16:03:38 +07008 $obj->table = new Mock_Libraries_Table();
Andrey Andreevc1862882012-06-09 23:16:58 +03009
Greg Aker57fb5182011-04-21 14:58:26 -050010 $this->ci_instance($obj);
Andrey Andreevc1862882012-06-09 23:16:58 +030011
Greg Aker57fb5182011-04-21 14:58:26 -050012 $this->table = $obj->table;
13 }
14
Greg Aker57fb5182011-04-21 14:58:26 -050015 // Setter Methods
16 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030017
Eric Barnes68286a42011-04-21 22:00:33 -040018 public function test_set_template()
Greg Aker57fb5182011-04-21 14:58:26 -050019 {
20 $this->assertFalse($this->table->set_template('not an array'));
Andrey Andreevc1862882012-06-09 23:16:58 +030021
22 $template = array('a' => 'b');
23
Greg Aker57fb5182011-04-21 14:58:26 -050024 $this->table->set_template($template);
25 $this->assertEquals($template, $this->table->template);
26 }
Andrey Andreevc1862882012-06-09 23:16:58 +030027
Eric Barnes68286a42011-04-21 22:00:33 -040028 public function test_set_empty()
Greg Aker57fb5182011-04-21 14:58:26 -050029 {
30 $this->table->set_empty('nada');
31 $this->assertEquals('nada', $this->table->empty_cells);
32 }
Andrey Andreevc1862882012-06-09 23:16:58 +030033
Eric Barnes68286a42011-04-21 22:00:33 -040034 public function test_set_caption()
Greg Aker57fb5182011-04-21 14:58:26 -050035 {
36 $this->table->set_caption('awesome cap');
37 $this->assertEquals('awesome cap', $this->table->caption);
38 }
Andrey Andreevc1862882012-06-09 23:16:58 +030039
Greg Aker57fb5182011-04-21 14:58:26 -050040 /*
41 * @depends testPrepArgs
42 */
Eric Barnes68286a42011-04-21 22:00:33 -040043 public function test_set_heading()
Greg Aker57fb5182011-04-21 14:58:26 -050044 {
45 // uses _prep_args internally, so we'll just do a quick
46 // check to verify that func_get_args and prep_args are
47 // being called.
Andrey Andreevc1862882012-06-09 23:16:58 +030048
Greg Aker57fb5182011-04-21 14:58:26 -050049 $this->table->set_heading('name', 'color', 'size');
Andrey Andreevc1862882012-06-09 23:16:58 +030050
Greg Aker57fb5182011-04-21 14:58:26 -050051 $this->assertEquals(
52 array(
53 array('data' => 'name'),
54 array('data' => 'color'),
55 array('data' => 'size')
56 ),
57 $this->table->heading
58 );
59 }
Andrey Andreevc1862882012-06-09 23:16:58 +030060
Greg Aker57fb5182011-04-21 14:58:26 -050061 /*
62 * @depends testPrepArgs
63 */
Eric Barnes68286a42011-04-21 22:00:33 -040064 public function test_add_row()
Greg Aker57fb5182011-04-21 14:58:26 -050065 {
66 // uses _prep_args internally, so we'll just do a quick
67 // check to verify that func_get_args and prep_args are
68 // being called.
Andrey Andreevc1862882012-06-09 23:16:58 +030069
Greg Aker57fb5182011-04-21 14:58:26 -050070 $this->table->add_row('my', 'pony', 'sings');
71 $this->table->add_row('your', 'pony', 'stinks');
72 $this->table->add_row('my pony', '>', 'your pony');
Andrey Andreevc1862882012-06-09 23:16:58 +030073
Greg Aker57fb5182011-04-21 14:58:26 -050074 $this->assertEquals(count($this->table->rows), 3);
Andrey Andreevc1862882012-06-09 23:16:58 +030075
Greg Aker57fb5182011-04-21 14:58:26 -050076 $this->assertEquals(
77 array(
78 array('data' => 'your'),
79 array('data' => 'pony'),
80 array('data' => 'stinks')
81 ),
82 $this->table->rows[1]
83 );
84 }
Andrey Andreevc1862882012-06-09 23:16:58 +030085
Greg Aker57fb5182011-04-21 14:58:26 -050086 // Uility Methods
87 // --------------------------------------------------------------------
Andrey Andreevc1862882012-06-09 23:16:58 +030088
Eric Barnes68286a42011-04-21 22:00:33 -040089 public function test_prep_args()
Greg Aker57fb5182011-04-21 14:58:26 -050090 {
91 $expected = array(
92 array('data' => 'name'),
93 array('data' => 'color'),
94 array('data' => 'size')
95 );
Andrey Andreevc1862882012-06-09 23:16:58 +030096
Greg Aker57fb5182011-04-21 14:58:26 -050097 $this->assertEquals(
98 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +070099 $this->table->prep_args(array('name', 'color', 'size'))
tiyowane39728c2012-03-10 02:10:44 +0400100 );
Taufan Aditya30b34d02012-03-28 16:35:48 +0700101
Greg Aker57fb5182011-04-21 14:58:26 -0500102 // with cell attributes
Greg Aker57fb5182011-04-21 14:58:26 -0500103 // need to add that new argument row to our expected outcome
104 $expected[] = array('data' => 'weight', 'class' => 'awesome');
tiyowane39728c2012-03-10 02:10:44 +0400105
Greg Aker57fb5182011-04-21 14:58:26 -0500106 $this->assertEquals(
107 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +0700108 $this->table->prep_args(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')))
tiyowane39728c2012-03-10 02:10:44 +0400109 );
Greg Aker57fb5182011-04-21 14:58:26 -0500110 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300111
Eric Barnes68286a42011-04-21 22:00:33 -0400112 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500113 {
Greg Aker57fb5182011-04-21 14:58:26 -0500114 $keys = array(
115 'table_open',
116 'thead_open', 'thead_close',
117 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
118 'tbody_open', 'tbody_close',
119 'row_start', 'row_end', 'cell_start', 'cell_end',
120 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
121 'table_close'
122 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300123
Greg Aker57fb5182011-04-21 14:58:26 -0500124 foreach ($keys as $key)
125 {
Taufan Adityad61b7722012-03-28 16:07:58 +0700126 $this->assertArrayHasKey($key, $this->table->default_template());
Greg Aker57fb5182011-04-21 14:58:26 -0500127 }
128 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300129
Eric Barnes68286a42011-04-21 22:00:33 -0400130 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500131 {
132 $this->assertFalse($this->table->set_template('invalid_junk'));
Andrey Andreevc1862882012-06-09 23:16:58 +0300133
Greg Aker57fb5182011-04-21 14:58:26 -0500134 // non default key
135 $this->table->set_template(array('nonsense' => 'foo'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700136 $this->table->compile_template();
Andrey Andreevc1862882012-06-09 23:16:58 +0300137
Greg Aker57fb5182011-04-21 14:58:26 -0500138 $this->assertArrayHasKey('nonsense', $this->table->template);
139 $this->assertEquals('foo', $this->table->template['nonsense']);
Andrey Andreevc1862882012-06-09 23:16:58 +0300140
Greg Aker57fb5182011-04-21 14:58:26 -0500141 // override default
142 $this->table->set_template(array('table_close' => '</table junk>'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700143 $this->table->compile_template();
Andrey Andreevc1862882012-06-09 23:16:58 +0300144
Greg Aker57fb5182011-04-21 14:58:26 -0500145 $this->assertArrayHasKey('table_close', $this->table->template);
146 $this->assertEquals('</table junk>', $this->table->template['table_close']);
147 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300148
Eric Barnes68286a42011-04-21 22:00:33 -0400149 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500150 {
151 // Test bogus parameters
152 $this->assertFalse($this->table->make_columns('invalid_junk'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700153 $this->assertFalse($this->table->make_columns(array()));
154 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Andrey Andreevc1862882012-06-09 23:16:58 +0300155
Greg Aker57fb5182011-04-21 14:58:26 -0500156 // Now on to the actual column creation
Andrey Andreevc1862882012-06-09 23:16:58 +0300157
Greg Aker57fb5182011-04-21 14:58:26 -0500158 $five_values = array(
159 'Laura', 'Red', '15',
160 'Katie', 'Blue'
161 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300162
Greg Aker57fb5182011-04-21 14:58:26 -0500163 // No column count - no changes to the array
164 $this->assertEquals(
165 $five_values,
166 $this->table->make_columns($five_values)
167 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300168
Greg Aker57fb5182011-04-21 14:58:26 -0500169 // Column count of 3 leaves us with one &nbsp;
170 $this->assertEquals(
171 array(
172 array('Laura', 'Red', '15'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300173 array('Katie', 'Blue', '&nbsp;')
Greg Aker57fb5182011-04-21 14:58:26 -0500174 ),
175 $this->table->make_columns($five_values, 3)
176 );
Greg Aker57fb5182011-04-21 14:58:26 -0500177 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300178
Eric Barnes68286a42011-04-21 22:00:33 -0400179 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500180 {
181 $this->table->set_heading('Name', 'Color', 'Size');
Andrey Andreevc1862882012-06-09 23:16:58 +0300182
Greg Aker57fb5182011-04-21 14:58:26 -0500183 // Make columns changes auto_heading
184 $rows = $this->table->make_columns(array(
185 'Laura', 'Red', '15',
186 'Katie', 'Blue'
187 ), 3);
Andrey Andreevc1862882012-06-09 23:16:58 +0300188
Greg Aker57fb5182011-04-21 14:58:26 -0500189 foreach ($rows as $row)
190 {
191 $this->table->add_row($row);
192 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300193
Greg Aker57fb5182011-04-21 14:58:26 -0500194 $this->assertFalse($this->table->auto_heading);
195 $this->assertEquals(count($this->table->heading), 3);
196 $this->assertEquals(count($this->table->rows), 2);
Andrey Andreevc1862882012-06-09 23:16:58 +0300197
Greg Aker57fb5182011-04-21 14:58:26 -0500198 $this->table->clear();
Andrey Andreevc1862882012-06-09 23:16:58 +0300199
Greg Aker57fb5182011-04-21 14:58:26 -0500200 $this->assertTrue($this->table->auto_heading);
201 $this->assertEmpty($this->table->heading);
202 $this->assertEmpty($this->table->rows);
203 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300204
Eric Barnes68286a42011-04-21 22:00:33 -0400205 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500206 {
Taufan Adityad44d7202012-03-28 16:24:23 +0700207 $this->assertFalse($this->table->set_from_array('bogus'));
208 $this->assertFalse($this->table->set_from_array(NULL));
Andrey Andreevc1862882012-06-09 23:16:58 +0300209
Greg Aker57fb5182011-04-21 14:58:26 -0500210 $data = array(
211 array('name', 'color', 'number'),
212 array('Laura', 'Red', '22'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300213 array('Katie', 'Blue')
Greg Aker57fb5182011-04-21 14:58:26 -0500214 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300215
Taufan Adityad44d7202012-03-28 16:24:23 +0700216 $this->table->set_from_array($data, FALSE);
Greg Aker57fb5182011-04-21 14:58:26 -0500217 $this->assertEmpty($this->table->heading);
Andrey Andreevc1862882012-06-09 23:16:58 +0300218
Greg Aker57fb5182011-04-21 14:58:26 -0500219 $this->table->clear();
Andrey Andreevc1862882012-06-09 23:16:58 +0300220
221 $this->table->set_from_array($data);
222 $this->assertEquals(count($this->table->rows), 2);
223
224 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500225 array('data' => 'name'),
226 array('data' => 'color'),
227 array('data' => 'number')
228 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300229
230 $this->assertEquals($expected, $this->table->heading);
231
232 $expected = array(
Greg Aker57fb5182011-04-21 14:58:26 -0500233 array('data' => 'Katie'),
234 array('data' => 'Blue'),
235 );
Andrey Andreevc1862882012-06-09 23:16:58 +0300236
237 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500238 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300239
240 public function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500241 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700242 // Make a stub of query instance
243 $query = new CI_TestCase();
244 $query->list_fields = function(){
245 return array('name', 'email');
246 };
247 $query->result_array = function(){
248 return array(
249 array('name' => 'John Doe', 'email' => 'john@doe.com'),
250 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
251 );
252 };
253 $query->num_rows = function(){
254 return 2;
255 };
256
Andrey Andreevc1862882012-06-09 23:16:58 +0300257 $this->table->set_from_object($query);
258
259 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700260 array('data' => 'name'),
261 array('data' => 'email')
262 );
263
Andrey Andreevc1862882012-06-09 23:16:58 +0300264 $this->assertEquals($expected, $this->table->heading);
265
266 $expected = array(
Taufan Aditya8749bc72012-03-11 05:43:45 +0700267 'name' => array('data' => 'Foo Bar'),
268 'email' => array('data' => 'foo@bar.com'),
269 );
270
Andrey Andreevc1862882012-06-09 23:16:58 +0300271 $this->assertEquals($expected, $this->table->rows[1]);
Greg Aker57fb5182011-04-21 14:58:26 -0500272 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300273
274 public function test_generate()
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700275 {
276 // Prepare the data
277 $data = array(
278 array('Name', 'Color', 'Size'),
279 array('Fred', 'Blue', 'Small'),
280 array('Mary', 'Red', 'Large'),
Andrey Andreevc1862882012-06-09 23:16:58 +0300281 array('John', 'Green', 'Medium')
Taufan Aditya6c7526c2012-05-27 13:51:27 +0700282 );
283
284 $table = $this->table->generate($data);
285
286 // Test the table header
287 $this->assertTrue(strpos($table, '<th>Name</th>') !== FALSE);
288 $this->assertTrue(strpos($table, '<th>Color</th>') !== FALSE);
289 $this->assertTrue(strpos($table, '<th>Size</th>') !== FALSE);
290
291 // Test the first entry
292 $this->assertTrue(strpos($table, '<td>Fred</td>') !== FALSE);
293 $this->assertTrue(strpos($table, '<td>Blue</td>') !== FALSE);
294 $this->assertTrue(strpos($table, '<td>Small</td>') !== FALSE);
295 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300296
Greg Aker57fb5182011-04-21 14:58:26 -0500297}