Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | require BASEPATH.'libraries/Table.php'; |
| 4 | |
| 5 | class Table_test extends CI_TestCase |
| 6 | { |
| 7 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 8 | public function set_up() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 9 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 22 | public function test_set_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 23 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 34 | public function test_set_empty() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 35 | { |
| 36 | $this->table->set_empty('nada'); |
| 37 | $this->assertEquals('nada', $this->table->empty_cells); |
| 38 | } |
| 39 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 40 | public function test_set_caption() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 41 | { |
| 42 | $this->table->set_caption('awesome cap'); |
| 43 | $this->assertEquals('awesome cap', $this->table->caption); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * @depends testPrepArgs |
| 49 | */ |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 50 | public function test_set_heading() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 51 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 72 | public function test_add_row() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 73 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 98 | public function test_prep_args() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 99 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 142 | public function test_default_template_keys() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 143 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 161 | public function test_compile_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 162 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 180 | public function test_make_columns() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 181 | { |
| 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 |
| 205 | $this->assertEquals( |
| 206 | array( |
| 207 | array('Laura', 'Red', '15'), |
| 208 | array('Katie', 'Blue', ' ') |
| 209 | ), |
| 210 | $this->table->make_columns($five_values, 3) |
| 211 | ); |
| 212 | |
| 213 | $this->markTestSkipped('Look at commented assertFalse above'); |
| 214 | } |
| 215 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 216 | public function test_clear() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 217 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 243 | public function test_set_from_array() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 244 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 284 | function test_set_from_object() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 285 | { |
| 286 | $this->markTestSkipped('Not yet implemented.'); |
| 287 | } |
| 288 | |
| 289 | // Test main generate method |
| 290 | // -------------------------------------------------------------------- |
| 291 | } |