blob: 133179f3a063e5cb9ba1a9b18d1318d2730608ed [file] [log] [blame]
Greg Aker57fb5182011-04-21 14:58:26 -05001<?php
2
3require BASEPATH.'libraries/Table.php';
4
5class Table_test extends CI_TestCase
6{
7
Eric Barnes68286a42011-04-21 22:00:33 -04008 public function set_up()
Greg Aker57fb5182011-04-21 14:58:26 -05009 {
10 $obj = new StdClass;
11 $obj->table = new CI_table();
12
13 $this->ci_instance($obj);
14
15 $this->table = $obj->table;
16 }
17
18
19 // Setter Methods
20 // --------------------------------------------------------------------
21
Eric Barnes68286a42011-04-21 22:00:33 -040022 public function test_set_template()
Greg Aker57fb5182011-04-21 14:58:26 -050023 {
24 $this->assertFalse($this->table->set_template('not an array'));
25
26 $template = array(
27 'a' => 'b'
28 );
29
30 $this->table->set_template($template);
31 $this->assertEquals($template, $this->table->template);
32 }
33
Eric Barnes68286a42011-04-21 22:00:33 -040034 public function test_set_empty()
Greg Aker57fb5182011-04-21 14:58:26 -050035 {
36 $this->table->set_empty('nada');
37 $this->assertEquals('nada', $this->table->empty_cells);
38 }
39
Eric Barnes68286a42011-04-21 22:00:33 -040040 public function test_set_caption()
Greg Aker57fb5182011-04-21 14:58:26 -050041 {
42 $this->table->set_caption('awesome cap');
43 $this->assertEquals('awesome cap', $this->table->caption);
44 }
45
46
47 /*
48 * @depends testPrepArgs
49 */
Eric Barnes68286a42011-04-21 22:00:33 -040050 public function test_set_heading()
Greg Aker57fb5182011-04-21 14:58:26 -050051 {
52 // uses _prep_args internally, so we'll just do a quick
53 // check to verify that func_get_args and prep_args are
54 // being called.
55
56 $this->table->set_heading('name', 'color', 'size');
57
58 $this->assertEquals(
59 array(
60 array('data' => 'name'),
61 array('data' => 'color'),
62 array('data' => 'size')
63 ),
64 $this->table->heading
65 );
66 }
67
68
69 /*
70 * @depends testPrepArgs
71 */
Eric Barnes68286a42011-04-21 22:00:33 -040072 public function test_add_row()
Greg Aker57fb5182011-04-21 14:58:26 -050073 {
74 // uses _prep_args internally, so we'll just do a quick
75 // check to verify that func_get_args and prep_args are
76 // being called.
77
78 $this->table->add_row('my', 'pony', 'sings');
79 $this->table->add_row('your', 'pony', 'stinks');
80 $this->table->add_row('my pony', '>', 'your pony');
81
82 $this->assertEquals(count($this->table->rows), 3);
83
84 $this->assertEquals(
85 array(
86 array('data' => 'your'),
87 array('data' => 'pony'),
88 array('data' => 'stinks')
89 ),
90 $this->table->rows[1]
91 );
92 }
93
94
95 // Uility Methods
96 // --------------------------------------------------------------------
97
Eric Barnes68286a42011-04-21 22:00:33 -040098 public function test_prep_args()
Greg Aker57fb5182011-04-21 14:58:26 -050099 {
100 $expected = array(
101 array('data' => 'name'),
102 array('data' => 'color'),
103 array('data' => 'size')
104 );
105
106 // test what would be discreet args,
107 // basically means a single array as the calling method
108 // will use func_get_args()
109 $this->assertEquals(
110 $expected,
111 $this->table->_prep_args(array(
112 'name', 'color', 'size'
113 )),
114 'discreet');
115
116
117 // test what would be a single array argument. Again, nested
118 // due to func_get_args on calling methods
119 $this->assertEquals(
120 $expected,
121 $this->table->_prep_args(array(
122 array('name', 'color', 'size')
123 )),
124 'array');
125
126
127 // with cell attributes
128
129 // need to add that new argument row to our expected outcome
130 $expected[] = array('data' => 'weight', 'class' => 'awesome');
131
132 $this->assertEquals(
133 $expected,
134 $this->table->_prep_args(array(
135 array('name', 'color', 'size',
136 array('data' => 'weight', 'class' => 'awesome')
137 )
138 )),
139 'attributes');
140 }
141
Eric Barnes68286a42011-04-21 22:00:33 -0400142 public function test_default_template_keys()
Greg Aker57fb5182011-04-21 14:58:26 -0500143 {
144 $deft_template = $this->table->_default_template();
145 $keys = array(
146 'table_open',
147 'thead_open', 'thead_close',
148 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end',
149 'tbody_open', 'tbody_close',
150 'row_start', 'row_end', 'cell_start', 'cell_end',
151 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end',
152 'table_close'
153 );
154
155 foreach ($keys as $key)
156 {
157 $this->assertArrayHasKey($key, $deft_template);
158 }
159 }
160
Eric Barnes68286a42011-04-21 22:00:33 -0400161 public function test_compile_template()
Greg Aker57fb5182011-04-21 14:58:26 -0500162 {
163 $this->assertFalse($this->table->set_template('invalid_junk'));
164
165 // non default key
166 $this->table->set_template(array('nonsense' => 'foo'));
167 $this->table->_compile_template();
168
169 $this->assertArrayHasKey('nonsense', $this->table->template);
170 $this->assertEquals('foo', $this->table->template['nonsense']);
171
172 // override default
173 $this->table->set_template(array('table_close' => '</table junk>'));
174 $this->table->_compile_template();
175
176 $this->assertArrayHasKey('table_close', $this->table->template);
177 $this->assertEquals('</table junk>', $this->table->template['table_close']);
178 }
179
Eric Barnes68286a42011-04-21 22:00:33 -0400180 public function test_make_columns()
Greg Aker57fb5182011-04-21 14:58:26 -0500181 {
182 // Test bogus parameters
183 $this->assertFalse($this->table->make_columns('invalid_junk'));
184 $this->assertFalse( $this->table->make_columns(array()));
185 // $this->assertFalse(
186 // $this->table->make_columns(array('one', 'two')),
187 // '2.5' // not an integer!
188 // );
189
190
191 // Now on to the actual column creation
192
193 $five_values = array(
194 'Laura', 'Red', '15',
195 'Katie', 'Blue'
196 );
197
198 // No column count - no changes to the array
199 $this->assertEquals(
200 $five_values,
201 $this->table->make_columns($five_values)
202 );
203
204 // Column count of 3 leaves us with one &nbsp;
205 $this->assertEquals(
206 array(
207 array('Laura', 'Red', '15'),
208 array('Katie', 'Blue', '&nbsp;')
209 ),
210 $this->table->make_columns($five_values, 3)
211 );
212
213 $this->markTestSkipped('Look at commented assertFalse above');
214 }
215
Eric Barnes68286a42011-04-21 22:00:33 -0400216 public function test_clear()
Greg Aker57fb5182011-04-21 14:58:26 -0500217 {
218 $this->table->set_heading('Name', 'Color', 'Size');
219
220 // Make columns changes auto_heading
221 $rows = $this->table->make_columns(array(
222 'Laura', 'Red', '15',
223 'Katie', 'Blue'
224 ), 3);
225
226 foreach ($rows as $row)
227 {
228 $this->table->add_row($row);
229 }
230
231 $this->assertFalse($this->table->auto_heading);
232 $this->assertEquals(count($this->table->heading), 3);
233 $this->assertEquals(count($this->table->rows), 2);
234
235 $this->table->clear();
236
237 $this->assertTrue($this->table->auto_heading);
238 $this->assertEmpty($this->table->heading);
239 $this->assertEmpty($this->table->rows);
240 }
241
242
Eric Barnes68286a42011-04-21 22:00:33 -0400243 public function test_set_from_array()
Greg Aker57fb5182011-04-21 14:58:26 -0500244 {
245 $this->assertFalse($this->table->_set_from_array('bogus'));
246 $this->assertFalse($this->table->_set_from_array(array()));
247
248 $data = array(
249 array('name', 'color', 'number'),
250 array('Laura', 'Red', '22'),
251 array('Katie', 'Blue')
252 );
253
254 $this->table->_set_from_array($data, FALSE);
255 $this->assertEmpty($this->table->heading);
256
257 $this->table->clear();
258
259 $expected_heading = array(
260 array('data' => 'name'),
261 array('data' => 'color'),
262 array('data' => 'number')
263 );
264
265 $expected_second = array(
266 array('data' => 'Katie'),
267 array('data' => 'Blue'),
268 );
269
270 $this->table->_set_from_array($data);
271 $this->assertEquals(count($this->table->rows), 2);
272
273 $this->assertEquals(
274 $expected_heading,
275 $this->table->heading
276 );
277
278 $this->assertEquals(
279 $expected_second,
280 $this->table->rows[1]
281 );
282 }
283
Eric Barnes68286a42011-04-21 22:00:33 -0400284 function test_set_from_object()
Greg Aker57fb5182011-04-21 14:58:26 -0500285 {
286 $this->markTestSkipped('Not yet implemented.');
287 }
288
289 // Test main generate method
290 // --------------------------------------------------------------------
291}