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 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 103 | $this->assertEquals( |
| 104 | $expected, |
Taufan Aditya | 30b34d0 | 2012-03-28 16:35:48 +0700 | [diff] [blame^] | 105 | $this->table->prep_args(array('name', 'color', 'size')) |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 106 | ); |
Taufan Aditya | 30b34d0 | 2012-03-28 16:35:48 +0700 | [diff] [blame^] | 107 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 108 | // with cell attributes |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 109 | // need to add that new argument row to our expected outcome |
| 110 | $expected[] = array('data' => 'weight', 'class' => 'awesome'); |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 111 | |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 112 | $this->assertEquals( |
| 113 | $expected, |
Taufan Aditya | 30b34d0 | 2012-03-28 16:35:48 +0700 | [diff] [blame^] | 114 | $this->table->prep_args(array('name', 'color', 'size', array('data' => 'weight', 'class' => 'awesome'))) |
tiyowan | e39728c | 2012-03-10 02:10:44 +0400 | [diff] [blame] | 115 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 118 | public function test_default_template_keys() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 119 | { |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 120 | $keys = array( |
| 121 | 'table_open', |
| 122 | 'thead_open', 'thead_close', |
| 123 | 'heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', |
| 124 | 'tbody_open', 'tbody_close', |
| 125 | 'row_start', 'row_end', 'cell_start', 'cell_end', |
| 126 | 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', |
| 127 | 'table_close' |
| 128 | ); |
| 129 | |
| 130 | foreach ($keys as $key) |
| 131 | { |
Taufan Aditya | d61b772 | 2012-03-28 16:07:58 +0700 | [diff] [blame] | 132 | $this->assertArrayHasKey($key, $this->table->default_template()); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 136 | public function test_compile_template() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 137 | { |
| 138 | $this->assertFalse($this->table->set_template('invalid_junk')); |
| 139 | |
| 140 | // non default key |
| 141 | $this->table->set_template(array('nonsense' => 'foo')); |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 142 | $this->table->compile_template(); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 143 | |
| 144 | $this->assertArrayHasKey('nonsense', $this->table->template); |
| 145 | $this->assertEquals('foo', $this->table->template['nonsense']); |
| 146 | |
| 147 | // override default |
| 148 | $this->table->set_template(array('table_close' => '</table junk>')); |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 149 | $this->table->compile_template(); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 150 | |
| 151 | $this->assertArrayHasKey('table_close', $this->table->template); |
| 152 | $this->assertEquals('</table junk>', $this->table->template['table_close']); |
| 153 | } |
| 154 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 155 | public function test_make_columns() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 156 | { |
| 157 | // Test bogus parameters |
| 158 | $this->assertFalse($this->table->make_columns('invalid_junk')); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 159 | $this->assertFalse($this->table->make_columns(array())); |
| 160 | $this->assertFalse($this->table->make_columns(array('one', 'two'), '2.5')); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 161 | |
| 162 | |
| 163 | // Now on to the actual column creation |
| 164 | |
| 165 | $five_values = array( |
| 166 | 'Laura', 'Red', '15', |
| 167 | 'Katie', 'Blue' |
| 168 | ); |
| 169 | |
| 170 | // No column count - no changes to the array |
| 171 | $this->assertEquals( |
| 172 | $five_values, |
| 173 | $this->table->make_columns($five_values) |
| 174 | ); |
| 175 | |
| 176 | // Column count of 3 leaves us with one |
| 177 | $this->assertEquals( |
| 178 | array( |
| 179 | array('Laura', 'Red', '15'), |
| 180 | array('Katie', 'Blue', ' ') |
| 181 | ), |
| 182 | $this->table->make_columns($five_values, 3) |
| 183 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 186 | public function test_clear() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 187 | { |
| 188 | $this->table->set_heading('Name', 'Color', 'Size'); |
| 189 | |
| 190 | // Make columns changes auto_heading |
| 191 | $rows = $this->table->make_columns(array( |
| 192 | 'Laura', 'Red', '15', |
| 193 | 'Katie', 'Blue' |
| 194 | ), 3); |
| 195 | |
| 196 | foreach ($rows as $row) |
| 197 | { |
| 198 | $this->table->add_row($row); |
| 199 | } |
| 200 | |
| 201 | $this->assertFalse($this->table->auto_heading); |
| 202 | $this->assertEquals(count($this->table->heading), 3); |
| 203 | $this->assertEquals(count($this->table->rows), 2); |
| 204 | |
| 205 | $this->table->clear(); |
| 206 | |
| 207 | $this->assertTrue($this->table->auto_heading); |
| 208 | $this->assertEmpty($this->table->heading); |
| 209 | $this->assertEmpty($this->table->rows); |
| 210 | } |
| 211 | |
| 212 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 213 | public function test_set_from_array() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 214 | { |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 215 | $this->assertFalse($this->table->set_from_array('bogus')); |
| 216 | $this->assertFalse($this->table->set_from_array(NULL)); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 217 | |
| 218 | $data = array( |
| 219 | array('name', 'color', 'number'), |
| 220 | array('Laura', 'Red', '22'), |
| 221 | array('Katie', 'Blue') |
| 222 | ); |
| 223 | |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 224 | $this->table->set_from_array($data, FALSE); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 225 | $this->assertEmpty($this->table->heading); |
| 226 | |
| 227 | $this->table->clear(); |
| 228 | |
| 229 | $expected_heading = array( |
| 230 | array('data' => 'name'), |
| 231 | array('data' => 'color'), |
| 232 | array('data' => 'number') |
| 233 | ); |
| 234 | |
| 235 | $expected_second = array( |
| 236 | array('data' => 'Katie'), |
| 237 | array('data' => 'Blue'), |
| 238 | ); |
| 239 | |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 240 | $this->table->set_from_array($data); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 241 | $this->assertEquals(count($this->table->rows), 2); |
| 242 | |
| 243 | $this->assertEquals( |
| 244 | $expected_heading, |
| 245 | $this->table->heading |
| 246 | ); |
| 247 | |
| 248 | $this->assertEquals( |
| 249 | $expected_second, |
| 250 | $this->table->rows[1] |
| 251 | ); |
| 252 | } |
| 253 | |
Eric Barnes | 68286a4 | 2011-04-21 22:00:33 -0400 | [diff] [blame] | 254 | function test_set_from_object() |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 255 | { |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 256 | // Make a stub of query instance |
| 257 | $query = new CI_TestCase(); |
| 258 | $query->list_fields = function(){ |
| 259 | return array('name', 'email'); |
| 260 | }; |
| 261 | $query->result_array = function(){ |
| 262 | return array( |
| 263 | array('name' => 'John Doe', 'email' => 'john@doe.com'), |
| 264 | array('name' => 'Foo Bar', 'email' => 'foo@bar.com'), |
| 265 | ); |
| 266 | }; |
| 267 | $query->num_rows = function(){ |
| 268 | return 2; |
| 269 | }; |
| 270 | |
| 271 | $expected_heading = array( |
| 272 | array('data' => 'name'), |
| 273 | array('data' => 'email') |
| 274 | ); |
| 275 | |
| 276 | $expected_second = array( |
| 277 | 'name' => array('data' => 'Foo Bar'), |
| 278 | 'email' => array('data' => 'foo@bar.com'), |
| 279 | ); |
| 280 | |
Taufan Aditya | d44d720 | 2012-03-28 16:24:23 +0700 | [diff] [blame] | 281 | $this->table->set_from_object($query); |
Taufan Aditya | 8749bc7 | 2012-03-11 05:43:45 +0700 | [diff] [blame] | 282 | |
| 283 | $this->assertEquals( |
| 284 | $expected_heading, |
| 285 | $this->table->heading |
| 286 | ); |
| 287 | |
| 288 | $this->assertEquals( |
| 289 | $expected_second, |
| 290 | $this->table->rows[1] |
| 291 | ); |
Greg Aker | 57fb518 | 2011-04-21 14:58:26 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Test main generate method |
| 295 | // -------------------------------------------------------------------- |
| 296 | } |