HashTest.php 81 KB

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