/modules/phpunit-1.0/tests/Helper_Arr_Test.php

https://github.com/robertleeplummerjr/bluebox · PHP · 349 lines · 224 code · 31 blank · 94 comment · 0 complexity · 919c4640680140e24549afc8cee90cdf MD5 · raw file

  1. <?php
  2. /**
  3. * Arr Helper Unit Tests
  4. *
  5. * @package Core
  6. * @subpackage Helpers
  7. * @author Kiall Mac Innes
  8. * @group core
  9. * @group core.helpers
  10. * @group core.helpers.arr
  11. */
  12. class Helper_Arr_Test extends PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * DataProvider for the arr::rotate() test
  16. */
  17. public function rotate_provider()
  18. {
  19. return array(
  20. array(
  21. array(
  22. 'CD' => array('700', '780'),
  23. 'DVD' => array('4700','650'),
  24. 'BD' => array('25000','405')
  25. ),
  26. array(
  27. 0 => array('CD' => 700, 'DVD' => 4700, 'BD' => 25000),
  28. 1 => array('CD' => 780, 'DVD' => 650, 'BD' => 405),
  29. ),
  30. )
  31. );
  32. }
  33. /**
  34. * Tests the arr::rotate() function.
  35. * @dataProvider rotate_provider
  36. * @group core.helpers.arr.rotate
  37. * @test
  38. */
  39. public function rotate($input_array, $expected_result)
  40. {
  41. $result = arr::rotate($input_array);
  42. $this->assertEquals($expected_result, $result);
  43. }
  44. /**
  45. * DataProvider for the arr::remove() test
  46. */
  47. public function remove_provider()
  48. {
  49. return array(
  50. array(
  51. 'CD',
  52. array(
  53. 'CD' => array('700', '780'),
  54. 'DVD' => array('4700','650'),
  55. 'BD' => array('25000','405')
  56. ),
  57. array(
  58. 'DVD' => array('4700','650'),
  59. 'BD' => array('25000','405')
  60. ),
  61. array('700', '780')
  62. )
  63. );
  64. }
  65. /**
  66. * Tests the arr::remove() function.
  67. * @dataProvider remove_provider
  68. * @group core.helpers.arr.remove
  69. * @test
  70. */
  71. public function remove($input_key, $input_array, $expected_result, $expected_result2)
  72. {
  73. $result = arr::remove($input_key, $input_array);
  74. $this->assertEquals($expected_result, $input_array);
  75. $this->assertEquals($expected_result2, $result);
  76. }
  77. /**
  78. * DataProvider for the arr::callback_string() test
  79. */
  80. public function callback_string_provider()
  81. {
  82. return array(
  83. array('trim', array('trim', NULL)),
  84. array('valid::digit', array('valid::digit', NULL)),
  85. array('limit[10]', array('limit', array('10'))),
  86. array('limit[10,20]', array('limit', array('10','20'))),
  87. array('chars[a,b,c,d]', array('chars', array('a','b','c','d'))),
  88. );
  89. }
  90. /**
  91. * Tests the arr::callback_string() function.
  92. * @dataProvider callback_string_provider
  93. * @group core.helpers.arr.callback_string
  94. * @test
  95. */
  96. public function callback_string($input_str, $expected_result)
  97. {
  98. $result = arr::callback_string($input_str);
  99. $this->assertEquals($expected_result, $result);
  100. }
  101. /**
  102. * DataProvider for the arr::extract() test
  103. */
  104. public function extract_provider()
  105. {
  106. return array(
  107. array(
  108. array(
  109. 'CD' => array('700', '780'),
  110. 'DVD' => array('4700','650'),
  111. 'BD' => array('25000','405')
  112. ),
  113. array(
  114. 'DVD' => array('4700','650'),
  115. 'Blueray' => NULL
  116. ),
  117. 'DVD',
  118. 'Blueray'
  119. ),
  120. array(
  121. array(
  122. 'CD' => array('700', '780'),
  123. 'DVD' => array('4700','650'),
  124. 'BD' => array('25000','405')
  125. ),
  126. array(
  127. 'DVD' => array('4700','650'),
  128. 'BD' => array('25000','405')
  129. ),
  130. 'DVD',
  131. 'BD'
  132. )
  133. );
  134. }
  135. /**
  136. * Tests the arr::extract() function.
  137. * @dataProvider extract_provider
  138. * @group core.helpers.arr.extract
  139. * @test
  140. */
  141. public function extract($input_array, $expected_result, $input_keys)
  142. {
  143. $args = array_slice(func_get_args(), 2);
  144. array_unshift($args, $input_array);
  145. $result = call_user_func_array('arr::extract', $args);
  146. $this->assertEquals($expected_result, $result);
  147. }
  148. /**
  149. * DataProvider for the arr::merge() test
  150. */
  151. public function merge_provider()
  152. {
  153. return array(
  154. array(
  155. array(
  156. 0 => 'a',
  157. 1 => 'b',
  158. 2 => 'c',
  159. 3 => 'd',
  160. 'e' => array(
  161. 0 => 'f',
  162. 1 => 'g'
  163. )
  164. ),
  165. array('a', 'b'),
  166. array('c', 'd'),
  167. array('e' => array('f', 'g'))
  168. ),
  169. array(
  170. array(
  171. 'a' => 'E',
  172. 'b' => 'F',
  173. 'c' => array('d' => 'G'),
  174. ),
  175. array('a' => 'A'),
  176. array('b' => 'B', 'c' => array('d' => 'D')),
  177. array('a' => 'E', 'b' => 'F', 'c' => array('d' => 'G'))
  178. ),
  179. );
  180. }
  181. /**
  182. * Tests the arr::merge() function.
  183. * @dataProvider merge_provider
  184. * @group core.helpers.arr.merge
  185. * @test
  186. */
  187. public function merge($expected_result, $input_keys)
  188. {
  189. $args = array_slice(func_get_args(), 1);
  190. $result = call_user_func_array('arr::merge', $args);
  191. $this->assertEquals($expected_result, $result);
  192. }
  193. /**
  194. * DataProvider for the arr::overwrite() test
  195. */
  196. public function overwrite_provider()
  197. {
  198. return array(
  199. array(
  200. array('fruit1' => 'strawberry', 'fruit2' => 'kiwi', 'fruit3' => 'pineapple'),
  201. array('fruit1' => 'apple', 'fruit2' => 'mango', 'fruit3' => 'pineapple'),
  202. array('fruit1' => 'strawberry', 'fruit4' => 'coconut'),
  203. array('fruit2' => 'kiwi', 'fruit5' => 'papaya'),
  204. ),
  205. );
  206. }
  207. /**
  208. * Tests the arr::overwrite() function.
  209. * @dataProvider overwrite_provider
  210. * @group core.helpers.arr.overwrite
  211. * @test
  212. */
  213. public function overwrite($expected_result, $input_array1)
  214. {
  215. $args = array_slice(func_get_args(), 1);
  216. $result = call_user_func_array('arr::overwrite', $args);
  217. $this->assertEquals($expected_result, $result);
  218. }
  219. /**
  220. * DataProvider for the arr::map_recursive() test
  221. */
  222. public function map_recursive_provider()
  223. {
  224. return array(
  225. array(
  226. array($this, 'map_recursive_callback'),
  227. array('a' => 1, 'b' => 2, 'c' => array(3, 4), 'd' => array('e' => 5)),
  228. array(
  229. 'a' => 2,
  230. 'b' => 3,
  231. 'c' => array(
  232. 0 => 4,
  233. 1 => 5,
  234. ),
  235. 'd' => array(
  236. 'e' => 6
  237. ),
  238. )
  239. ),
  240. );
  241. }
  242. /**
  243. * Test callback for the arr::map_recursive() test
  244. */
  245. public function map_recursive_callback($value)
  246. {
  247. return $value + 1;
  248. }
  249. /**
  250. * Tests the arr::map_recursive() function.
  251. * @dataProvider map_recursive_provider
  252. * @group core.helpers.arr.map_recursive
  253. * @test
  254. */
  255. public function map_recursive($input_callback, $input_array, $expected_result)
  256. {
  257. $result = arr::map_recursive($input_callback, $input_array);
  258. $this->assertEquals($expected_result, $result);
  259. }
  260. /**
  261. * DataProvider for the arr::unshift_assoc() test
  262. */
  263. public function unshift_assoc_provider()
  264. {
  265. return array(
  266. array(
  267. array('fruit1' => 'apple', 'fruit2' => 'mango', 'fruit3' => 'pineapple'),
  268. 'fruit1',
  269. 'strawberry',
  270. array('fruit1' => 'strawberry', 'fruit2' => 'mango', 'fruit3' => 'pineapple')
  271. ),
  272. );
  273. }
  274. /**
  275. * Tests the arr::unshift_assoc() function.
  276. * @dataProvider unshift_assoc_provider
  277. * @group core.helpers.arr.unshift_assoc
  278. * @test
  279. */
  280. public function unshift_assoc($input_array, $input_key, $input_value, $expected_result)
  281. {
  282. $result = arr::unshift_assoc($input_array, $input_key, $input_value);
  283. $this->assertEquals($expected_result, $result);
  284. }
  285. /**
  286. * DataProvider for the arr::to_object() test
  287. */
  288. public function to_object_provider()
  289. {
  290. $expected_result1 = new stdClass();
  291. $expected_result1->test = 13;
  292. $expected_result2 = new stdClass;
  293. $expected_result2->test = $expected_result1;
  294. return array(
  295. array(
  296. array('test' => 13),
  297. 'stdClass',
  298. $expected_result1
  299. ),
  300. array(
  301. array('test' => array('test' => 13)),
  302. 'stdClass',
  303. $expected_result2
  304. )
  305. );
  306. }
  307. /**
  308. * Tests the arr::to_object() function.
  309. * @dataProvider to_object_provider
  310. * @group core.helpers.arr.to_object
  311. * @test
  312. */
  313. public function to_object($input_array, $input_class, $expected_result)
  314. {
  315. $result = arr::to_object($input_array, $input_class);
  316. $this->assertEquals($expected_result, $result);
  317. }
  318. }