blob: 04396d5fe231e3328b68ee6a4afcf131788c181e [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 {
162 $this->assertFalse($this->table->set_template('invalid_junk'));
163
164 // non default key
165 $this->table->set_template(array('nonsense' => 'foo'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700166 $this->table->compile_template();
Greg Aker57fb5182011-04-21 14:58:26 -0500167
168 $this->assertArrayHasKey('nonsense', $this->table->template);
169 $this->assertEquals('foo', $this->table->template['nonsense']);
170
171 // override default
172 $this->table->set_template(array('table_close' => '</table junk>'));
Taufan Adityad44d7202012-03-28 16:24:23 +0700173 $this->table->compile_template();
Greg Aker57fb5182011-04-21 14:58:26 -0500174
175 $this->assertArrayHasKey('table_close', $this->table->template);
176 $this->assertEquals('</table junk>', $this->table->template['table_close']);
177 }
178
Eric Barnes68286a42011-04-21 22:00:33 -0400179 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500180 {
181 // Test bogus parameters
182 $this->assertFalse($this->table->make_columns('invalid_junk'));
Taufan Aditya8749bc72012-03-11 05:43:45 +0700183 $this->assertFalse($this->table->make_columns(array()));
184 $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5'));
Greg Aker57fb5182011-04-21 14:58:26 -0500185
186
187 // Now on to the actual column creation
188
189 $five_values = array(
190 'Laura', 'Red', '15',
191 'Katie', 'Blue'
192 );
193
194 // No column count - no changes to the array
195 $this->assertEquals(
196 $five_values,
197 $this->table->make_columns($five_values)
198 );
199
200 // Column count of 3 leaves us with one &nbsp;
201 $this->assertEquals(
202 array(
203 array('Laura', 'Red', '15'),
204 array('Katie', 'Blue', '&nbsp;')
205 ),
206 $this->table->make_columns($five_values, 3)
207 );
Greg Aker57fb5182011-04-21 14:58:26 -0500208 }
209
Eric Barnes68286a42011-04-21 22:00:33 -0400210 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500211 {
212 $this->table->set_heading('Name', 'Color', 'Size');
213
214 // Make columns changes auto_heading
215 $rows = $this->table->make_columns(array(
216 'Laura', 'Red', '15',
217 'Katie', 'Blue'
218 ), 3);
219
220 foreach ($rows as $row)
221 {
222 $this->table->add_row($row);
223 }
224
225 $this->assertFalse($this->table->auto_heading);
226 $this->assertEquals(count($this->table->heading), 3);
227 $this->assertEquals(count($this->table->rows), 2);
228
229 $this->table->clear();
230
231 $this->assertTrue($this->table->auto_heading);
232 $this->assertEmpty($this->table->heading);
233 $this->assertEmpty($this->table->rows);
234 }
235
236
Eric Barnes68286a42011-04-21 22:00:33 -0400237 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500238 {
tiyowane39728c2012-03-10 02:10:44 +0400239 $reflectionOfTable = new ReflectionClass($this->table);
240 $method = $reflectionOfTable->getMethod('_set_from_array');
241
242 $method->setAccessible(true);
243
Taufan Adityad44d7202012-03-28 16:24:23 +0700244 $this->assertFalse($this->table->set_from_array('bogus'));
245 $this->assertFalse($this->table->set_from_array(NULL));
Greg Aker57fb5182011-04-21 14:58:26 -0500246
247 $data = array(
248 array('name', 'color', 'number'),
249 array('Laura', 'Red', '22'),
250 array('Katie', 'Blue')
251 );
252
Taufan Adityad44d7202012-03-28 16:24:23 +0700253 $this->table->set_from_array($data, FALSE);
Greg Aker57fb5182011-04-21 14:58:26 -0500254 $this->assertEmpty($this->table->heading);
255
256 $this->table->clear();
257
258 $expected_heading = array(
259 array('data' => 'name'),
260 array('data' => 'color'),
261 array('data' => 'number')
262 );
263
264 $expected_second = array(
265 array('data' => 'Katie'),
266 array('data' => 'Blue'),
267 );
268
Taufan Adityad44d7202012-03-28 16:24:23 +0700269 $this->table->set_from_array($data);
Greg Aker57fb5182011-04-21 14:58:26 -0500270 $this->assertEquals(count($this->table->rows), 2);
271
272 $this->assertEquals(
273 $expected_heading,
274 $this->table->heading
275 );
276
277 $this->assertEquals(
278 $expected_second,
279 $this->table->rows[1]
280 );
281 }
282
Eric Barnes68286a42011-04-21 22:00:33 -0400283 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500284 {
Taufan Aditya8749bc72012-03-11 05:43:45 +0700285 // Make a stub of query instance
286 $query = new CI_TestCase();
287 $query->list_fields = function(){
288 return array('name', 'email');
289 };
290 $query->result_array = function(){
291 return array(
292 array('name' => 'John Doe', 'email' => 'john@doe.com'),
293 array('name' => 'Foo Bar', 'email' => 'foo@bar.com'),
294 );
295 };
296 $query->num_rows = function(){
297 return 2;
298 };
299
300 $expected_heading = array(
301 array('data' => 'name'),
302 array('data' => 'email')
303 );
304
305 $expected_second = array(
306 'name' => array('data' => 'Foo Bar'),
307 'email' => array('data' => 'foo@bar.com'),
308 );
309
Taufan Adityad44d7202012-03-28 16:24:23 +0700310 $this->table->set_from_object($query);
Taufan Aditya8749bc72012-03-11 05:43:45 +0700311
312 $this->assertEquals(
313 $expected_heading,
314 $this->table->heading
315 );
316
317 $this->assertEquals(
318 $expected_second,
319 $this->table->rows[1]
320 );
Greg Aker57fb5182011-04-21 14:58:26 -0500321 }
322
323 // Test main generate method
324 // --------------------------------------------------------------------
325}