Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 502 lines (456 sloc) 9.316 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
<?php
/**
* Fuel is a fast, lightweight, community driven PHP5 framework.
*
* @package Fuel
* @version 1.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2011 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Core;

/**
* Arr class tests
*
* @group Core
* @group Arr
*/
class Tests_Arr extends TestCase {

public static function person_provider()
{
return array(
array(
array(
"name" => "Jack",
"age" => "21",
"weight" => 200,
"location" => array(
"city" => "Pittsburgh",
"state" => "PA",
"country" => "US"
),
),
),
);
}

/**
* Tests Arr::assoc_to_keyval()
*
* @test
*/
public function test_assoc_to_keyval()
{
$assoc = array(
array(
'color' => 'red',
'rank' => 4,
'name' => 'Apple',
),
array(
'color' => 'yellow',
'rank' => 3,
'name' => 'Banana',
),
array(
'color' => 'purple',
'rank' => 2,
'name' => 'Grape',
),
);

$expected = array(
'red' => 'Apple',
'yellow' => 'Banana',
'purple' => 'Grape',
);
$output = Arr::assoc_to_keyval($assoc, 'color', 'name');
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::element()
*
* @test
* @dataProvider person_provider
*/
public function test_element_with_element_found($person)
{
$expected = "Jack";
$output = Arr::element($person, "name", "Unknown Name");
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::element()
*
* @test
* @dataProvider person_provider
*/
public function test_element_with_element_not_found($person)
{
$expected = "Unknown job";
$output = Arr::element($person, "job", "Unknown job");
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::element()
*
* @test
* @dataProvider person_provider
*/
public function test_element_with_dot_separated_key($person)
{
$expected = "Pittsburgh";
$output = Arr::element($person, "location.city", "Unknown City");
$this->assertEquals($expected, $output);

}

/**
* Tests Arr::element()
*
* @test
*/
public function test_element_when_array_is_not_an_array()
{
$expected = "Unknown Name";
$output = Arr::element('Jack', 'name', 'Unknown Name');
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::element()
*
* @test
* @dataProvider person_provider
*/
public function test_element_when_dot_notated_key_is_not_array($person)
{
$expected = "Unknown Name";
$output = Arr::element($person, 'foo.first', 'Unknown Name');
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::elements()
*
* @test
* @dataProvider person_provider
*/
public function test_elements_with_all_elements_found($person)
{
$expected = array(
'name' => 'Jack',
'weight' => 200,
);
$output = Arr::elements($person, array('name', 'weight'), 'Unknown');
$this->assertEquals($expected, $output);
}


/**
* Tests Arr::elements()
*
* @test
* @dataProvider person_provider
*/
public function test_elements_with_all_elements_not_found($person)
{
$expected = array(
'name' => 'Jack',
'height' => 'Unknown',
);
$output = Arr::elements($person, array('name', 'height'), 'Unknown');
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::elements()
*
* @test
* @dataProvider person_provider
* @expectedException InvalidArgumentException
*/
public function test_elements_throws_exception_when_keys_is_not_an_array($person)
{
$output = Arr::elements($person, 'name', 'Unknown');
}

/**
* Tests Arr::flatten()
*
* @test
*/
public function test_flatten()
{
$indexed = array ( array('a'), array('b'), array('c') );

$expected = array(
"0_0" => "a",
"1_0" => "b",
"2_0" => "c",
);

$output = Arr::flatten($indexed, '_');
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::flatten_assoc()
*
* @test
*/
public function test_flatten_assoc()
{
$people = array(
array(
"name" => "Jack",
"age" => 21
),
array(
"name" => "Jill",
"age" => 23
)
);

$expected = array(
"0:name" => "Jack",
"0:age" => 21,
"1:name" => "Jill",
"1:age" => 23
);

$output = Arr::flatten_assoc($people);
$this->assertEquals($expected, $output);
}

/**
* Tests Arr::insert()
*
* @test
*/
public function test_insert()
{
$people = array("Jack", "Jill");

$expected = array("Humpty", "Jack", "Jill");
$output = Arr::insert($people, "Humpty", 0);

$this->assertEquals(true, $output);
$this->assertEquals($expected, $people);
}

/**
* Tests Arr::insert()
*
* @test
*/
public function test_insert_with_index_out_of_range()
{
$people = array("Jack", "Jill");

$output = Arr::insert($people, "Humpty", 4);

$this->assertFalse($output);
}

/**
* Tests Arr::insert_after_key()
*
* @test
*/
public function test_insert_after_key_that_exists()
{
$people = array("Jack", "Jill");

$expected = array("Jack", "Jill", "Humpty");
$output = Arr::insert_after_key($people, "Humpty", 1);

$this->assertTrue($output);
$this->assertEquals($expected, $people);
}

/**
* Tests Arr::insert_after_key()
*
* @test
*/
public function test_insert_after_key_that_does_not_exist()
{
$people = array("Jack", "Jill");
$output = Arr::insert_after_key($people, "Humpty", 6);
$this->assertFalse($output);
}

/**
* Tests Arr::insert_after_value()
*
* @test
*/
public function test_insert_after_value_that_exists()
{
$people = array("Jack", "Jill");
$expected = array("Jack", "Humpty", "Jill");
$output = Arr::insert_after_value($people, "Humpty", "Jack");
$this->assertTrue($output);
$this->assertEquals($expected, $people);
}

/**
* Tests Arr::insert_after_value()
*
* @test
*/
public function test_insert_after_value_that_does_not_exists()
{
$people = array("Jack", "Jill");
$output = Arr::insert_after_value($people, "Humpty", "Joe");
$this->assertFalse($output);
}

/**
* Tests Arr::average()
*
* @test
*/
public function test_average()
{
$arr = array(13, 8, 6);
$this->assertEquals(9, Arr::average($arr));
}

/**
* Tests Arr::average()
*
* @test
*/
public function test_average_of_empty_array()
{
$arr = array();
$this->assertEquals(0, Arr::average($arr));
}

/**
* Tests Arr::element()
*
* @test
*/
public function test_filter_prefixed()
{
$arr = array('foo' => 'baz', 'prefix_bar' => 'yay');

$output = Arr::filter_prefixed($arr);
$this->assertEquals(array('bar' => 'yay'), $output);
}

/**
* Tests Arr::sort()
*
* @test
* @expectedException InvalidArgumentException
*/
public function test_sort_of_non_array()
{
$sorted = Arr::sort('not an array', 'foo.key');
}

public function sort_provider()
{
return array(
array(
// Unsorted Array
array(
array(
'info' => array(
'pet' => array(
'type' => 'dog'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'fish'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'cat'
)
),
),
),

// Sorted Array
array(
array(
'info' => array(
'pet' => array(
'type' => 'cat'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'dog'
)
),
),
array(
'info' => array(
'pet' => array(
'type' => 'fish'
)
),
),
)
)
);
}

/**
* Tests Arr::sort()
*
* @test
* @dataProvider sort_provider
*/
public function test_sort_asc($data, $expected)
{
$this->assertEquals(Arr::sort($data, 'info.pet.type', 'asc'), $expected);
}

/**
* Tests Arr::sort()
*
* @test
* @dataProvider sort_provider
*/
public function test_sort_desc($data, $expected)
{
$expected = array_reverse($expected);
$this->assertEquals(Arr::sort($data, 'info.pet.type', 'desc'), $expected);
}

/**
* Tests Arr::sort()
*
* @test
* @dataProvider sort_provider
* @expectedException InvalidArgumentException
*/
public function test_sort_invalid_direction($data, $expected)
{
$this->assertEquals(Arr::sort($data, 'info.pet.type', 'downer'), $expected);
}

/**
* Tests Arr::filter_keys()
*
* @test
*/
public function test_filter_keys()
{
$data = array(
'epic' => 'win',
'weak' => 'sauce',
'foo' => 'bar'
);
$expected = array(
'epic' => 'win',
'foo' => 'bar'
);
$expected_remove = array(
'weak' => 'sauce',
);
$keys = array('epic', 'foo');
$this->assertEquals(Arr::filter_keys($data, $keys), $expected);
$this->assertEquals(Arr::filter_keys($data, $keys, true), $expected_remove);
}

/**
* Tests Arr::to_assoc()
*
* @test
*/
public function test_to_assoc_with_even_number_of_elements()
{
$arr = array('foo', 'bar', 'baz', 'yay');
$expected = array('foo' => 'bar', 'baz' => 'yay');
$this->assertEquals($expected, Arr::to_assoc($arr));
}

/**
* Tests Arr::to_assoc()
*
* @test
*/
public function test_to_assoc_with_odd_number_of_elements()
{
$arr = array('foo', 'bar', 'baz');
$expected = null;
$this->assertEquals($expected, Arr::to_assoc($arr));
}
}
Something went wrong with that request. Please try again.