123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- <?php
- /**
- * Part of the Fuel framework.
- *
- * @package Fuel
- * @version 1.5
- * @author Fuel Development Team
- * @license MIT License
- * @copyright 2010 - 2013 Fuel Development Team
- * @link http://fuelphp.com
- */
- namespace Fuel\Core;
- /**
- * Arr class tests
- *
- * @group Core
- * @group Arr
- */
- class Test_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"
- ),
- ),
- ),
- );
- }
- public static function collection_provider()
- {
- $object = new \stdClass;
- $object->id = 7;
- $object->name = 'Bert';
- $object->surname = 'Visser';
- return array(
- array(
- array(
- array(
- 'id' => 2,
- 'name' => 'Bill',
- 'surname' => 'Cosby',
- ),
- array(
- 'id' => 5,
- 'name' => 'Chris',
- 'surname' => 'Rock',
- ),
- $object,
- ),
- ),
- );
- }
- /**
- * Test Arr::pluck()
- *
- * @test
- * @dataProvider collection_provider
- */
- public function test_pluck($collection)
- {
- $output = \Arr::pluck($collection, 'id');
- $expected = array(2, 5, 7);
- $this->assertEquals($expected, $output);
- }
- /**
- * Test Arr::pluck()
- *
- * @test
- * @dataProvider collection_provider
- */
- public function test_pluck_with_index($collection)
- {
- $output = \Arr::pluck($collection, 'name', 'id');
- $expected = array(2 => 'Bill', 5 => 'Chris', 7 => 'Bert');
- $this->assertEquals($expected, $output);
- }
- /**
- * 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::key_exists()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_key_exists_with_key_found($person)
- {
- $expected = true;
- $output = Arr::key_exists($person, "name");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::key_exists()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_key_exists_with_key_not_found($person)
- {
- $expected = false;
- $output = Arr::key_exists($person, "unknown");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::key_exists()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_key_exists_with_dot_separated_key($person)
- {
- $expected = true;
- $output = Arr::key_exists($person, "location.city");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_with_element_found($person)
- {
- $expected = "Jack";
- $output = Arr::get($person, "name", "Unknown Name");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_with_element_not_found($person)
- {
- $expected = "Unknown job";
- $output = Arr::get($person, "job", "Unknown job");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_with_dot_separated_key($person)
- {
- $expected = "Pittsburgh";
- $output = Arr::get($person, "location.city", "Unknown City");
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @expectedException InvalidArgumentException
- */
- public function test_get_throws_exception_when_array_is_not_an_array()
- {
- $output = Arr::get('Jack', 'name', 'Unknown Name');
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_when_dot_notated_key_is_not_array($person)
- {
- $expected = "Unknown Name";
- $output = Arr::get($person, 'foo.first', 'Unknown Name');
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_with_all_elements_found($person)
- {
- $expected = array(
- 'name' => 'Jack',
- 'weight' => 200,
- );
- $output = Arr::get($person, array('name', 'weight'), 'Unknown');
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_with_all_elements_not_found($person)
- {
- $expected = array(
- 'name' => 'Jack',
- 'height' => 'Unknown',
- );
- $output = Arr::get($person, array('name', 'height'), 'Unknown');
- $this->assertEquals($expected, $output);
- }
- /**
- * Tests Arr::get()
- *
- * @test
- * @dataProvider person_provider
- */
- public function test_get_when_keys_is_not_an_array($person)
- {
- $expected = 'Jack';
- $output = Arr::get($person, 'name', 'Unknown');
- $this->assertEquals($expected, $output);
- }
- /**
- * 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::filter_prefixed()
- *
- * @test
- */
- public function test_filter_prefixed()
- {
- $arr = array('foo' => 'baz', 'prefix_bar' => 'yay');
- $output = Arr::filter_prefixed($arr, 'prefix_');
- $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($expected, Arr::sort($data, 'info.pet.type', 'asc'));
- }
- /**
- * Tests Arr::sort()
- *
- * @test
- * @dataProvider sort_provider
- */
- public function test_sort_desc($data, $expected)
- {
- $expected = array_reverse($expected);
- $this->assertEquals($expected, Arr::sort($data, 'info.pet.type', 'desc'));
- }
- /**
- * Tests Arr::sort()
- *
- * @test
- * @dataProvider sort_provider
- * @expectedException InvalidArgumentException
- */
- public function test_sort_invalid_direction($data, $expected)
- {
- $this->assertEquals($expected, Arr::sort($data, 'info.pet.type', 'downer'));
- }
- public function test_sort_empty()
- {
- $expected = array();
- $output = Arr::sort(array(), 'test', 'test');
- $this->assertEquals($expected, $output);
- }
- /**
- * 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($expected, Arr::filter_keys($data, $keys));
- $this->assertEquals($expected_remove, Arr::filter_keys($data, $keys, true));
- }
- /**
- * 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
- * @expectedException BadMethodCallException
- */
- public function test_to_assoc_with_odd_number_of_elements()
- {
- $arr = array('foo', 'bar', 'baz');
- Arr::to_assoc($arr);
- }
- /**
- * Tests Arr::prepend()
- *
- * @test
- */
- public function test_prepend()
- {
- $arr = array(
- 'two' => 2,
- 'three' => 3,
- );
- $expected = array(
- 'one' => 1,
- 'two' => 2,
- 'three' => 3,
- );
- Arr::prepend($arr, 'one', 1);
- $this->assertEquals($expected, $arr);
- }
- /**
- * Tests Arr::prepend()
- *
- * @test
- */
- public function test_prepend_array()
- {
- $arr = array(
- 'two' => 2,
- 'three' => 3,
- );
- $expected = array(
- 'one' => 1,
- 'two' => 2,
- 'three' => 3,
- );
- Arr::prepend($arr, array('one' => 1));
- $this->assertEquals($expected, $arr);
- }
- /**
- * Tests Arr::is_multi()
- *
- * @test
- */
- public function test_multidimensional_array()
- {
- // Single array
- $arr_single = array('one' => 1, 'two' => 2);
- $this->assertFalse(Arr::is_multi($arr_single));
- // Multi-dimensional array
- $arr_multi = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => array('test' => 3));
- $this->assertTrue(Arr::is_multi($arr_multi));
- // Multi-dimensional array (not all elements are arrays)
- $arr_multi_strange = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => 3);
- $this->assertTrue(Arr::is_multi($arr_multi_strange, false));
- $this->assertFalse(Arr::is_multi($arr_multi_strange, true));
- }
- }
|