HashTest.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 2.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Utility;
  16. use Cake\TestSuite\TestCase;
  17. use Cake\Utility\Hash;
  18. /**
  19. * Class HashTest
  20. *
  21. */
  22. class HashTest extends TestCase {
  23. public static function articleData() {
  24. return array(
  25. array(
  26. 'Article' => array(
  27. 'id' => '1',
  28. 'user_id' => '1',
  29. 'title' => 'First Article',
  30. 'body' => 'First Article Body'
  31. ),
  32. 'User' => array(
  33. 'id' => '1',
  34. 'user' => 'mariano',
  35. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  36. ),
  37. 'Comment' => array(
  38. array(
  39. 'id' => '1',
  40. 'article_id' => '1',
  41. 'user_id' => '2',
  42. 'comment' => 'First Comment for First Article',
  43. ),
  44. array(
  45. 'id' => '2',
  46. 'article_id' => '1',
  47. 'user_id' => '4',
  48. 'comment' => 'Second Comment for First Article',
  49. ),
  50. ),
  51. 'Tag' => array(
  52. array(
  53. 'id' => '1',
  54. 'tag' => 'tag1',
  55. ),
  56. array(
  57. 'id' => '2',
  58. 'tag' => 'tag2',
  59. )
  60. ),
  61. 'Deep' => array(
  62. 'Nesting' => array(
  63. 'test' => array(
  64. 1 => 'foo',
  65. 2 => array(
  66. 'and' => array('more' => 'stuff')
  67. )
  68. )
  69. )
  70. )
  71. ),
  72. array(
  73. 'Article' => array(
  74. 'id' => '2',
  75. 'user_id' => '1',
  76. 'title' => 'Second Article',
  77. 'body' => 'Second Article Body',
  78. 'published' => 'Y',
  79. ),
  80. 'User' => array(
  81. 'id' => '2',
  82. 'user' => 'mariano',
  83. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  84. ),
  85. 'Comment' => array(),
  86. 'Tag' => array()
  87. ),
  88. array(
  89. 'Article' => array(
  90. 'id' => '3',
  91. 'user_id' => '1',
  92. 'title' => 'Third Article',
  93. 'body' => 'Third Article Body',
  94. ),
  95. 'User' => array(
  96. 'id' => '3',
  97. 'user' => 'mariano',
  98. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  99. ),
  100. 'Comment' => array(),
  101. 'Tag' => array()
  102. ),
  103. array(
  104. 'Article' => array(
  105. 'id' => '4',
  106. 'user_id' => '1',
  107. 'title' => 'Fourth Article',
  108. 'body' => 'Fourth Article Body',
  109. ),
  110. 'User' => array(
  111. 'id' => '4',
  112. 'user' => 'mariano',
  113. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  114. ),
  115. 'Comment' => array(),
  116. 'Tag' => array()
  117. ),
  118. array(
  119. 'Article' => array(
  120. 'id' => '5',
  121. 'user_id' => '1',
  122. 'title' => 'Fifth Article',
  123. 'body' => 'Fifth Article Body',
  124. ),
  125. 'User' => array(
  126. 'id' => '5',
  127. 'user' => 'mariano',
  128. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  129. ),
  130. 'Comment' => array(),
  131. 'Tag' => array()
  132. )
  133. );
  134. }
  135. public static function userData() {
  136. return array(
  137. array(
  138. 'User' => array(
  139. 'id' => 2,
  140. 'group_id' => 1,
  141. 'Data' => array(
  142. 'user' => 'mariano.iglesias',
  143. 'name' => 'Mariano Iglesias'
  144. )
  145. )
  146. ),
  147. array(
  148. 'User' => array(
  149. 'id' => 14,
  150. 'group_id' => 2,
  151. 'Data' => array(
  152. 'user' => 'phpnut',
  153. 'name' => 'Larry E. Masters'
  154. )
  155. )
  156. ),
  157. array(
  158. 'User' => array(
  159. 'id' => 25,
  160. 'group_id' => 1,
  161. 'Data' => array(
  162. 'user' => 'gwoo',
  163. 'name' => 'The Gwoo'
  164. )
  165. )
  166. )
  167. );
  168. }
  169. /**
  170. * Test get()
  171. *
  172. * return void
  173. */
  174. public function testGet() {
  175. $data = array('abc', 'def');
  176. $result = Hash::get($data, '0');
  177. $this->assertEquals('abc', $result);
  178. $result = Hash::get($data, 0);
  179. $this->assertEquals('abc', $result);
  180. $result = Hash::get($data, '1');
  181. $this->assertEquals('def', $result);
  182. $data = self::articleData();
  183. $result = Hash::get(array(), '1.Article.title');
  184. $this->assertNull($result);
  185. $result = Hash::get($data, '');
  186. $this->assertNull($result);
  187. $result = Hash::get($data, '0.Article.title');
  188. $this->assertEquals('First Article', $result);
  189. $result = Hash::get($data, '1.Article.title');
  190. $this->assertEquals('Second Article', $result);
  191. $result = Hash::get($data, '5.Article.title');
  192. $this->assertNull($result);
  193. $default = array('empty');
  194. $this->assertEquals($default, Hash::get($data, '5.Article.title', $default));
  195. $this->assertEquals($default, Hash::get(array(), '5.Article.title', $default));
  196. $result = Hash::get($data, '1.Article.title.not_there');
  197. $this->assertNull($result);
  198. $result = Hash::get($data, '1.Article');
  199. $this->assertEquals($data[1]['Article'], $result);
  200. $result = Hash::get($data, array('1', 'Article'));
  201. $this->assertEquals($data[1]['Article'], $result);
  202. }
  203. /**
  204. * Test dimensions.
  205. *
  206. * @return void
  207. */
  208. public function testDimensions() {
  209. $result = Hash::dimensions(array());
  210. $this->assertEquals($result, 0);
  211. $data = array('one', '2', 'three');
  212. $result = Hash::dimensions($data);
  213. $this->assertEquals($result, 1);
  214. $data = array('1' => '1.1', '2', '3');
  215. $result = Hash::dimensions($data);
  216. $this->assertEquals($result, 1);
  217. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  218. $result = Hash::dimensions($data);
  219. $this->assertEquals($result, 2);
  220. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  221. $result = Hash::dimensions($data);
  222. $this->assertEquals($result, 1);
  223. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  224. $result = Hash::dimensions($data);
  225. $this->assertEquals($result, 2);
  226. }
  227. /**
  228. * Test maxDimensions
  229. *
  230. * @return void
  231. */
  232. public function testMaxDimensions() {
  233. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  234. $result = Hash::maxDimensions($data);
  235. $this->assertEquals($result, 2);
  236. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  237. $result = Hash::maxDimensions($data);
  238. $this->assertEquals($result, 3);
  239. $data = array(
  240. '1' => array('1.1' => '1.1.1'),
  241. array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
  242. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  243. );
  244. $result = Hash::maxDimensions($data);
  245. $this->assertEquals($result, 4);
  246. $data = array(
  247. '1' => array('1.1' => '1.1.1'),
  248. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
  249. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  250. );
  251. $result = Hash::maxDimensions($data);
  252. $this->assertEquals($result, 5);
  253. $data = array(
  254. '1' => array('1.1' => '1.1.1'),
  255. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  256. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  257. );
  258. $result = Hash::maxDimensions($data);
  259. $this->assertEquals($result, 5);
  260. $data = array(
  261. '1' => array('1.1' => '1.1.1'),
  262. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  263. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  264. );
  265. $result = Hash::maxDimensions($data);
  266. $this->assertEquals($result, 5);
  267. }
  268. /**
  269. * Tests Hash::flatten
  270. *
  271. * @return void
  272. */
  273. public function testFlatten() {
  274. $data = array('Larry', 'Curly', 'Moe');
  275. $result = Hash::flatten($data);
  276. $this->assertEquals($result, $data);
  277. $data[9] = 'Shemp';
  278. $result = Hash::flatten($data);
  279. $this->assertEquals($result, $data);
  280. $data = array(
  281. array(
  282. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  283. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  284. ),
  285. array(
  286. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  287. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  288. )
  289. );
  290. $result = Hash::flatten($data);
  291. $expected = array(
  292. '0.Post.id' => '1',
  293. '0.Post.author_id' => '1',
  294. '0.Post.title' => 'First Post',
  295. '0.Author.id' => '1',
  296. '0.Author.user' => 'nate',
  297. '0.Author.password' => 'foo',
  298. '1.Post.id' => '2',
  299. '1.Post.author_id' => '3',
  300. '1.Post.title' => 'Second Post',
  301. '1.Post.body' => 'Second Post Body',
  302. '1.Author.id' => '3',
  303. '1.Author.user' => 'larry',
  304. '1.Author.password' => null
  305. );
  306. $this->assertEquals($expected, $result);
  307. $data = array(
  308. array(
  309. 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
  310. 'Author' => array(),
  311. )
  312. );
  313. $result = Hash::flatten($data);
  314. $expected = array(
  315. '0.Post.id' => '1',
  316. '0.Post.author_id' => null,
  317. '0.Post.title' => 'First Post',
  318. '0.Author' => array()
  319. );
  320. $this->assertEquals($expected, $result);
  321. $data = array(
  322. array('Post' => array('id' => 1)),
  323. array('Post' => array('id' => 2)),
  324. );
  325. $result = Hash::flatten($data, '/');
  326. $expected = array(
  327. '0/Post/id' => '1',
  328. '1/Post/id' => '2',
  329. );
  330. $this->assertEquals($expected, $result);
  331. }
  332. /**
  333. * Test diff();
  334. *
  335. * @return void
  336. */
  337. public function testDiff() {
  338. $a = array(
  339. 0 => array('name' => 'main'),
  340. 1 => array('name' => 'about')
  341. );
  342. $b = array(
  343. 0 => array('name' => 'main'),
  344. 1 => array('name' => 'about'),
  345. 2 => array('name' => 'contact')
  346. );
  347. $result = Hash::diff($a, array());
  348. $expected = $a;
  349. $this->assertEquals($expected, $result);
  350. $result = Hash::diff(array(), $b);
  351. $expected = $b;
  352. $this->assertEquals($expected, $result);
  353. $result = Hash::diff($a, $b);
  354. $expected = array(
  355. 2 => array('name' => 'contact')
  356. );
  357. $this->assertEquals($expected, $result);
  358. $b = array(
  359. 0 => array('name' => 'me'),
  360. 1 => array('name' => 'about')
  361. );
  362. $result = Hash::diff($a, $b);
  363. $expected = array(
  364. 0 => array('name' => 'main')
  365. );
  366. $this->assertEquals($expected, $result);
  367. $a = array();
  368. $b = array('name' => 'bob', 'address' => 'home');
  369. $result = Hash::diff($a, $b);
  370. $this->assertEquals($result, $b);
  371. $a = array('name' => 'bob', 'address' => 'home');
  372. $b = array();
  373. $result = Hash::diff($a, $b);
  374. $this->assertEquals($result, $a);
  375. $a = array('key' => true, 'another' => false, 'name' => 'me');
  376. $b = array('key' => 1, 'another' => 0);
  377. $expected = array('name' => 'me');
  378. $result = Hash::diff($a, $b);
  379. $this->assertEquals($expected, $result);
  380. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  381. $b = array('key' => 'differentValue', 'another' => null);
  382. $expected = array('key' => 'value', 'name' => 'me');
  383. $result = Hash::diff($a, $b);
  384. $this->assertEquals($expected, $result);
  385. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  386. $b = array('key' => 'differentValue', 'another' => 'value');
  387. $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
  388. $result = Hash::diff($a, $b);
  389. $this->assertEquals($expected, $result);
  390. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  391. $b = array('key' => 'differentValue', 'another' => 'value');
  392. $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
  393. $result = Hash::diff($b, $a);
  394. $this->assertEquals($expected, $result);
  395. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  396. $b = array(0 => 'differentValue', 1 => 'value');
  397. $expected = $a + $b;
  398. $result = Hash::diff($a, $b);
  399. $this->assertEquals($expected, $result);
  400. }
  401. /**
  402. * Test merge()
  403. *
  404. * @return void
  405. */
  406. public function testMerge() {
  407. $result = Hash::merge(array('foo'), array('bar'));
  408. $this->assertEquals($result, array('foo', 'bar'));
  409. $result = Hash::merge(array('foo'), array('user' => 'bob', 'no-bar'), 'bar');
  410. $this->assertEquals($result, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
  411. $a = array('foo', 'foo2');
  412. $b = array('bar', 'bar2');
  413. $expected = array('foo', 'foo2', 'bar', 'bar2');
  414. $this->assertEquals($expected, Hash::merge($a, $b));
  415. $a = array('foo' => 'bar', 'bar' => 'foo');
  416. $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
  417. $expected = array('foo' => 'no-bar', 'bar' => 'no-foo');
  418. $this->assertEquals($expected, Hash::merge($a, $b));
  419. $a = array('users' => array('bob', 'jim'));
  420. $b = array('users' => array('lisa', 'tina'));
  421. $expected = array('users' => array('bob', 'jim', 'lisa', 'tina'));
  422. $this->assertEquals($expected, Hash::merge($a, $b));
  423. $a = array('users' => array('jim', 'bob'));
  424. $b = array('users' => 'none');
  425. $expected = array('users' => 'none');
  426. $this->assertEquals($expected, Hash::merge($a, $b));
  427. $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
  428. $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
  429. $expected = array(
  430. 'users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)),
  431. 'cakephp',
  432. 'ice-cream'
  433. );
  434. $result = Hash::merge($a, $b);
  435. $this->assertEquals($expected, $result);
  436. $c = array(
  437. 'users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
  438. 'chocolate'
  439. );
  440. $expected = array(
  441. 'users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')),
  442. 'cakephp',
  443. 'ice-cream',
  444. 'chocolate'
  445. );
  446. $this->assertEquals($expected, Hash::merge($a, $b, $c));
  447. $this->assertEquals($expected, Hash::merge($a, $b, array(), $c));
  448. $a = array(
  449. 'Tree',
  450. 'CounterCache',
  451. 'Upload' => array(
  452. 'folder' => 'products',
  453. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
  454. )
  455. );
  456. $b = array(
  457. 'Cacheable' => array('enabled' => false),
  458. 'Limit',
  459. 'Bindable',
  460. 'Validator',
  461. 'Transactional'
  462. );
  463. $expected = array(
  464. 'Tree',
  465. 'CounterCache',
  466. 'Upload' => array(
  467. 'folder' => 'products',
  468. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')
  469. ),
  470. 'Cacheable' => array('enabled' => false),
  471. 'Limit',
  472. 'Bindable',
  473. 'Validator',
  474. 'Transactional'
  475. );
  476. $this->assertEquals(Hash::merge($a, $b), $expected);
  477. }
  478. /**
  479. * test normalizing arrays
  480. *
  481. * @return void
  482. */
  483. public function testNormalize() {
  484. $result = Hash::normalize(array('one', 'two', 'three'));
  485. $expected = array('one' => null, 'two' => null, 'three' => null);
  486. $this->assertEquals($expected, $result);
  487. $result = Hash::normalize(array('one', 'two', 'three'), false);
  488. $expected = array('one', 'two', 'three');
  489. $this->assertEquals($expected, $result);
  490. $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
  491. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  492. $this->assertEquals($expected, $result);
  493. $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
  494. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  495. $this->assertEquals($expected, $result);
  496. $result = Hash::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
  497. $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
  498. $this->assertEquals($expected, $result);
  499. }
  500. /**
  501. * testContains method
  502. *
  503. * @return void
  504. */
  505. public function testContains() {
  506. $data = array('apple', 'bee', 'cyclops');
  507. $this->assertTrue(Hash::contains($data, array('apple')));
  508. $this->assertFalse(Hash::contains($data, array('data')));
  509. $a = array(
  510. 0 => array('name' => 'main'),
  511. 1 => array('name' => 'about')
  512. );
  513. $b = array(
  514. 0 => array('name' => 'main'),
  515. 1 => array('name' => 'about'),
  516. 2 => array('name' => 'contact'),
  517. 'a' => 'b'
  518. );
  519. $this->assertTrue(Hash::contains($a, $a));
  520. $this->assertFalse(Hash::contains($a, $b));
  521. $this->assertTrue(Hash::contains($b, $a));
  522. $a = array(
  523. array('User' => array('id' => 1)),
  524. array('User' => array('id' => 2)),
  525. );
  526. $b = array(
  527. array('User' => array('id' => 1)),
  528. array('User' => array('id' => 2)),
  529. array('User' => array('id' => 3))
  530. );
  531. $this->assertTrue(Hash::contains($b, $a));
  532. $this->assertFalse(Hash::contains($a, $b));
  533. $a = array(0 => 'test', 'string' => null);
  534. $this->assertTrue(Hash::contains($a, array('string' => null)));
  535. $a = array(0 => 'test', 'string' => null);
  536. $this->assertTrue(Hash::contains($a, array('test')));
  537. }
  538. /**
  539. * testFilter method
  540. *
  541. * @return void
  542. */
  543. public function testFilter() {
  544. $result = Hash::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  545. $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
  546. $this->assertSame($expected, $result);
  547. $result = Hash::filter(array(1, array(false)));
  548. $expected = array(1);
  549. $this->assertEquals($expected, $result);
  550. $result = Hash::filter(array(1, array(false, false)));
  551. $expected = array(1);
  552. $this->assertEquals($expected, $result);
  553. $result = Hash::filter(array(1, array('empty', false)));
  554. $expected = array(1, array('empty'));
  555. $this->assertEquals($expected, $result);
  556. $result = Hash::filter(array(1, array('2', false, array(3, null))));
  557. $expected = array(1, array('2', 2 => array(3)));
  558. $this->assertEquals($expected, $result);
  559. $this->assertSame(array(), Hash::filter(array()));
  560. }
  561. /**
  562. * testNumericArrayCheck method
  563. *
  564. * @return void
  565. */
  566. public function testNumeric() {
  567. $data = array('one');
  568. $this->assertTrue(Hash::numeric(array_keys($data)));
  569. $data = array(1 => 'one');
  570. $this->assertFalse(Hash::numeric($data));
  571. $data = array('one');
  572. $this->assertFalse(Hash::numeric($data));
  573. $data = array('one' => 'two');
  574. $this->assertFalse(Hash::numeric($data));
  575. $data = array('one' => 1);
  576. $this->assertTrue(Hash::numeric($data));
  577. $data = array(0);
  578. $this->assertTrue(Hash::numeric($data));
  579. $data = array('one', 'two', 'three', 'four', 'five');
  580. $this->assertTrue(Hash::numeric(array_keys($data)));
  581. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  582. $this->assertTrue(Hash::numeric(array_keys($data)));
  583. $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  584. $this->assertTrue(Hash::numeric(array_keys($data)));
  585. $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  586. $this->assertFalse(Hash::numeric(array_keys($data)));
  587. $data = array(2.4, 1, 0, -1, -2);
  588. $this->assertTrue(Hash::numeric($data));
  589. }
  590. /**
  591. * Test simple paths.
  592. *
  593. * @return void
  594. */
  595. public function testExtractBasic() {
  596. $data = static::articleData();
  597. $result = Hash::extract($data, '');
  598. $this->assertEquals($data, $result);
  599. $result = Hash::extract($data, '0.Article.title');
  600. $this->assertEquals(array('First Article'), $result);
  601. $result = Hash::extract($data, '1.Article.title');
  602. $this->assertEquals(array('Second Article'), $result);
  603. $result = Hash::extract(array(false), '{n}.Something.another_thing');
  604. $this->assertEquals(array(), $result);
  605. }
  606. /**
  607. * Test the {n} selector
  608. *
  609. * @return void
  610. */
  611. public function testExtractNumericKey() {
  612. $data = static::articleData();
  613. $result = Hash::extract($data, '{n}.Article.title');
  614. $expected = array(
  615. 'First Article', 'Second Article',
  616. 'Third Article', 'Fourth Article',
  617. 'Fifth Article'
  618. );
  619. $this->assertEquals($expected, $result);
  620. $result = Hash::extract($data, '0.Comment.{n}.user_id');
  621. $expected = array(
  622. '2', '4'
  623. );
  624. $this->assertEquals($expected, $result);
  625. }
  626. /**
  627. * Test the {n} selector with inconsistent arrays
  628. *
  629. * @return void
  630. */
  631. public function testExtractNumericMixedKeys() {
  632. $data = array(
  633. 'User' => array(
  634. 0 => array(
  635. 'id' => 4,
  636. 'name' => 'Neo'
  637. ),
  638. 1 => array(
  639. 'id' => 5,
  640. 'name' => 'Morpheus'
  641. ),
  642. 'stringKey' => array(
  643. 'name' => 'Fail'
  644. )
  645. )
  646. );
  647. $result = Hash::extract($data, 'User.{n}.name');
  648. $expected = array('Neo', 'Morpheus');
  649. $this->assertEquals($expected, $result);
  650. }
  651. /**
  652. * Test the {n} selector with non-zero based arrays
  653. *
  654. * @return void
  655. */
  656. public function testExtractNumericNonZero() {
  657. $data = array(
  658. 1 => array(
  659. 'User' => array(
  660. 'id' => 1,
  661. 'name' => 'John',
  662. )
  663. ),
  664. 2 => array(
  665. 'User' => array(
  666. 'id' => 2,
  667. 'name' => 'Bob',
  668. )
  669. ),
  670. 3 => array(
  671. 'User' => array(
  672. 'id' => 3,
  673. 'name' => 'Tony',
  674. )
  675. )
  676. );
  677. $result = Hash::extract($data, '{n}.User.name');
  678. $expected = array('John', 'Bob', 'Tony');
  679. $this->assertEquals($expected, $result);
  680. }
  681. /**
  682. * Test the {s} selector.
  683. *
  684. * @return void
  685. */
  686. public function testExtractStringKey() {
  687. $data = static::articleData();
  688. $result = Hash::extract($data, '{n}.{s}.user');
  689. $expected = array(
  690. 'mariano',
  691. 'mariano',
  692. 'mariano',
  693. 'mariano',
  694. 'mariano'
  695. );
  696. $this->assertEquals($expected, $result);
  697. $result = Hash::extract($data, '{n}.{s}.Nesting.test.1');
  698. $this->assertEquals(array('foo'), $result);
  699. }
  700. /**
  701. * Test the attribute presense selector.
  702. *
  703. * @return void
  704. */
  705. public function testExtractAttributePresence() {
  706. $data = static::articleData();
  707. $result = Hash::extract($data, '{n}.Article[published]');
  708. $expected = array($data[1]['Article']);
  709. $this->assertEquals($expected, $result);
  710. $result = Hash::extract($data, '{n}.Article[id][published]');
  711. $expected = array($data[1]['Article']);
  712. $this->assertEquals($expected, $result);
  713. }
  714. /**
  715. * Test = and != operators.
  716. *
  717. * @return void
  718. */
  719. public function testExtractAttributeEquality() {
  720. $data = static::articleData();
  721. $result = Hash::extract($data, '{n}.Article[id=3]');
  722. $expected = array($data[2]['Article']);
  723. $this->assertEquals($expected, $result);
  724. $result = Hash::extract($data, '{n}.Article[id = 3]');
  725. $expected = array($data[2]['Article']);
  726. $this->assertEquals($expected, $result, 'Whitespace should not matter.');
  727. $result = Hash::extract($data, '{n}.Article[id!=3]');
  728. $this->assertEquals(1, $result[0]['id']);
  729. $this->assertEquals(2, $result[1]['id']);
  730. $this->assertEquals(4, $result[2]['id']);
  731. $this->assertEquals(5, $result[3]['id']);
  732. }
  733. /**
  734. * Test that attribute matchers don't cause errors on scalar data.
  735. *
  736. * @return void
  737. */
  738. public function testExtractAttributeEqualityOnScalarValue() {
  739. $data = array(
  740. 'Entity' => array(
  741. 'id' => 1,
  742. 'data1' => 'value',
  743. )
  744. );
  745. $result = Hash::extract($data, 'Entity[id=1].data1');
  746. $this->assertEquals(array('value'), $result);
  747. $data = array('Entity' => false );
  748. $result = Hash::extract($data, 'Entity[id=1].data1');
  749. $this->assertEquals(array(), $result);
  750. }
  751. /**
  752. * Test comparison operators.
  753. *
  754. * @return void
  755. */
  756. public function testExtractAttributeComparison() {
  757. $data = static::articleData();
  758. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2]');
  759. $expected = array($data[0]['Comment'][1]);
  760. $this->assertEquals($expected, $result);
  761. $this->assertEquals(4, $expected[0]['user_id']);
  762. $result = Hash::extract($data, '{n}.Comment.{n}[user_id >= 4]');
  763. $expected = array($data[0]['Comment'][1]);
  764. $this->assertEquals($expected, $result);
  765. $this->assertEquals(4, $expected[0]['user_id']);
  766. $result = Hash::extract($data, '{n}.Comment.{n}[user_id < 3]');
  767. $expected = array($data[0]['Comment'][0]);
  768. $this->assertEquals($expected, $result);
  769. $this->assertEquals(2, $expected[0]['user_id']);
  770. $result = Hash::extract($data, '{n}.Comment.{n}[user_id <= 2]');
  771. $expected = array($data[0]['Comment'][0]);
  772. $this->assertEquals($expected, $result);
  773. $this->assertEquals(2, $expected[0]['user_id']);
  774. }
  775. /**
  776. * Test multiple attributes with conditions.
  777. *
  778. * @return void
  779. */
  780. public function testExtractAttributeMultiple() {
  781. $data = static::articleData();
  782. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=1]');
  783. $this->assertEmpty($result);
  784. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=2]');
  785. $expected = array($data[0]['Comment'][1]);
  786. $this->assertEquals($expected, $result);
  787. $this->assertEquals(4, $expected[0]['user_id']);
  788. }
  789. /**
  790. * Test attribute pattern matching.
  791. *
  792. * @return void
  793. */
  794. public function testExtractAttributePattern() {
  795. $data = static::articleData();
  796. $result = Hash::extract($data, '{n}.Article[title=/^First/]');
  797. $expected = array($data[0]['Article']);
  798. $this->assertEquals($expected, $result);
  799. $result = Hash::extract($data, '{n}.Article[title=/^Fir[a-z]+/]');
  800. $expected = array($data[0]['Article']);
  801. $this->assertEquals($expected, $result);
  802. }
  803. /**
  804. * Test that extract() + matching can hit null things.
  805. */
  806. public function testExtractMatchesNull() {
  807. $data = array(
  808. 'Country' => array(
  809. array('name' => 'Canada'),
  810. array('name' => 'Australia'),
  811. array('name' => null),
  812. )
  813. );
  814. $result = Hash::extract($data, 'Country.{n}[name=/Canada|^$/]');
  815. $expected = array(
  816. array(
  817. 'name' => 'Canada',
  818. ),
  819. array(
  820. 'name' => null,
  821. ),
  822. );
  823. $this->assertEquals($expected, $result);
  824. }
  825. /**
  826. * Test that uneven keys are handled correctly.
  827. *
  828. * @return void
  829. */
  830. public function testExtractUnevenKeys() {
  831. $data = array(
  832. 'Level1' => array(
  833. 'Level2' => array('test1', 'test2'),
  834. 'Level2bis' => array('test3', 'test4')
  835. )
  836. );
  837. $this->assertEquals(
  838. array('test1', 'test2'),
  839. Hash::extract($data, 'Level1.Level2')
  840. );
  841. $this->assertEquals(
  842. array('test3', 'test4'),
  843. Hash::extract($data, 'Level1.Level2bis')
  844. );
  845. $data = array(
  846. 'Level1' => array(
  847. 'Level2bis' => array(
  848. array('test3', 'test4'),
  849. array('test5', 'test6')
  850. )
  851. )
  852. );
  853. $expected = array(
  854. array('test3', 'test4'),
  855. array('test5', 'test6')
  856. );
  857. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  858. $data['Level1']['Level2'] = array('test1', 'test2');
  859. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  860. }
  861. /**
  862. * testSort method
  863. *
  864. * @return void
  865. */
  866. public function testSort() {
  867. $result = Hash::sort(array(), '{n}.name', 'asc');
  868. $this->assertEquals(array(), $result);
  869. $a = array(
  870. 0 => array(
  871. 'Person' => array('name' => 'Jeff'),
  872. 'Friend' => array(array('name' => 'Nate'))
  873. ),
  874. 1 => array(
  875. 'Person' => array('name' => 'Tracy'),
  876. 'Friend' => array(array('name' => 'Lindsay'))
  877. )
  878. );
  879. $b = array(
  880. 0 => array(
  881. 'Person' => array('name' => 'Tracy'),
  882. 'Friend' => array(array('name' => 'Lindsay'))
  883. ),
  884. 1 => array(
  885. 'Person' => array('name' => 'Jeff'),
  886. 'Friend' => array(array('name' => 'Nate'))
  887. )
  888. );
  889. $a = Hash::sort($a, '{n}.Friend.{n}.name', 'asc');
  890. $this->assertEquals($a, $b);
  891. $b = array(
  892. 0 => array(
  893. 'Person' => array('name' => 'Jeff'),
  894. 'Friend' => array(array('name' => 'Nate'))
  895. ),
  896. 1 => array(
  897. 'Person' => array('name' => 'Tracy'),
  898. 'Friend' => array(array('name' => 'Lindsay'))
  899. )
  900. );
  901. $a = array(
  902. 0 => array(
  903. 'Person' => array('name' => 'Tracy'),
  904. 'Friend' => array(array('name' => 'Lindsay'))
  905. ),
  906. 1 => array(
  907. 'Person' => array('name' => 'Jeff'),
  908. 'Friend' => array(array('name' => 'Nate'))
  909. )
  910. );
  911. $a = Hash::sort($a, '{n}.Friend.{n}.name', 'desc');
  912. $this->assertEquals($a, $b);
  913. $a = array(
  914. 0 => array(
  915. 'Person' => array('name' => 'Jeff'),
  916. 'Friend' => array(array('name' => 'Nate'))
  917. ),
  918. 1 => array(
  919. 'Person' => array('name' => 'Tracy'),
  920. 'Friend' => array(array('name' => 'Lindsay'))
  921. ),
  922. 2 => array(
  923. 'Person' => array('name' => 'Adam'),
  924. 'Friend' => array(array('name' => 'Bob'))
  925. )
  926. );
  927. $b = array(
  928. 0 => array(
  929. 'Person' => array('name' => 'Adam'),
  930. 'Friend' => array(array('name' => 'Bob'))
  931. ),
  932. 1 => array(
  933. 'Person' => array('name' => 'Jeff'),
  934. 'Friend' => array(array('name' => 'Nate'))
  935. ),
  936. 2 => array(
  937. 'Person' => array('name' => 'Tracy'),
  938. 'Friend' => array(array('name' => 'Lindsay'))
  939. )
  940. );
  941. $a = Hash::sort($a, '{n}.Person.name', 'asc');
  942. $this->assertEquals($a, $b);
  943. $a = array(
  944. 0 => array('Person' => array('name' => 'Jeff')),
  945. 1 => array('Shirt' => array('color' => 'black'))
  946. );
  947. $b = array(
  948. 0 => array('Shirt' => array('color' => 'black')),
  949. 1 => array('Person' => array('name' => 'Jeff')),
  950. );
  951. $a = Hash::sort($a, '{n}.Person.name', 'ASC', 'STRING');
  952. $this->assertSame($a, $b);
  953. $names = array(
  954. array('employees' => array(
  955. array('name' => array('first' => 'John', 'last' => 'Doe')))
  956. ),
  957. array('employees' => array(
  958. array('name' => array('first' => 'Jane', 'last' => 'Doe')))
  959. ),
  960. array('employees' => array(array('name' => array()))),
  961. array('employees' => array(array('name' => array())))
  962. );
  963. $result = Hash::sort($names, '{n}.employees.0.name', 'asc');
  964. $expected = array(
  965. array('employees' => array(
  966. array('name' => array('first' => 'John', 'last' => 'Doe')))
  967. ),
  968. array('employees' => array(
  969. array('name' => array('first' => 'Jane', 'last' => 'Doe')))
  970. ),
  971. array('employees' => array(array('name' => array()))),
  972. array('employees' => array(array('name' => array())))
  973. );
  974. $this->assertSame($expected, $result);
  975. $a = array(
  976. 'SU' => array(
  977. 'total_fulfillable' => 2
  978. ),
  979. 'AA' => array(
  980. 'total_fulfillable' => 1
  981. ),
  982. 'LX' => array(
  983. 'total_fulfillable' => 0
  984. ),
  985. 'BL' => array(
  986. 'total_fulfillable' => 3
  987. ),
  988. );
  989. $expected = array(
  990. 'LX' => array(
  991. 'total_fulfillable' => 0
  992. ),
  993. 'AA' => array(
  994. 'total_fulfillable' => 1
  995. ),
  996. 'SU' => array(
  997. 'total_fulfillable' => 2
  998. ),
  999. 'BL' => array(
  1000. 'total_fulfillable' => 3
  1001. ),
  1002. );
  1003. $result = Hash::sort($a, '{s}.total_fulfillable', 'asc');
  1004. $this->assertSame($expected, $result);
  1005. }
  1006. /**
  1007. * Test sort() with numeric option.
  1008. *
  1009. * @return void
  1010. */
  1011. public function testSortNumeric() {
  1012. $items = array(
  1013. array('Item' => array('price' => '155,000')),
  1014. array('Item' => array('price' => '139,000')),
  1015. array('Item' => array('price' => '275,622')),
  1016. array('Item' => array('price' => '230,888')),
  1017. array('Item' => array('price' => '66,000')),
  1018. );
  1019. $result = Hash::sort($items, '{n}.Item.price', 'asc', 'numeric');
  1020. $expected = array(
  1021. array('Item' => array('price' => '66,000')),
  1022. array('Item' => array('price' => '139,000')),
  1023. array('Item' => array('price' => '155,000')),
  1024. array('Item' => array('price' => '230,888')),
  1025. array('Item' => array('price' => '275,622')),
  1026. );
  1027. $this->assertEquals($expected, $result);
  1028. $result = Hash::sort($items, '{n}.Item.price', 'desc', 'numeric');
  1029. $expected = array(
  1030. array('Item' => array('price' => '275,622')),
  1031. array('Item' => array('price' => '230,888')),
  1032. array('Item' => array('price' => '155,000')),
  1033. array('Item' => array('price' => '139,000')),
  1034. array('Item' => array('price' => '66,000')),
  1035. );
  1036. $this->assertEquals($expected, $result);
  1037. }
  1038. /**
  1039. * Test natural sorting.
  1040. *
  1041. * @return void
  1042. */
  1043. public function testSortNatural() {
  1044. $items = array(
  1045. array('Item' => array('image' => 'img1.jpg')),
  1046. array('Item' => array('image' => 'img99.jpg')),
  1047. array('Item' => array('image' => 'img12.jpg')),
  1048. array('Item' => array('image' => 'img10.jpg')),
  1049. array('Item' => array('image' => 'img2.jpg')),
  1050. );
  1051. $result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural');
  1052. $expected = array(
  1053. array('Item' => array('image' => 'img99.jpg')),
  1054. array('Item' => array('image' => 'img12.jpg')),
  1055. array('Item' => array('image' => 'img10.jpg')),
  1056. array('Item' => array('image' => 'img2.jpg')),
  1057. array('Item' => array('image' => 'img1.jpg')),
  1058. );
  1059. $this->assertEquals($expected, $result);
  1060. $result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural');
  1061. $expected = array(
  1062. array('Item' => array('image' => 'img1.jpg')),
  1063. array('Item' => array('image' => 'img2.jpg')),
  1064. array('Item' => array('image' => 'img10.jpg')),
  1065. array('Item' => array('image' => 'img12.jpg')),
  1066. array('Item' => array('image' => 'img99.jpg')),
  1067. );
  1068. $this->assertEquals($expected, $result);
  1069. }
  1070. /**
  1071. * Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
  1072. *
  1073. * @return void
  1074. */
  1075. public function testSortNaturalFallbackToRegular() {
  1076. $a = array(
  1077. 0 => array('Person' => array('name' => 'Jeff')),
  1078. 1 => array('Shirt' => array('color' => 'black'))
  1079. );
  1080. $b = array(
  1081. 0 => array('Shirt' => array('color' => 'black')),
  1082. 1 => array('Person' => array('name' => 'Jeff')),
  1083. );
  1084. $sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
  1085. $this->assertEquals($sorted, $b);
  1086. }
  1087. /**
  1088. * test sorting with out of order keys.
  1089. *
  1090. * @return void
  1091. */
  1092. public function testSortWithOutOfOrderKeys() {
  1093. $data = array(
  1094. 9 => array('class' => 510, 'test2' => 2),
  1095. 1 => array('class' => 500, 'test2' => 1),
  1096. 2 => array('class' => 600, 'test2' => 2),
  1097. 5 => array('class' => 625, 'test2' => 4),
  1098. 0 => array('class' => 605, 'test2' => 3),
  1099. );
  1100. $expected = array(
  1101. array('class' => 500, 'test2' => 1),
  1102. array('class' => 510, 'test2' => 2),
  1103. array('class' => 600, 'test2' => 2),
  1104. array('class' => 605, 'test2' => 3),
  1105. array('class' => 625, 'test2' => 4),
  1106. );
  1107. $result = Hash::sort($data, '{n}.class', 'asc');
  1108. $this->assertEquals($expected, $result);
  1109. $result = Hash::sort($data, '{n}.test2', 'asc');
  1110. $this->assertEquals($expected, $result);
  1111. }
  1112. /**
  1113. * test sorting with string keys.
  1114. *
  1115. * @return void
  1116. */
  1117. public function testSortString() {
  1118. $toSort = array(
  1119. 'four' => array('number' => 4, 'some' => 'foursome'),
  1120. 'six' => array('number' => 6, 'some' => 'sixsome'),
  1121. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1122. 'two' => array('number' => 2, 'some' => 'twosome'),
  1123. 'three' => array('number' => 3, 'some' => 'threesome')
  1124. );
  1125. $sorted = Hash::sort($toSort, '{s}.number', 'asc');
  1126. $expected = array(
  1127. 'two' => array('number' => 2, 'some' => 'twosome'),
  1128. 'three' => array('number' => 3, 'some' => 'threesome'),
  1129. 'four' => array('number' => 4, 'some' => 'foursome'),
  1130. 'five' => array('number' => 5, 'some' => 'fivesome'),
  1131. 'six' => array('number' => 6, 'some' => 'sixsome')
  1132. );
  1133. $this->assertEquals($expected, $sorted);
  1134. $menus = array(
  1135. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1136. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1137. 'users' => array('title' => 'Users', 'weight' => 1),
  1138. );
  1139. $expected = array(
  1140. 'users' => array('title' => 'Users', 'weight' => 1),
  1141. 'comments' => array('title' => 'Comments', 'weight' => 2),
  1142. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  1143. );
  1144. $result = Hash::sort($menus, '{s}.weight', 'ASC');
  1145. $this->assertEquals($expected, $result);
  1146. }
  1147. /**
  1148. * Test insert()
  1149. *
  1150. * @return void
  1151. */
  1152. public function testInsertSimple() {
  1153. $a = array(
  1154. 'pages' => array('name' => 'page')
  1155. );
  1156. $result = Hash::insert($a, 'files', array('name' => 'files'));
  1157. $expected = array(
  1158. 'pages' => array('name' => 'page'),
  1159. 'files' => array('name' => 'files')
  1160. );
  1161. $this->assertEquals($expected, $result);
  1162. $a = array(
  1163. 'pages' => array('name' => 'page')
  1164. );
  1165. $result = Hash::insert($a, 'pages.name', array());
  1166. $expected = array(
  1167. 'pages' => array('name' => array()),
  1168. );
  1169. $this->assertEquals($expected, $result);
  1170. }
  1171. /**
  1172. * Test inserting with multiple values.
  1173. *
  1174. * @return void
  1175. */
  1176. public function testInsertMulti() {
  1177. $data = static::articleData();
  1178. $result = Hash::insert($data, '{n}.Article.insert', 'value');
  1179. $this->assertEquals('value', $result[0]['Article']['insert']);
  1180. $this->assertEquals('value', $result[1]['Article']['insert']);
  1181. $result = Hash::insert($data, '{n}.Comment.{n}.insert', 'value');
  1182. $this->assertEquals('value', $result[0]['Comment'][0]['insert']);
  1183. $this->assertEquals('value', $result[0]['Comment'][1]['insert']);
  1184. $data = array(
  1185. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1186. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1187. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1188. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1189. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1190. );
  1191. $result = Hash::insert($data, '{n}.Item[id=/\b2|\b4/]', array('test' => 2));
  1192. $expected = array(
  1193. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1194. 1 => array('Item' => array('id' => 2, 'title' => 'second', 'test' => 2)),
  1195. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1196. 3 => array('Item' => array('id' => 4, 'title' => 'fourth', 'test' => 2)),
  1197. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1198. );
  1199. $this->assertEquals($expected, $result);
  1200. }
  1201. /**
  1202. * Test that insert() can insert data over a string value.
  1203. *
  1204. * @return void
  1205. */
  1206. public function testInsertOverwriteStringValue() {
  1207. $data = array(
  1208. 'Some' => array(
  1209. 'string' => 'value'
  1210. )
  1211. );
  1212. $result = Hash::insert($data, 'Some.string.value', array('values'));
  1213. $expected = array(
  1214. 'Some' => array(
  1215. 'string' => array(
  1216. 'value' => array('values')
  1217. )
  1218. )
  1219. );
  1220. $this->assertEquals($expected, $result);
  1221. }
  1222. /**
  1223. * Test remove() method.
  1224. *
  1225. * @return void
  1226. */
  1227. public function testRemove() {
  1228. $a = array(
  1229. 'pages' => array('name' => 'page'),
  1230. 'files' => array('name' => 'files')
  1231. );
  1232. $result = Hash::remove($a, 'files');
  1233. $expected = array(
  1234. 'pages' => array('name' => 'page')
  1235. );
  1236. $this->assertEquals($expected, $result);
  1237. $a = array(
  1238. 'pages' => array(
  1239. 0 => array('name' => 'main'),
  1240. 1 => array(
  1241. 'name' => 'about',
  1242. 'vars' => array('title' => 'page title')
  1243. )
  1244. )
  1245. );
  1246. $result = Hash::remove($a, 'pages.1.vars');
  1247. $expected = array(
  1248. 'pages' => array(
  1249. 0 => array('name' => 'main'),
  1250. 1 => array('name' => 'about')
  1251. )
  1252. );
  1253. $this->assertEquals($expected, $result);
  1254. $result = Hash::remove($a, 'pages.2.vars');
  1255. $expected = $a;
  1256. $this->assertEquals($expected, $result);
  1257. $a = array(
  1258. 0 => array(
  1259. 'name' => 'pages'
  1260. ),
  1261. 1 => array(
  1262. 'name' => 'files'
  1263. )
  1264. );
  1265. $result = Hash::remove($a, '{n}[name=files]');
  1266. $expected = array(
  1267. 0 => array(
  1268. 'name' => 'pages'
  1269. )
  1270. );
  1271. $this->assertEquals($expected, $result);
  1272. }
  1273. /**
  1274. * Test removing multiple values.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testRemoveMulti() {
  1279. $data = static::articleData();
  1280. $result = Hash::remove($data, '{n}.Article.title');
  1281. $this->assertFalse(isset($result[0]['Article']['title']));
  1282. $this->assertFalse(isset($result[1]['Article']['title']));
  1283. $result = Hash::remove($data, '{n}.Article.{s}');
  1284. $this->assertFalse(isset($result[0]['Article']['id']));
  1285. $this->assertFalse(isset($result[0]['Article']['user_id']));
  1286. $this->assertFalse(isset($result[0]['Article']['title']));
  1287. $this->assertFalse(isset($result[0]['Article']['body']));
  1288. $data = array(
  1289. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1290. 1 => array('Item' => array('id' => 2, 'title' => 'second')),
  1291. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1292. 3 => array('Item' => array('id' => 4, 'title' => 'fourth')),
  1293. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1294. );
  1295. $result = Hash::remove($data, '{n}.Item[id=/\b2|\b4/]');
  1296. $expected = array(
  1297. 0 => array('Item' => array('id' => 1, 'title' => 'first')),
  1298. 2 => array('Item' => array('id' => 3, 'title' => 'third')),
  1299. 4 => array('Item' => array('id' => 5, 'title' => 'fifth')),
  1300. );
  1301. $this->assertEquals($result, $expected);
  1302. }
  1303. /**
  1304. * testCheck method
  1305. *
  1306. * @return void
  1307. */
  1308. public function testCheck() {
  1309. $set = array(
  1310. 'My Index 1' => array('First' => 'The first item')
  1311. );
  1312. $this->assertTrue(Hash::check($set, 'My Index 1.First'));
  1313. $this->assertTrue(Hash::check($set, 'My Index 1'));
  1314. $set = array(
  1315. 'My Index 1' => array(
  1316. 'First' => array(
  1317. 'Second' => array(
  1318. 'Third' => array(
  1319. 'Fourth' => 'Heavy. Nesting.'
  1320. )
  1321. )
  1322. )
  1323. )
  1324. );
  1325. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second'));
  1326. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third'));
  1327. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1328. $this->assertFalse(Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1329. }
  1330. /**
  1331. * testCombine method
  1332. *
  1333. * @return void
  1334. */
  1335. public function testCombine() {
  1336. $result = Hash::combine(array(), '{n}.User.id', '{n}.User.Data');
  1337. $this->assertTrue(empty($result));
  1338. $a = static::userData();
  1339. $result = Hash::combine($a, '{n}.User.id');
  1340. $expected = array(2 => null, 14 => null, 25 => null);
  1341. $this->assertEquals($expected, $result);
  1342. $result = Hash::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1343. $expected = array(2 => null, 14 => null, 25 => null);
  1344. $this->assertEquals($expected, $result);
  1345. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
  1346. $expected = array(
  1347. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1348. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1349. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1350. $this->assertEquals($expected, $result);
  1351. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1352. $expected = array(
  1353. 2 => 'Mariano Iglesias',
  1354. 14 => 'Larry E. Masters',
  1355. 25 => 'The Gwoo');
  1356. $this->assertEquals($expected, $result);
  1357. }
  1358. /**
  1359. * test combine() giving errors on key/value length mismatches.
  1360. *
  1361. * @expectedException RuntimeException
  1362. * @return void
  1363. */
  1364. public function testCombineErrorMissingValue() {
  1365. $data = array(
  1366. array('User' => array('id' => 1, 'name' => 'mark')),
  1367. array('User' => array('name' => 'jose')),
  1368. );
  1369. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1370. }
  1371. /**
  1372. * test combine() giving errors on key/value length mismatches.
  1373. *
  1374. * @expectedException RuntimeException
  1375. * @return void
  1376. */
  1377. public function testCombineErrorMissingKey() {
  1378. $data = array(
  1379. array('User' => array('id' => 1, 'name' => 'mark')),
  1380. array('User' => array('id' => 2)),
  1381. );
  1382. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1383. }
  1384. /**
  1385. * test combine() with a group path.
  1386. *
  1387. * @return void
  1388. */
  1389. public function testCombineWithGroupPath() {
  1390. $a = static::userData();
  1391. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1392. $expected = array(
  1393. 1 => array(
  1394. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1395. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1396. ),
  1397. 2 => array(
  1398. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1399. )
  1400. );
  1401. $this->assertEquals($expected, $result);
  1402. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1403. $expected = array(
  1404. 1 => array(
  1405. 2 => 'Mariano Iglesias',
  1406. 25 => 'The Gwoo'
  1407. ),
  1408. 2 => array(
  1409. 14 => 'Larry E. Masters'
  1410. )
  1411. );
  1412. $this->assertEquals($expected, $result);
  1413. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1414. $expected = array(
  1415. 1 => array(
  1416. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1417. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')
  1418. ),
  1419. 2 => array(
  1420. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')
  1421. )
  1422. );
  1423. $this->assertEquals($expected, $result);
  1424. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1425. $expected = array(
  1426. 1 => array(
  1427. 2 => 'Mariano Iglesias',
  1428. 25 => 'The Gwoo'
  1429. ),
  1430. 2 => array(
  1431. 14 => 'Larry E. Masters'
  1432. )
  1433. );
  1434. $this->assertEquals($expected, $result);
  1435. }
  1436. /**
  1437. * Test combine with formatting rules.
  1438. *
  1439. * @return void
  1440. */
  1441. public function testCombineWithFormatting() {
  1442. $a = static::userData();
  1443. $result = Hash::combine(
  1444. $a,
  1445. '{n}.User.id',
  1446. array('%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'),
  1447. '{n}.User.group_id'
  1448. );
  1449. $expected = array(
  1450. 1 => array(
  1451. 2 => 'mariano.iglesias: Mariano Iglesias',
  1452. 25 => 'gwoo: The Gwoo'
  1453. ),
  1454. 2 => array(
  1455. 14 => 'phpnut: Larry E. Masters'
  1456. )
  1457. );
  1458. $this->assertEquals($expected, $result);
  1459. $result = Hash::combine(
  1460. $a,
  1461. array(
  1462. '%s: %s',
  1463. '{n}.User.Data.user',
  1464. '{n}.User.Data.name'
  1465. ),
  1466. '{n}.User.id'
  1467. );
  1468. $expected = array(
  1469. 'mariano.iglesias: Mariano Iglesias' => 2,
  1470. 'phpnut: Larry E. Masters' => 14,
  1471. 'gwoo: The Gwoo' => 25
  1472. );
  1473. $this->assertEquals($expected, $result);
  1474. $result = Hash::combine(
  1475. $a,
  1476. array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'),
  1477. '{n}.User.Data.name'
  1478. );
  1479. $expected = array(
  1480. 'mariano.iglesias: 2' => 'Mariano Iglesias',
  1481. 'phpnut: 14' => 'Larry E. Masters',
  1482. 'gwoo: 25' => 'The Gwoo'
  1483. );
  1484. $this->assertEquals($expected, $result);
  1485. $result = Hash::combine(
  1486. $a,
  1487. array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'),
  1488. '{n}.User.Data.name'
  1489. );
  1490. $expected = array(
  1491. '2: mariano.iglesias' => 'Mariano Iglesias',
  1492. '14: phpnut' => 'Larry E. Masters',
  1493. '25: gwoo' => 'The Gwoo'
  1494. );
  1495. $this->assertEquals($expected, $result);
  1496. }
  1497. /**
  1498. * testFormat method
  1499. *
  1500. * @return void
  1501. */
  1502. public function testFormat() {
  1503. $data = static::userData();
  1504. $result = Hash::format(
  1505. $data,
  1506. array('{n}.User.Data.user', '{n}.User.id'),
  1507. '%s, %s'
  1508. );
  1509. $expected = array(
  1510. 'mariano.iglesias, 2',
  1511. 'phpnut, 14',
  1512. 'gwoo, 25'
  1513. );
  1514. $this->assertEquals($expected, $result);
  1515. $result = Hash::format(
  1516. $data,
  1517. array('{n}.User.Data.user', '{n}.User.id'),
  1518. '%2$s, %1$s'
  1519. );
  1520. $expected = array(
  1521. '2, mariano.iglesias',
  1522. '14, phpnut',
  1523. '25, gwoo'
  1524. );
  1525. $this->assertEquals($expected, $result);
  1526. }
  1527. /**
  1528. * testFormattingNullValues method
  1529. *
  1530. * @return void
  1531. */
  1532. public function testFormatNullValues() {
  1533. $data = array(
  1534. array('Person' => array(
  1535. 'first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42'
  1536. )),
  1537. array('Person' => array(
  1538. 'first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null
  1539. )),
  1540. array('Person' => array(
  1541. 'first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null
  1542. ))
  1543. );
  1544. $result = Hash::format($data, array('{n}.Person.something'), '%s');
  1545. $expected = array('42', '', '');
  1546. $this->assertEquals($expected, $result);
  1547. $result = Hash::format($data, array('{n}.Person.city', '{n}.Person.something'), '%s, %s');
  1548. $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
  1549. $this->assertEquals($expected, $result);
  1550. }
  1551. /**
  1552. * Test map()
  1553. *
  1554. * @return void
  1555. */
  1556. public function testMap() {
  1557. $data = static::articleData();
  1558. $result = Hash::map($data, '{n}.Article.id', array($this, 'mapCallback'));
  1559. $expected = array(2, 4, 6, 8, 10);
  1560. $this->assertEquals($expected, $result);
  1561. }
  1562. public function testApply() {
  1563. $data = static::articleData();
  1564. $result = Hash::apply($data, '{n}.Article.id', 'array_sum');
  1565. $this->assertEquals(15, $result);
  1566. }
  1567. /**
  1568. * Test reduce()
  1569. *
  1570. * @return void
  1571. */
  1572. public function testReduce() {
  1573. $data = static::articleData();
  1574. $result = Hash::reduce($data, '{n}.Article.id', array($this, 'reduceCallback'));
  1575. $this->assertEquals(15, $result);
  1576. }
  1577. /**
  1578. * testing method for map callbacks.
  1579. *
  1580. * @param mixed $value
  1581. * @return mixed.
  1582. */
  1583. public function mapCallback($value) {
  1584. return $value * 2;
  1585. }
  1586. /**
  1587. * testing method for reduce callbacks.
  1588. *
  1589. * @param mixed $one
  1590. * @param mixed $two
  1591. * @return mixed.
  1592. */
  1593. public function reduceCallback($one, $two) {
  1594. return $one + $two;
  1595. }
  1596. /**
  1597. * test Hash nest with a normal model result set. For kicks rely on Hash nest detecting the key names
  1598. * automatically
  1599. *
  1600. * @return void
  1601. */
  1602. public function testNestModel() {
  1603. $input = array(
  1604. array(
  1605. 'ModelName' => array(
  1606. 'id' => 1,
  1607. 'parent_id' => null
  1608. ),
  1609. ),
  1610. array(
  1611. 'ModelName' => array(
  1612. 'id' => 2,
  1613. 'parent_id' => 1
  1614. ),
  1615. ),
  1616. array(
  1617. 'ModelName' => array(
  1618. 'id' => 3,
  1619. 'parent_id' => 1
  1620. ),
  1621. ),
  1622. array(
  1623. 'ModelName' => array(
  1624. 'id' => 4,
  1625. 'parent_id' => 1
  1626. ),
  1627. ),
  1628. array(
  1629. 'ModelName' => array(
  1630. 'id' => 5,
  1631. 'parent_id' => 1
  1632. ),
  1633. ),
  1634. array(
  1635. 'ModelName' => array(
  1636. 'id' => 6,
  1637. 'parent_id' => null
  1638. ),
  1639. ),
  1640. array(
  1641. 'ModelName' => array(
  1642. 'id' => 7,
  1643. 'parent_id' => 6
  1644. ),
  1645. ),
  1646. array(
  1647. 'ModelName' => array(
  1648. 'id' => 8,
  1649. 'parent_id' => 6
  1650. ),
  1651. ),
  1652. array(
  1653. 'ModelName' => array(
  1654. 'id' => 9,
  1655. 'parent_id' => 6
  1656. ),
  1657. ),
  1658. array(
  1659. 'ModelName' => array(
  1660. 'id' => 10,
  1661. 'parent_id' => 6
  1662. )
  1663. )
  1664. );
  1665. $expected = array(
  1666. array(
  1667. 'ModelName' => array(
  1668. 'id' => 1,
  1669. 'parent_id' => null
  1670. ),
  1671. 'children' => array(
  1672. array(
  1673. 'ModelName' => array(
  1674. 'id' => 2,
  1675. 'parent_id' => 1
  1676. ),
  1677. 'children' => array()
  1678. ),
  1679. array(
  1680. 'ModelName' => array(
  1681. 'id' => 3,
  1682. 'parent_id' => 1
  1683. ),
  1684. 'children' => array()
  1685. ),
  1686. array(
  1687. 'ModelName' => array(
  1688. 'id' => 4,
  1689. 'parent_id' => 1
  1690. ),
  1691. 'children' => array()
  1692. ),
  1693. array(
  1694. 'ModelName' => array(
  1695. 'id' => 5,
  1696. 'parent_id' => 1
  1697. ),
  1698. 'children' => array()
  1699. ),
  1700. )
  1701. ),
  1702. array(
  1703. 'ModelName' => array(
  1704. 'id' => 6,
  1705. 'parent_id' => null
  1706. ),
  1707. 'children' => array(
  1708. array(
  1709. 'ModelName' => array(
  1710. 'id' => 7,
  1711. 'parent_id' => 6
  1712. ),
  1713. 'children' => array()
  1714. ),
  1715. array(
  1716. 'ModelName' => array(
  1717. 'id' => 8,
  1718. 'parent_id' => 6
  1719. ),
  1720. 'children' => array()
  1721. ),
  1722. array(
  1723. 'ModelName' => array(
  1724. 'id' => 9,
  1725. 'parent_id' => 6
  1726. ),
  1727. 'children' => array()
  1728. ),
  1729. array(
  1730. 'ModelName' => array(
  1731. 'id' => 10,
  1732. 'parent_id' => 6
  1733. ),
  1734. 'children' => array()
  1735. )
  1736. )
  1737. )
  1738. );
  1739. $result = Hash::nest($input);
  1740. $this->assertEquals($expected, $result);
  1741. }
  1742. /**
  1743. * test Hash nest with a normal model result set, and a nominated root id
  1744. *
  1745. * @return void
  1746. */
  1747. public function testNestModelExplicitRoot() {
  1748. $input = array(
  1749. array(
  1750. 'ModelName' => array(
  1751. 'id' => 1,
  1752. 'parent_id' => null
  1753. ),
  1754. ),
  1755. array(
  1756. 'ModelName' => array(
  1757. 'id' => 2,
  1758. 'parent_id' => 1
  1759. ),
  1760. ),
  1761. array(
  1762. 'ModelName' => array(
  1763. 'id' => 3,
  1764. 'parent_id' => 1
  1765. ),
  1766. ),
  1767. array(
  1768. 'ModelName' => array(
  1769. 'id' => 4,
  1770. 'parent_id' => 1
  1771. ),
  1772. ),
  1773. array(
  1774. 'ModelName' => array(
  1775. 'id' => 5,
  1776. 'parent_id' => 1
  1777. ),
  1778. ),
  1779. array(
  1780. 'ModelName' => array(
  1781. 'id' => 6,
  1782. 'parent_id' => null
  1783. ),
  1784. ),
  1785. array(
  1786. 'ModelName' => array(
  1787. 'id' => 7,
  1788. 'parent_id' => 6
  1789. ),
  1790. ),
  1791. array(
  1792. 'ModelName' => array(
  1793. 'id' => 8,
  1794. 'parent_id' => 6
  1795. ),
  1796. ),
  1797. array(
  1798. 'ModelName' => array(
  1799. 'id' => 9,
  1800. 'parent_id' => 6
  1801. ),
  1802. ),
  1803. array(
  1804. 'ModelName' => array(
  1805. 'id' => 10,
  1806. 'parent_id' => 6
  1807. )
  1808. )
  1809. );
  1810. $expected = array(
  1811. array(
  1812. 'ModelName' => array(
  1813. 'id' => 6,
  1814. 'parent_id' => null
  1815. ),
  1816. 'children' => array(
  1817. array(
  1818. 'ModelName' => array(
  1819. 'id' => 7,
  1820. 'parent_id' => 6
  1821. ),
  1822. 'children' => array()
  1823. ),
  1824. array(
  1825. 'ModelName' => array(
  1826. 'id' => 8,
  1827. 'parent_id' => 6
  1828. ),
  1829. 'children' => array()
  1830. ),
  1831. array(
  1832. 'ModelName' => array(
  1833. 'id' => 9,
  1834. 'parent_id' => 6
  1835. ),
  1836. 'children' => array()
  1837. ),
  1838. array(
  1839. 'ModelName' => array(
  1840. 'id' => 10,
  1841. 'parent_id' => 6
  1842. ),
  1843. 'children' => array()
  1844. )
  1845. )
  1846. )
  1847. );
  1848. $result = Hash::nest($input, array('root' => 6));
  1849. $this->assertEquals($expected, $result);
  1850. }
  1851. /**
  1852. * test Hash nest with a 1d array - this method should be able to handle any type of array input
  1853. *
  1854. * @return void
  1855. */
  1856. public function testNest1Dimensional() {
  1857. $input = array(
  1858. array(
  1859. 'id' => 1,
  1860. 'parent_id' => null
  1861. ),
  1862. array(
  1863. 'id' => 2,
  1864. 'parent_id' => 1
  1865. ),
  1866. array(
  1867. 'id' => 3,
  1868. 'parent_id' => 1
  1869. ),
  1870. array(
  1871. 'id' => 4,
  1872. 'parent_id' => 1
  1873. ),
  1874. array(
  1875. 'id' => 5,
  1876. 'parent_id' => 1
  1877. ),
  1878. array(
  1879. 'id' => 6,
  1880. 'parent_id' => null
  1881. ),
  1882. array(
  1883. 'id' => 7,
  1884. 'parent_id' => 6
  1885. ),
  1886. array(
  1887. 'id' => 8,
  1888. 'parent_id' => 6
  1889. ),
  1890. array(
  1891. 'id' => 9,
  1892. 'parent_id' => 6
  1893. ),
  1894. array(
  1895. 'id' => 10,
  1896. 'parent_id' => 6
  1897. )
  1898. );
  1899. $expected = array(
  1900. array(
  1901. 'id' => 1,
  1902. 'parent_id' => null,
  1903. 'children' => array(
  1904. array(
  1905. 'id' => 2,
  1906. 'parent_id' => 1,
  1907. 'children' => array()
  1908. ),
  1909. array(
  1910. 'id' => 3,
  1911. 'parent_id' => 1,
  1912. 'children' => array()
  1913. ),
  1914. array(
  1915. 'id' => 4,
  1916. 'parent_id' => 1,
  1917. 'children' => array()
  1918. ),
  1919. array(
  1920. 'id' => 5,
  1921. 'parent_id' => 1,
  1922. 'children' => array()
  1923. ),
  1924. )
  1925. ),
  1926. array(
  1927. 'id' => 6,
  1928. 'parent_id' => null,
  1929. 'children' => array(
  1930. array(
  1931. 'id' => 7,
  1932. 'parent_id' => 6,
  1933. 'children' => array()
  1934. ),
  1935. array(
  1936. 'id' => 8,
  1937. 'parent_id' => 6,
  1938. 'children' => array()
  1939. ),
  1940. array(
  1941. 'id' => 9,
  1942. 'parent_id' => 6,
  1943. 'children' => array()
  1944. ),
  1945. array(
  1946. 'id' => 10,
  1947. 'parent_id' => 6,
  1948. 'children' => array()
  1949. )
  1950. )
  1951. )
  1952. );
  1953. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  1954. $this->assertEquals($expected, $result);
  1955. }
  1956. /**
  1957. * test Hash nest with no specified parent data.
  1958. *
  1959. * The result should be the same as the input.
  1960. * For an easier comparison, unset all the empty children arrays from the result
  1961. *
  1962. * @return void
  1963. */
  1964. public function testMissingParent() {
  1965. $input = array(
  1966. array(
  1967. 'id' => 1,
  1968. ),
  1969. array(
  1970. 'id' => 2,
  1971. ),
  1972. array(
  1973. 'id' => 3,
  1974. ),
  1975. array(
  1976. 'id' => 4,
  1977. ),
  1978. array(
  1979. 'id' => 5,
  1980. ),
  1981. array(
  1982. 'id' => 6,
  1983. ),
  1984. array(
  1985. 'id' => 7,
  1986. ),
  1987. array(
  1988. 'id' => 8,
  1989. ),
  1990. array(
  1991. 'id' => 9,
  1992. ),
  1993. array(
  1994. 'id' => 10,
  1995. )
  1996. );
  1997. $result = Hash::nest($input, array('idPath' => '{n}.id', 'parentPath' => '{n}.parent_id'));
  1998. foreach ($result as &$row) {
  1999. if (empty($row['children'])) {
  2000. unset($row['children']);
  2001. }
  2002. }
  2003. $this->assertEquals($input, $result);
  2004. }
  2005. /**
  2006. * testMergeDiff method
  2007. *
  2008. * @return void
  2009. */
  2010. public function testMergeDiff() {
  2011. $first = array(
  2012. 'ModelOne' => array(
  2013. 'id' => 1001,
  2014. 'field_one' => 'a1.m1.f1',
  2015. 'field_two' => 'a1.m1.f2'
  2016. )
  2017. );
  2018. $second = array(
  2019. 'ModelTwo' => array(
  2020. 'id' => 1002,
  2021. 'field_one' => 'a2.m2.f1',
  2022. 'field_two' => 'a2.m2.f2'
  2023. )
  2024. );
  2025. $result = Hash::mergeDiff($first, $second);
  2026. $this->assertEquals($result, $first + $second);
  2027. $result = Hash::mergeDiff($first, array());
  2028. $this->assertEquals($result, $first);
  2029. $result = Hash::mergeDiff(array(), $first);
  2030. $this->assertEquals($result, $first);
  2031. $third = array(
  2032. 'ModelOne' => array(
  2033. 'id' => 1003,
  2034. 'field_one' => 'a3.m1.f1',
  2035. 'field_two' => 'a3.m1.f2',
  2036. 'field_three' => 'a3.m1.f3'
  2037. )
  2038. );
  2039. $result = Hash::mergeDiff($first, $third);
  2040. $expected = array(
  2041. 'ModelOne' => array(
  2042. 'id' => 1001,
  2043. 'field_one' => 'a1.m1.f1',
  2044. 'field_two' => 'a1.m1.f2',
  2045. 'field_three' => 'a3.m1.f3'
  2046. )
  2047. );
  2048. $this->assertEquals($expected, $result);
  2049. $first = array(
  2050. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2051. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))
  2052. );
  2053. $second = array(
  2054. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2055. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))
  2056. );
  2057. $result = Hash::mergeDiff($first, $second);
  2058. $this->assertEquals($result, $first);
  2059. $third = array(
  2060. 0 => array(
  2061. 'ModelThree' => array(
  2062. 'id' => 1003,
  2063. 'field_one' => 's3.0.m3.f1',
  2064. 'field_two' => 's3.0.m3.f2'
  2065. )
  2066. )
  2067. );
  2068. $result = Hash::mergeDiff($first, $third);
  2069. $expected = array(
  2070. 0 => array(
  2071. 'ModelOne' => array(
  2072. 'id' => 1001,
  2073. 'field_one' => 's1.0.m1.f1',
  2074. 'field_two' => 's1.0.m1.f2'
  2075. ),
  2076. 'ModelThree' => array(
  2077. 'id' => 1003,
  2078. 'field_one' => 's3.0.m3.f1',
  2079. 'field_two' => 's3.0.m3.f2'
  2080. )
  2081. ),
  2082. 1 => array(
  2083. 'ModelTwo' => array(
  2084. 'id' => 1002,
  2085. 'field_one' => 's1.1.m2.f2',
  2086. 'field_two' => 's1.1.m2.f2'
  2087. )
  2088. )
  2089. );
  2090. $this->assertEquals($expected, $result);
  2091. $result = Hash::mergeDiff($first, null);
  2092. $this->assertEquals($result, $first);
  2093. $result = Hash::mergeDiff($first, $second);
  2094. $this->assertEquals($result, $first + $second);
  2095. }
  2096. /**
  2097. * Tests Hash::expand
  2098. *
  2099. * @return void
  2100. */
  2101. public function testExpand() {
  2102. $data = array('My', 'Array', 'To', 'Flatten');
  2103. $flat = Hash::flatten($data);
  2104. $result = Hash::expand($flat);
  2105. $this->assertEquals($data, $result);
  2106. $data = array(
  2107. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2108. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2109. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2110. '1.Author.user' => 'larry', '1.Author.password' => null
  2111. );
  2112. $result = Hash::expand($data);
  2113. $expected = array(
  2114. array(
  2115. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2116. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2117. ),
  2118. array(
  2119. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2120. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2121. )
  2122. );
  2123. $this->assertEquals($expected, $result);
  2124. $data = array(
  2125. '0/Post/id' => 1,
  2126. '0/Post/name' => 'test post'
  2127. );
  2128. $result = Hash::expand($data, '/');
  2129. $expected = array(
  2130. array(
  2131. 'Post' => array(
  2132. 'id' => 1,
  2133. 'name' => 'test post'
  2134. )
  2135. )
  2136. );
  2137. $this->assertEquals($expected, $result);
  2138. $data = array('a.b.100.a' => null, 'a.b.200.a' => null);
  2139. $expected = array(
  2140. 'a' => array(
  2141. 'b' => array(
  2142. 100 => array('a' => null),
  2143. 200 => array('a' => null)
  2144. )
  2145. )
  2146. );
  2147. $result = Hash::expand($data);
  2148. $this->assertEquals($expected, $result);
  2149. }
  2150. /**
  2151. * Test that flattening a large complex set doesn't loop forever.
  2152. *
  2153. * @return void
  2154. */
  2155. public function testFlattenInfiniteLoop() {
  2156. $data = array(
  2157. 'Order.ASI' => '0',
  2158. 'Order.Accounting' => '0',
  2159. 'Order.Admin' => '0',
  2160. 'Order.Art' => '0',
  2161. 'Order.ArtChecker' => '0',
  2162. 'Order.Canned' => '0',
  2163. 'Order.Customer_Tags' => '',
  2164. 'Order.Embroidery' => '0',
  2165. 'Order.Item.0.Product.style_number' => 'a11222',
  2166. 'Order.Item.0.Product.slug' => 'a11222',
  2167. 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
  2168. 'Order.Item.0.Product.Color.slug' => 'kelly_green',
  2169. 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
  2170. 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
  2171. 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
  2172. 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
  2173. 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
  2174. 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
  2175. 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
  2176. 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
  2177. 'Order.Item.0.Product.brand' => 'OUTER BANKS',
  2178. 'Order.Item.0.Product.style' => 'T-shirt',
  2179. 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
  2180. 'Order.Item.0.Product.sizes.0.Size.qty' => '',
  2181. 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
  2182. 'Order.Item.0.Product.sizes.0.Size.id' => '38',
  2183. 'Order.Item.0.Product.sizes.1.Size.qty' => '',
  2184. 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
  2185. 'Order.Item.0.Product.sizes.1.Size.id' => '39',
  2186. 'Order.Item.0.Product.sizes.2.Size.qty' => '78',
  2187. 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
  2188. 'Order.Item.0.Product.sizes.2.Size.id' => '40',
  2189. 'Order.Item.0.Product.sizes.3.Size.qty' => '',
  2190. 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
  2191. 'Order.Item.0.Product.sizes.3.Size.id' => '41',
  2192. 'Order.Item.0.Product.sizes.4.Size.qty' => '',
  2193. 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
  2194. 'Order.Item.0.Product.sizes.4.Size.id' => '42',
  2195. 'Order.Item.0.Art.imprint_locations.0.id' => 2,
  2196. 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
  2197. 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
  2198. 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
  2199. 'Order.Item.0.Art.imprint_locations.0.art' => '',
  2200. 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
  2201. 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
  2202. 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
  2203. 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
  2204. 'Order.Item.0.Art.imprint_locations.0.height' => 4,
  2205. 'Order.Item.0.Art.imprint_locations.0.width' => 5,
  2206. 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
  2207. 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
  2208. 'Order.Item.0.Art.imprint_locations.1.id' => 4,
  2209. 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
  2210. 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
  2211. 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
  2212. 'Order.Item.0.Art.imprint_locations.1.art' => '',
  2213. 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
  2214. 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
  2215. 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
  2216. 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
  2217. 'Order.Item.0.Art.imprint_locations.2.id' => 26,
  2218. 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
  2219. 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
  2220. 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
  2221. 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
  2222. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
  2223. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
  2224. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
  2225. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
  2226. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
  2227. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
  2228. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
  2229. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
  2230. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
  2231. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
  2232. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
  2233. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
  2234. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
  2235. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
  2236. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
  2237. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
  2238. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
  2239. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
  2240. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
  2241. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
  2242. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
  2243. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
  2244. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
  2245. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
  2246. 'Order.Job.0._id' => 'job-1',
  2247. 'Order.Job.0.type' => 'screenprinting',
  2248. 'Order.Job.0.postPress' => 'job-2',
  2249. 'Order.Job.1._id' => 'job-2',
  2250. 'Order.Job.1.type' => 'embroidery',
  2251. 'Order.Postpress' => '0',
  2252. 'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
  2253. 'Order.PriceAdjustment.0.adjustment' => '-20',
  2254. 'Order.PriceAdjustment.0.adjustment_type' => 'percent',
  2255. 'Order.PriceAdjustment.0.type' => 'grand_total',
  2256. 'Order.PriceAdjustment.1.adjustment' => '20',
  2257. 'Order.PriceAdjustment.1.adjustment_type' => 'flat',
  2258. 'Order.PriceAdjustment.1.min-items' => '10',
  2259. 'Order.PriceAdjustment.1.type' => 'min-items',
  2260. 'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
  2261. 'Order.Purchasing' => '0',
  2262. 'Order.QualityControl' => '0',
  2263. 'Order.Receiving' => '0',
  2264. 'Order.ScreenPrinting' => '0',
  2265. 'Order.Stage.art_approval' => 0,
  2266. 'Order.Stage.draft' => 1,
  2267. 'Order.Stage.quote' => 1,
  2268. 'Order.Stage.order' => 1,
  2269. 'Order.StoreLiason' => '0',
  2270. 'Order.Tag_UI_Email' => '',
  2271. 'Order.Tags' => '',
  2272. 'Order._id' => 'test-2',
  2273. 'Order.add_print_location' => '',
  2274. 'Order.created' => '2011-Dec-29 05:40:18',
  2275. 'Order.force_admin' => '0',
  2276. 'Order.modified' => '2012-Jul-25 01:24:49',
  2277. 'Order.name' => 'towering power',
  2278. 'Order.order_id' => '135961',
  2279. 'Order.slug' => 'test-2',
  2280. 'Order.title' => 'test job 2',
  2281. 'Order.type' => 'ttt'
  2282. );
  2283. $expanded = Hash::expand($data);
  2284. $flattened = Hash::flatten($expanded);
  2285. $this->assertEquals($data, $flattened);
  2286. }
  2287. }