HashTest.php 81 KB

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