blob: 13f338c6bd1a4fead9777a6e6658f55be70ff66e [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
Greg Aker57fb5182011-04-21 14:58:26 -0500103 $this->assertEquals(
104 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +0700105 $this->table->prep_args(array('name', 'color', 'size'))
tiyowane39728c2012-03-10 02:10:44 +0400106 );
Taufan Aditya30b34d02012-03-28 16:35:48 +0700107
Greg Aker57fb5182011-04-21 14:58:26 -0500108 // with cell attributes
Greg Aker57fb5182011-04-21 14:58:26 -0500109 // need to add that new argument row to our expected outcome
110 $expected[] = array('data' => 'weight', 'class' => 'awesome');
tiyowane39728c2012-03-10 02:10:44 +0400111
Greg Aker57fb5182011-04-21 14:58:26 -0500112 $this->assertEquals(
113 $expected,
Taufan Aditya30b34d02012-03-28 16:35:48 +0700114 $this->table->prep_args(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')))
tiyowane39728c2012-03-10 02:10:44 +0400115 );
Greg Aker57fb5182011-04-21 14:58:26 -0500116 }
117
Eric Barnes68286a42011-04-21 22:00:33 -0400118 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500119 {
Greg Aker57fb5182011-04-21 14:58:26 -0500120 $keys = array(
121 'table_open',
122 'thead_open', 'thead_close',
123 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
124 'tbody_open', 'tbody_close',
125 'row_start', 'row_end', 'cell_start', 'cell_end',
126 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
127 'table_close'
128 );
129
130 foreach ($keys as $key)
131 {
Taufan Adityad61b7722012-03-28 16:07:58 +0700132 $this->assertArrayHasKey($key, $this->table->default_template());
Greg Aker57fb5182011-04-21 14:58:26 -0500133 }
134 }
135
Eric Barnes68286a42011-04-21 22:00:33 -0400136 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500137 {
138 $this->assertFalse($this->table->set_template('invalid_junk'));
139
140 // non default key
141 $this->table->set_template(array('nonsense' => 'foo'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700142 $this->table->compile_template();
Greg Aker57fb5182011-04-21 14:58:26 -0500143
144 $this->assertArrayHasKey('nonsense', $this->table->template);
145 $this->assertEquals('foo', $this->table->template['nonsense']);
146
147 // override default
148 $this->table->set_template(array('table_close' => '</table junk>'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700149 $this->table->compile_template();
Greg Aker57fb5182011-04-21 14:58:26 -0500150
151 $this->assertArrayHasKey('table_close', $this->table->template);
152 $this->assertEquals('</table junk>', $this->table->template['table_close']);
153 }
154
Eric Barnes68286a42011-04-21 22:00:33 -0400155 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500156 {
157 // Test bogus parameters
158 $this->assertFalse($this->table->make_columns('invalid_junk'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700159 $this->assertFalse($this->table->make_columns(array()));
160 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Greg Aker57fb5182011-04-21 14:58:26 -0500161
162
163 // Now on to the actual column creation
164
165 $five_values = array(
166 'Laura', 'Red', '15',
167 'Katie', 'Blue'
168 );
169
170 // No column count - no changes to the array
171 $this->assertEquals(
172 $five_values,
173 $this->table->make_columns($five_values)
174 );
175
176 // Column count of 3 leaves us with one &nbsp;
177 $this->assertEquals(
178 array(
179 array('Laura', 'Red', '15'),
180 array('Katie', 'Blue', '&nbsp;')
181 ),
182 $this->table->make_columns($five_values, 3)
183 );
Greg Aker57fb5182011-04-21 14:58:26 -0500184 }
185
Eric Barnes68286a42011-04-21 22:00:33 -0400186 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500187 {
188 $this->table->set_heading('Name', 'Color', 'Size');
189
190 // Make columns changes auto_heading
191 $rows = $this->table->make_columns(array(
192 'Laura', 'Red', '15',
193 'Katie', 'Blue'
194 ), 3);
195
196 foreach ($rows as $row)
197 {
198 $this->table->add_row($row);
199 }
200
201 $this->assertFalse($this->table->auto_heading);
202 $this->assertEquals(count($this->table->heading), 3);
203 $this->assertEquals(count($this->table->rows), 2);
204
205 $this->table->clear();
206
207 $this->assertTrue($this->table->auto_heading);
208 $this->assertEmpty($this->table->heading);
209 $this->assertEmpty($this->table->rows);
210 }
211
212
Eric Barnes68286a42011-04-21 22:00:33 -0400213 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500214 {
Taufan Adityad44d7202012-03-28 16:24:23 +0700215 $this->assertFalse($this->table->set_from_array('bogus'));
216 $this->assertFalse($this->table->set_from_array(NULL));
Greg Aker57fb5182011-04-21 14:58:26 -0500217
218 $data = array(
219 array('name', 'color', 'number'),
220 array('Laura', 'Red', '22'),
221 array('Katie', 'Blue')
222 );
223
Taufan Adityad44d7202012-03-28 16:24:23 +0700224 $this->table->set_from_array($data, FALSE);
Greg Aker57fb5182011-04-21 14:58:26 -0500225 $this->assertEmpty($this->table->heading);
226
227 $this->table->clear();
228
229 $expected_heading = array(
230 array('data' => 'name'),
231 array('data' => 'color'),
232 array('data' => 'number')
233 );
234
235 $expected_second = array(
236 array('data' => 'Katie'),
237 array('data' => 'Blue'),
238 );
239
Taufan Adityad44d7202012-03-28 16:24:23 +0700240 $this->table->set_from_array($data);
Greg Aker57fb5182011-04-21 14:58:26 -0500241 $this->assertEquals(count($this->table->rows), 2);
242
243 $this->assertEquals(
244 $expected_heading,
245 $this->table->heading
246 );
247
248 $this->assertEquals(
249 $expected_second,
250 $this->table->rows[1]
251 );
252 }
253
Eric Barnes68286a42011-04-21 22:00:33 -0400254 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500255 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700256 // Make a stub of query instance
257 $query = new CI_TestCase();
258 $query->list_fields = function(){
259 return array('name', 'email');
260 };
261 $query->result_array = function(){
262 return array(
263 array('name' => 'John Doe', 'email' => 'john@doe.com'),
264 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
265 );
266 };
267 $query->num_rows = function(){
268 return 2;
269 };
270
271 $expected_heading = array(
272 array('data' => 'name'),
273 array('data' => 'email')
274 );
275
276 $expected_second = array(
277 'name' => array('data' => 'Foo Bar'),
278 'email' => array('data' => 'foo@bar.com'),
279 );
280
Taufan Adityad44d7202012-03-28 16:24:23 +0700281 $this->table->set_from_object($query);
Taufan Aditya8749bc72012-03-11 05:43:45 +0700282
283 $this->assertEquals(
284 $expected_heading,
285 $this->table->heading
286 );
287
288 $this->assertEquals(
289 $expected_second,
290 $this->table->rows[1]
291 );
Greg Aker57fb5182011-04-21 14:58:26 -0500292 }
293
294 // Test main generate method
295 // --------------------------------------------------------------------
296}