arr.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Arr class tests
  15. *
  16. * @group Core
  17. * @group Arr
  18. */
  19. class Test_Arr extends TestCase
  20. {
  21. public static function person_provider()
  22. {
  23. return array(
  24. array(
  25. array(
  26. "name" => "Jack",
  27. "age" => "21",
  28. "weight" => 200,
  29. "location" => array(
  30. "city" => "Pittsburgh",
  31. "state" => "PA",
  32. "country" => "US"
  33. ),
  34. ),
  35. ),
  36. );
  37. }
  38. public static function collection_provider()
  39. {
  40. $object = new \stdClass;
  41. $object->id = 7;
  42. $object->name = 'Bert';
  43. $object->surname = 'Visser';
  44. return array(
  45. array(
  46. array(
  47. array(
  48. 'id' => 2,
  49. 'name' => 'Bill',
  50. 'surname' => 'Cosby',
  51. ),
  52. array(
  53. 'id' => 5,
  54. 'name' => 'Chris',
  55. 'surname' => 'Rock',
  56. ),
  57. $object,
  58. ),
  59. ),
  60. );
  61. }
  62. /**
  63. * Test Arr::pluck()
  64. *
  65. * @test
  66. * @dataProvider collection_provider
  67. */
  68. public function test_pluck($collection)
  69. {
  70. $output = \Arr::pluck($collection, 'id');
  71. $expected = array(2, 5, 7);
  72. $this->assertEquals($expected, $output);
  73. }
  74. /**
  75. * Test Arr::pluck()
  76. *
  77. * @test
  78. * @dataProvider collection_provider
  79. */
  80. public function test_pluck_with_index($collection)
  81. {
  82. $output = \Arr::pluck($collection, 'name', 'id');
  83. $expected = array(2 => 'Bill', 5 => 'Chris', 7 => 'Bert');
  84. $this->assertEquals($expected, $output);
  85. }
  86. /**
  87. * Tests Arr::assoc_to_keyval()
  88. *
  89. * @test
  90. */
  91. public function test_assoc_to_keyval()
  92. {
  93. $assoc = array(
  94. array(
  95. 'color' => 'red',
  96. 'rank' => 4,
  97. 'name' => 'Apple',
  98. ),
  99. array(
  100. 'color' => 'yellow',
  101. 'rank' => 3,
  102. 'name' => 'Banana',
  103. ),
  104. array(
  105. 'color' => 'purple',
  106. 'rank' => 2,
  107. 'name' => 'Grape',
  108. ),
  109. );
  110. $expected = array(
  111. 'red' => 'Apple',
  112. 'yellow' => 'Banana',
  113. 'purple' => 'Grape',
  114. );
  115. $output = Arr::assoc_to_keyval($assoc, 'color', 'name');
  116. $this->assertEquals($expected, $output);
  117. }
  118. /**
  119. * Tests Arr::key_exists()
  120. *
  121. * @test
  122. * @dataProvider person_provider
  123. */
  124. public function test_key_exists_with_key_found($person)
  125. {
  126. $expected = true;
  127. $output = Arr::key_exists($person, "name");
  128. $this->assertEquals($expected, $output);
  129. }
  130. /**
  131. * Tests Arr::key_exists()
  132. *
  133. * @test
  134. * @dataProvider person_provider
  135. */
  136. public function test_key_exists_with_key_not_found($person)
  137. {
  138. $expected = false;
  139. $output = Arr::key_exists($person, "unknown");
  140. $this->assertEquals($expected, $output);
  141. }
  142. /**
  143. * Tests Arr::key_exists()
  144. *
  145. * @test
  146. * @dataProvider person_provider
  147. */
  148. public function test_key_exists_with_dot_separated_key($person)
  149. {
  150. $expected = true;
  151. $output = Arr::key_exists($person, "location.city");
  152. $this->assertEquals($expected, $output);
  153. }
  154. /**
  155. * Tests Arr::get()
  156. *
  157. * @test
  158. * @dataProvider person_provider
  159. */
  160. public function test_get_with_element_found($person)
  161. {
  162. $expected = "Jack";
  163. $output = Arr::get($person, "name", "Unknown Name");
  164. $this->assertEquals($expected, $output);
  165. }
  166. /**
  167. * Tests Arr::get()
  168. *
  169. * @test
  170. * @dataProvider person_provider
  171. */
  172. public function test_get_with_element_not_found($person)
  173. {
  174. $expected = "Unknown job";
  175. $output = Arr::get($person, "job", "Unknown job");
  176. $this->assertEquals($expected, $output);
  177. }
  178. /**
  179. * Tests Arr::get()
  180. *
  181. * @test
  182. * @dataProvider person_provider
  183. */
  184. public function test_get_with_dot_separated_key($person)
  185. {
  186. $expected = "Pittsburgh";
  187. $output = Arr::get($person, "location.city", "Unknown City");
  188. $this->assertEquals($expected, $output);
  189. }
  190. /**
  191. * Tests Arr::get()
  192. *
  193. * @test
  194. * @expectedException InvalidArgumentException
  195. */
  196. public function test_get_throws_exception_when_array_is_not_an_array()
  197. {
  198. $output = Arr::get('Jack', 'name', 'Unknown Name');
  199. }
  200. /**
  201. * Tests Arr::get()
  202. *
  203. * @test
  204. * @dataProvider person_provider
  205. */
  206. public function test_get_when_dot_notated_key_is_not_array($person)
  207. {
  208. $expected = "Unknown Name";
  209. $output = Arr::get($person, 'foo.first', 'Unknown Name');
  210. $this->assertEquals($expected, $output);
  211. }
  212. /**
  213. * Tests Arr::get()
  214. *
  215. * @test
  216. * @dataProvider person_provider
  217. */
  218. public function test_get_with_all_elements_found($person)
  219. {
  220. $expected = array(
  221. 'name' => 'Jack',
  222. 'weight' => 200,
  223. );
  224. $output = Arr::get($person, array('name', 'weight'), 'Unknown');
  225. $this->assertEquals($expected, $output);
  226. }
  227. /**
  228. * Tests Arr::get()
  229. *
  230. * @test
  231. * @dataProvider person_provider
  232. */
  233. public function test_get_with_all_elements_not_found($person)
  234. {
  235. $expected = array(
  236. 'name' => 'Jack',
  237. 'height' => 'Unknown',
  238. );
  239. $output = Arr::get($person, array('name', 'height'), 'Unknown');
  240. $this->assertEquals($expected, $output);
  241. }
  242. /**
  243. * Tests Arr::get()
  244. *
  245. * @test
  246. * @dataProvider person_provider
  247. */
  248. public function test_get_when_keys_is_not_an_array($person)
  249. {
  250. $expected = 'Jack';
  251. $output = Arr::get($person, 'name', 'Unknown');
  252. $this->assertEquals($expected, $output);
  253. }
  254. /**
  255. * Tests Arr::flatten()
  256. *
  257. * @test
  258. */
  259. public function test_flatten()
  260. {
  261. $indexed = array ( array('a'), array('b'), array('c') );
  262. $expected = array(
  263. "0_0" => "a",
  264. "1_0" => "b",
  265. "2_0" => "c",
  266. );
  267. $output = Arr::flatten($indexed, '_');
  268. $this->assertEquals($expected, $output);
  269. }
  270. /**
  271. * Tests Arr::flatten_assoc()
  272. *
  273. * @test
  274. */
  275. public function test_flatten_assoc()
  276. {
  277. $people = array(
  278. array(
  279. "name" => "Jack",
  280. "age" => 21
  281. ),
  282. array(
  283. "name" => "Jill",
  284. "age" => 23
  285. )
  286. );
  287. $expected = array(
  288. "0:name" => "Jack",
  289. "0:age" => 21,
  290. "1:name" => "Jill",
  291. "1:age" => 23
  292. );
  293. $output = Arr::flatten_assoc($people);
  294. $this->assertEquals($expected, $output);
  295. }
  296. /**
  297. * Tests Arr::insert()
  298. *
  299. * @test
  300. */
  301. public function test_insert()
  302. {
  303. $people = array("Jack", "Jill");
  304. $expected = array("Humpty", "Jack", "Jill");
  305. $output = Arr::insert($people, "Humpty", 0);
  306. $this->assertEquals(true, $output);
  307. $this->assertEquals($expected, $people);
  308. }
  309. /**
  310. * Tests Arr::insert()
  311. *
  312. * @test
  313. */
  314. public function test_insert_with_index_out_of_range()
  315. {
  316. $people = array("Jack", "Jill");
  317. $output = Arr::insert($people, "Humpty", 4);
  318. $this->assertFalse($output);
  319. }
  320. /**
  321. * Tests Arr::insert_after_key()
  322. *
  323. * @test
  324. */
  325. public function test_insert_after_key_that_exists()
  326. {
  327. $people = array("Jack", "Jill");
  328. $expected = array("Jack", "Jill", "Humpty");
  329. $output = Arr::insert_after_key($people, "Humpty", 1);
  330. $this->assertTrue($output);
  331. $this->assertEquals($expected, $people);
  332. }
  333. /**
  334. * Tests Arr::insert_after_key()
  335. *
  336. * @test
  337. */
  338. public function test_insert_after_key_that_does_not_exist()
  339. {
  340. $people = array("Jack", "Jill");
  341. $output = Arr::insert_after_key($people, "Humpty", 6);
  342. $this->assertFalse($output);
  343. }
  344. /**
  345. * Tests Arr::insert_after_value()
  346. *
  347. * @test
  348. */
  349. public function test_insert_after_value_that_exists()
  350. {
  351. $people = array("Jack", "Jill");
  352. $expected = array("Jack", "Humpty", "Jill");
  353. $output = Arr::insert_after_value($people, "Humpty", "Jack");
  354. $this->assertTrue($output);
  355. $this->assertEquals($expected, $people);
  356. }
  357. /**
  358. * Tests Arr::insert_after_value()
  359. *
  360. * @test
  361. */
  362. public function test_insert_after_value_that_does_not_exists()
  363. {
  364. $people = array("Jack", "Jill");
  365. $output = Arr::insert_after_value($people, "Humpty", "Joe");
  366. $this->assertFalse($output);
  367. }
  368. /**
  369. * Tests Arr::average()
  370. *
  371. * @test
  372. */
  373. public function test_average()
  374. {
  375. $arr = array(13, 8, 6);
  376. $this->assertEquals(9, Arr::average($arr));
  377. }
  378. /**
  379. * Tests Arr::average()
  380. *
  381. * @test
  382. */
  383. public function test_average_of_empty_array()
  384. {
  385. $arr = array();
  386. $this->assertEquals(0, Arr::average($arr));
  387. }
  388. /**
  389. * Tests Arr::filter_prefixed()
  390. *
  391. * @test
  392. */
  393. public function test_filter_prefixed()
  394. {
  395. $arr = array('foo' => 'baz', 'prefix_bar' => 'yay');
  396. $output = Arr::filter_prefixed($arr, 'prefix_');
  397. $this->assertEquals(array('bar' => 'yay'), $output);
  398. }
  399. /**
  400. * Tests Arr::sort()
  401. *
  402. * @test
  403. * @expectedException InvalidArgumentException
  404. */
  405. public function test_sort_of_non_array()
  406. {
  407. $sorted = Arr::sort('not an array', 'foo.key');
  408. }
  409. public function sort_provider()
  410. {
  411. return array(
  412. array(
  413. // Unsorted Array
  414. array(
  415. array(
  416. 'info' => array(
  417. 'pet' => array(
  418. 'type' => 'dog'
  419. )
  420. ),
  421. ),
  422. array(
  423. 'info' => array(
  424. 'pet' => array(
  425. 'type' => 'fish'
  426. )
  427. ),
  428. ),
  429. array(
  430. 'info' => array(
  431. 'pet' => array(
  432. 'type' => 'cat'
  433. )
  434. ),
  435. ),
  436. ),
  437. // Sorted Array
  438. array(
  439. array(
  440. 'info' => array(
  441. 'pet' => array(
  442. 'type' => 'cat'
  443. )
  444. ),
  445. ),
  446. array(
  447. 'info' => array(
  448. 'pet' => array(
  449. 'type' => 'dog'
  450. )
  451. ),
  452. ),
  453. array(
  454. 'info' => array(
  455. 'pet' => array(
  456. 'type' => 'fish'
  457. )
  458. ),
  459. ),
  460. )
  461. )
  462. );
  463. }
  464. /**
  465. * Tests Arr::sort()
  466. *
  467. * @test
  468. * @dataProvider sort_provider
  469. */
  470. public function test_sort_asc($data, $expected)
  471. {
  472. $this->assertEquals($expected, Arr::sort($data, 'info.pet.type', 'asc'));
  473. }
  474. /**
  475. * Tests Arr::sort()
  476. *
  477. * @test
  478. * @dataProvider sort_provider
  479. */
  480. public function test_sort_desc($data, $expected)
  481. {
  482. $expected = array_reverse($expected);
  483. $this->assertEquals($expected, Arr::sort($data, 'info.pet.type', 'desc'));
  484. }
  485. /**
  486. * Tests Arr::sort()
  487. *
  488. * @test
  489. * @dataProvider sort_provider
  490. * @expectedException InvalidArgumentException
  491. */
  492. public function test_sort_invalid_direction($data, $expected)
  493. {
  494. $this->assertEquals($expected, Arr::sort($data, 'info.pet.type', 'downer'));
  495. }
  496. public function test_sort_empty()
  497. {
  498. $expected = array();
  499. $output = Arr::sort(array(), 'test', 'test');
  500. $this->assertEquals($expected, $output);
  501. }
  502. /**
  503. * Tests Arr::filter_keys()
  504. *
  505. * @test
  506. */
  507. public function test_filter_keys()
  508. {
  509. $data = array(
  510. 'epic' => 'win',
  511. 'weak' => 'sauce',
  512. 'foo' => 'bar'
  513. );
  514. $expected = array(
  515. 'epic' => 'win',
  516. 'foo' => 'bar'
  517. );
  518. $expected_remove = array(
  519. 'weak' => 'sauce',
  520. );
  521. $keys = array('epic', 'foo');
  522. $this->assertEquals($expected, Arr::filter_keys($data, $keys));
  523. $this->assertEquals($expected_remove, Arr::filter_keys($data, $keys, true));
  524. }
  525. /**
  526. * Tests Arr::to_assoc()
  527. *
  528. * @test
  529. */
  530. public function test_to_assoc_with_even_number_of_elements()
  531. {
  532. $arr = array('foo', 'bar', 'baz', 'yay');
  533. $expected = array('foo' => 'bar', 'baz' => 'yay');
  534. $this->assertEquals($expected, Arr::to_assoc($arr));
  535. }
  536. /**
  537. * Tests Arr::to_assoc()
  538. *
  539. * @test
  540. * @expectedException BadMethodCallException
  541. */
  542. public function test_to_assoc_with_odd_number_of_elements()
  543. {
  544. $arr = array('foo', 'bar', 'baz');
  545. Arr::to_assoc($arr);
  546. }
  547. /**
  548. * Tests Arr::prepend()
  549. *
  550. * @test
  551. */
  552. public function test_prepend()
  553. {
  554. $arr = array(
  555. 'two' => 2,
  556. 'three' => 3,
  557. );
  558. $expected = array(
  559. 'one' => 1,
  560. 'two' => 2,
  561. 'three' => 3,
  562. );
  563. Arr::prepend($arr, 'one', 1);
  564. $this->assertEquals($expected, $arr);
  565. }
  566. /**
  567. * Tests Arr::prepend()
  568. *
  569. * @test
  570. */
  571. public function test_prepend_array()
  572. {
  573. $arr = array(
  574. 'two' => 2,
  575. 'three' => 3,
  576. );
  577. $expected = array(
  578. 'one' => 1,
  579. 'two' => 2,
  580. 'three' => 3,
  581. );
  582. Arr::prepend($arr, array('one' => 1));
  583. $this->assertEquals($expected, $arr);
  584. }
  585. /**
  586. * Tests Arr::is_multi()
  587. *
  588. * @test
  589. */
  590. public function test_multidimensional_array()
  591. {
  592. // Single array
  593. $arr_single = array('one' => 1, 'two' => 2);
  594. $this->assertFalse(Arr::is_multi($arr_single));
  595. // Multi-dimensional array
  596. $arr_multi = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => array('test' => 3));
  597. $this->assertTrue(Arr::is_multi($arr_multi));
  598. // Multi-dimensional array (not all elements are arrays)
  599. $arr_multi_strange = array('one' => array('test' => 1), 'two' => array('test' => 2), 'three' => 3);
  600. $this->assertTrue(Arr::is_multi($arr_multi_strange, false));
  601. $this->assertFalse(Arr::is_multi($arr_multi_strange, true));
  602. }
  603. }