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() |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 109 | |
| 110 | $reflectionOfTable = new ReflectionClass($this->table); |
| 111 | $method = $reflectionOfTable->getMethod('_prep_args'); |
| 112 | |
| 113 | $method->setAccessible(true); |
| 114 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 115 | $this->assertEquals( |
| 116 | $expected, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 117 | $method->invokeArgs( |
| 118 | $this->table, array(array('name', 'color', 'size'), 'discreet') |
| 119 | ) |
| 120 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 121 | |
| 122 | // test what would be a single array argument. Again, nested |
| 123 | // due to func_get_args on calling methods |
| 124 | $this->assertEquals( |
| 125 | $expected, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 126 | $method->invokeArgs( |
| 127 | $this->table, array(array('name', 'color', 'size'), 'array') |
| 128 | ) |
| 129 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 130 | |
| 131 | |
| 132 | // with cell attributes |
| 133 | |
| 134 | // need to add that new argument row to our expected outcome |
| 135 | $expected[] = array('data' => 'weight', 'class' => 'awesome'); |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 136 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 137 | $this->assertEquals( |
| 138 | $expected, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 139 | $method->invokeArgs( |
| 140 | $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes') |
| 141 | ) |
| 142 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 145 | public function test_default_template_keys() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 146 | { |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 147 | $reflectionOfTable = new ReflectionClass($this->table); |
| 148 | $method = $reflectionOfTable->getMethod('_default_template'); |
| 149 | |
| 150 | $method->setAccessible(true); |
| 151 | |
| 152 | $deft_template = $method->invoke($this->table); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 153 | $keys = array( |
| 154 | 'table_open', |
| 155 | 'thead_open', 'thead_close', |
| 156 | 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', |
| 157 | 'tbody_open', 'tbody_close', |
| 158 | 'row_start', 'row_end', 'cell_start', 'cell_end', |
| 159 | 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', |
| 160 | 'table_close' |
| 161 | ); |
| 162 | |
| 163 | foreach ($keys as $key) |
| 164 | { |
| 165 | $this->assertArrayHasKey($key, $deft_template); |
| 166 | } |
| 167 | } |
| 168 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 169 | public function test_compile_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 170 | { |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 171 | $reflectionOfTable = new ReflectionClass($this->table); |
| 172 | $method = $reflectionOfTable->getMethod('_compile_template'); |
| 173 | |
| 174 | $method->setAccessible(true); |
| 175 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 176 | $this->assertFalse($this->table->set_template('invalid_junk')); |
| 177 | |
| 178 | // non default key |
| 179 | $this->table->set_template(array('nonsense' => 'foo')); |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 180 | $method->invoke($this->table); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 181 | |
| 182 | $this->assertArrayHasKey('nonsense', $this->table->template); |
| 183 | $this->assertEquals('foo', $this->table->template['nonsense']); |
| 184 | |
| 185 | // override default |
| 186 | $this->table->set_template(array('table_close' => '</table junk>')); |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 187 | $method->invoke($this->table); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 188 | |
| 189 | $this->assertArrayHasKey('table_close', $this->table->template); |
| 190 | $this->assertEquals('</table junk>', $this->table->template['table_close']); |
| 191 | } |
| 192 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 193 | public function test_make_columns() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 194 | { |
| 195 | // Test bogus parameters |
| 196 | $this->assertFalse($this->table->make_columns('invalid_junk')); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 197 | $this->assertFalse($this->table->make_columns(array())); |
| 198 | $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5')); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 199 | |
| 200 | |
| 201 | // Now on to the actual column creation |
| 202 | |
| 203 | $five_values = array( |
| 204 | 'Laura', 'Red', '15', |
| 205 | 'Katie', 'Blue' |
| 206 | ); |
| 207 | |
| 208 | // No column count - no changes to the array |
| 209 | $this->assertEquals( |
| 210 | $five_values, |
| 211 | $this->table->make_columns($five_values) |
| 212 | ); |
| 213 | |
| 214 | // Column count of 3 leaves us with one |
| 215 | $this->assertEquals( |
| 216 | array( |
| 217 | array('Laura', 'Red', '15'), |
| 218 | array('Katie', 'Blue', ' ') |
| 219 | ), |
| 220 | $this->table->make_columns($five_values, 3) |
| 221 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 224 | public function test_clear() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 225 | { |
| 226 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 227 | |
| 228 | // Make columns changes auto_heading |
| 229 | $rows = $this->table->make_columns(array( |
| 230 | 'Laura', 'Red', '15', |
| 231 | 'Katie', 'Blue' |
| 232 | ), 3); |
| 233 | |
| 234 | foreach ($rows as $row) |
| 235 | { |
| 236 | $this->table->add_row($row); |
| 237 | } |
| 238 | |
| 239 | $this->assertFalse($this->table->auto_heading); |
| 240 | $this->assertEquals(count($this->table->heading), 3); |
| 241 | $this->assertEquals(count($this->table->rows), 2); |
| 242 | |
| 243 | $this->table->clear(); |
| 244 | |
| 245 | $this->assertTrue($this->table->auto_heading); |
| 246 | $this->assertEmpty($this->table->heading); |
| 247 | $this->assertEmpty($this->table->rows); |
| 248 | } |
| 249 | |
| 250 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 251 | public function test_set_from_array() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 252 | { |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 253 | $reflectionOfTable = new ReflectionClass($this->table); |
| 254 | $method = $reflectionOfTable->getMethod('_set_from_array'); |
| 255 | |
| 256 | $method->setAccessible(true); |
| 257 | |
| 258 | $this->assertFalse($method->invokeArgs($this->table, array('bogus'))); |
| 259 | $this->assertFalse($method->invoke($this->table, array())); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 260 | |
| 261 | $data = array( |
| 262 | array('name', 'color', 'number'), |
| 263 | array('Laura', 'Red', '22'), |
| 264 | array('Katie', 'Blue') |
| 265 | ); |
| 266 | |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 267 | $method->invokeArgs($this->table, array($data, FALSE)); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 268 | $this->assertEmpty($this->table->heading); |
| 269 | |
| 270 | $this->table->clear(); |
| 271 | |
| 272 | $expected_heading = array( |
| 273 | array('data' => 'name'), |
| 274 | array('data' => 'color'), |
| 275 | array('data' => 'number') |
| 276 | ); |
| 277 | |
| 278 | $expected_second = array( |
| 279 | array('data' => 'Katie'), |
| 280 | array('data' => 'Blue'), |
| 281 | ); |
| 282 | |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 283 | $method->invokeArgs($this->table, array($data)); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 284 | $this->assertEquals(count($this->table->rows), 2); |
| 285 | |
| 286 | $this->assertEquals( |
| 287 | $expected_heading, |
| 288 | $this->table->heading |
| 289 | ); |
| 290 | |
| 291 | $this->assertEquals( |
| 292 | $expected_second, |
| 293 | $this->table->rows[1] |
| 294 | ); |
| 295 | } |
| 296 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 297 | function test_set_from_object() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 298 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 299 | $reflectionOfTable = new ReflectionClass($this->table); |
| 300 | $method = $reflectionOfTable->getMethod('_set_from_object'); |
| 301 | |
| 302 | $method->setAccessible(true); |
| 303 | |
| 304 | // Make a stub of query instance |
| 305 | $query = new CI_TestCase(); |
| 306 | $query->list_fields = function(){ |
| 307 | return array('name', 'email'); |
| 308 | }; |
| 309 | $query->result_array = function(){ |
| 310 | return array( |
| 311 | array('name' => 'John Doe', 'email' => 'john@doe.com'), |
| 312 | array('name' => 'Foo Bar', 'email' => 'foo@bar.com'), |
| 313 | ); |
| 314 | }; |
| 315 | $query->num_rows = function(){ |
| 316 | return 2; |
| 317 | }; |
| 318 | |
| 319 | $expected_heading = array( |
| 320 | array('data' => 'name'), |
| 321 | array('data' => 'email') |
| 322 | ); |
| 323 | |
| 324 | $expected_second = array( |
| 325 | 'name' => array('data' => 'Foo Bar'), |
| 326 | 'email' => array('data' => 'foo@bar.com'), |
| 327 | ); |
| 328 | |
| 329 | $method->invokeArgs($this->table, array($query)); |
| 330 | |
| 331 | $this->assertEquals( |
| 332 | $expected_heading, |
| 333 | $this->table->heading |
| 334 | ); |
| 335 | |
| 336 | $this->assertEquals( |
| 337 | $expected_second, |
| 338 | $this->table->rows[1] |
| 339 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | // Test main generate method |
| 343 | // -------------------------------------------------------------------- |
| 344 | } |