Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 1 | <?php |
| 2 | |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 3 | class Table_test extends CI_TestCase { |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 4 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 5 | public function set_up() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 6 | { |
| 7 | $obj = new StdClass; |
Taufan Aditya | ac5373a | 2012-03-28 16:03:38 +0700 | [diff] [blame] | 8 | $obj->table = new Mock_Libraries_Table(); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 9 | |
| 10 | $this->ci_instance($obj); |
| 11 | |
| 12 | $this->table = $obj->table; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | // Setter Methods |
| 17 | // -------------------------------------------------------------------- |
| 18 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 19 | public function test_set_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 20 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 31 | public function test_set_empty() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 32 | { |
| 33 | $this->table->set_empty('nada'); |
| 34 | $this->assertEquals('nada', $this->table->empty_cells); |
| 35 | } |
| 36 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 37 | public function test_set_caption() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 38 | { |
| 39 | $this->table->set_caption('awesome cap'); |
| 40 | $this->assertEquals('awesome cap', $this->table->caption); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * @depends testPrepArgs |
| 46 | */ |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 47 | public function test_set_heading() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 48 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 69 | public function test_add_row() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 70 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 95 | public function test_prep_args() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 96 | { |
| 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() |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 106 | |
| 107 | $reflectionOfTable = new ReflectionClass($this->table); |
| 108 | $method = $reflectionOfTable->getMethod('_prep_args'); |
| 109 | |
| 110 | $method->setAccessible(true); |
| 111 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 112 | $this->assertEquals( |
| 113 | $expected, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 114 | $method->invokeArgs( |
| 115 | $this->table, array(array('name', 'color', 'size'), 'discreet') |
| 116 | ) |
| 117 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 118 | |
| 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, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 123 | $method->invokeArgs( |
| 124 | $this->table, array(array('name', 'color', 'size'), 'array') |
| 125 | ) |
| 126 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 127 | |
| 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'); |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 133 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 134 | $this->assertEquals( |
| 135 | $expected, |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 136 | $method->invokeArgs( |
| 137 | $this->table, array(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome')), 'attributes') |
| 138 | ) |
| 139 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 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 | { |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 144 | $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 Aditya | d61b772 | 2012-03-28 16:07:58 +0700 | [diff] [blame] | 156 | $this->assertArrayHasKey($key, $this->table->default_template()); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 160 | public function test_compile_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 161 | { |
| 162 | $this->assertFalse($this->table->set_template('invalid_junk')); |
| 163 | |
| 164 | // non default key |
| 165 | $this->table->set_template(array('nonsense' => 'foo')); |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 166 | $this->table->compile_template(); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 167 | |
| 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 Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 173 | $this->table->compile_template(); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 174 | |
| 175 | $this->assertArrayHasKey('table_close', $this->table->template); |
| 176 | $this->assertEquals('</table junk>', $this->table->template['table_close']); |
| 177 | } |
| 178 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 179 | public function test_make_columns() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 180 | { |
| 181 | // Test bogus parameters |
| 182 | $this->assertFalse($this->table->make_columns('invalid_junk')); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 183 | $this->assertFalse($this->table->make_columns(array())); |
| 184 | $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5')); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 185 | |
| 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 |
| 201 | $this->assertEquals( |
| 202 | array( |
| 203 | array('Laura', 'Red', '15'), |
| 204 | array('Katie', 'Blue', ' ') |
| 205 | ), |
| 206 | $this->table->make_columns($five_values, 3) |
| 207 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 210 | public function test_clear() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 211 | { |
| 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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 237 | public function test_set_from_array() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 238 | { |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 239 | $reflectionOfTable = new ReflectionClass($this->table); |
| 240 | $method = $reflectionOfTable->getMethod('_set_from_array'); |
| 241 | |
| 242 | $method->setAccessible(true); |
| 243 | |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 244 | $this->assertFalse($this->table->set_from_array('bogus')); |
| 245 | $this->assertFalse($this->table->set_from_array(NULL)); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 246 | |
| 247 | $data = array( |
| 248 | array('name', 'color', 'number'), |
| 249 | array('Laura', 'Red', '22'), |
| 250 | array('Katie', 'Blue') |
| 251 | ); |
| 252 | |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 253 | $this->table->set_from_array($data, FALSE); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 254 | $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 Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 269 | $this->table->set_from_array($data); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 270 | $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 Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 283 | function test_set_from_object() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 284 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 285 | // 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 Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame^] | 310 | $this->table->set_from_object($query); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 311 | |
| 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 Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Test main generate method |
| 324 | // -------------------------------------------------------------------- |
| 325 | } |