HashTest.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704
  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 wildcard matcher
  748. *
  749. * @return void
  750. */
  751. public function testExtractWildcard()
  752. {
  753. $data = [
  754. '02000009C5560001' => ['name' => 'Mr. Alphanumeric'],
  755. '2300000918020101' => ['name' => 'Mr. Numeric'],
  756. '390000096AB30001' => ['name' => 'Mrs. Alphanumeric'],
  757. 'stuff' => ['name' => 'Ms. Word'],
  758. 123 => ['name' => 'Mr. Number'],
  759. true => ['name' => 'Ms. Bool'],
  760. ];
  761. $result = Hash::extract($data, '{*}.name');
  762. $expected = [
  763. 'Mr. Alphanumeric',
  764. 'Mr. Numeric',
  765. 'Mrs. Alphanumeric',
  766. 'Ms. Word',
  767. 'Mr. Number',
  768. 'Ms. Bool',
  769. ];
  770. $this->assertEquals($expected, $result);
  771. }
  772. /**
  773. * Test the attribute presense selector.
  774. *
  775. * @return void
  776. */
  777. public function testExtractAttributePresence()
  778. {
  779. $data = static::articleData();
  780. $result = Hash::extract($data, '{n}.Article[published]');
  781. $expected = [$data[1]['Article']];
  782. $this->assertEquals($expected, $result);
  783. $result = Hash::extract($data, '{n}.Article[id][published]');
  784. $expected = [$data[1]['Article']];
  785. $this->assertEquals($expected, $result);
  786. }
  787. /**
  788. * Test = and != operators.
  789. *
  790. * @return void
  791. */
  792. public function testExtractAttributeEquality()
  793. {
  794. $data = static::articleData();
  795. $result = Hash::extract($data, '{n}.Article[id=3]');
  796. $expected = [$data[2]['Article']];
  797. $this->assertEquals($expected, $result);
  798. $result = Hash::extract($data, '{n}.Article[id = 3]');
  799. $expected = [$data[2]['Article']];
  800. $this->assertEquals($expected, $result, 'Whitespace should not matter.');
  801. $result = Hash::extract($data, '{n}.Article[id!=3]');
  802. $this->assertEquals(1, $result[0]['id']);
  803. $this->assertEquals(2, $result[1]['id']);
  804. $this->assertEquals(4, $result[2]['id']);
  805. $this->assertEquals(5, $result[3]['id']);
  806. }
  807. /**
  808. * Test extracting based on attributes with boolean values.
  809. *
  810. * @return void
  811. */
  812. public function testExtractAttributeBoolean()
  813. {
  814. $users = [
  815. [
  816. 'id' => 2,
  817. 'username' => 'johndoe',
  818. 'active' => true
  819. ],
  820. [
  821. 'id' => 5,
  822. 'username' => 'kevin',
  823. 'active' => true
  824. ],
  825. [
  826. 'id' => 9,
  827. 'username' => 'samantha',
  828. 'active' => false
  829. ],
  830. ];
  831. $result = Hash::extract($users, '{n}[active=0]');
  832. $this->assertCount(1, $result);
  833. $this->assertEquals($users[2], $result[0]);
  834. $result = Hash::extract($users, '{n}[active=false]');
  835. $this->assertCount(1, $result);
  836. $this->assertEquals($users[2], $result[0]);
  837. $result = Hash::extract($users, '{n}[active=1]');
  838. $this->assertCount(2, $result);
  839. $this->assertEquals($users[0], $result[0]);
  840. $this->assertEquals($users[1], $result[1]);
  841. $result = Hash::extract($users, '{n}[active=true]');
  842. $this->assertCount(2, $result);
  843. $this->assertEquals($users[0], $result[0]);
  844. $this->assertEquals($users[1], $result[1]);
  845. }
  846. /**
  847. * Test that attribute matchers don't cause errors on scalar data.
  848. *
  849. * @return void
  850. */
  851. public function testExtractAttributeEqualityOnScalarValue()
  852. {
  853. $data = [
  854. 'Entity' => [
  855. 'id' => 1,
  856. 'data1' => 'value',
  857. ]
  858. ];
  859. $result = Hash::extract($data, 'Entity[id=1].data1');
  860. $this->assertEquals(['value'], $result);
  861. $data = ['Entity' => false ];
  862. $result = Hash::extract($data, 'Entity[id=1].data1');
  863. $this->assertEquals([], $result);
  864. }
  865. /**
  866. * Test comparison operators.
  867. *
  868. * @return void
  869. */
  870. public function testExtractAttributeComparison()
  871. {
  872. $data = static::articleData();
  873. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2]');
  874. $expected = [$data[0]['Comment'][1]];
  875. $this->assertEquals($expected, $result);
  876. $this->assertEquals(4, $expected[0]['user_id']);
  877. $result = Hash::extract($data, '{n}.Comment.{n}[user_id >= 4]');
  878. $expected = [$data[0]['Comment'][1]];
  879. $this->assertEquals($expected, $result);
  880. $this->assertEquals(4, $expected[0]['user_id']);
  881. $result = Hash::extract($data, '{n}.Comment.{n}[user_id < 3]');
  882. $expected = [$data[0]['Comment'][0]];
  883. $this->assertEquals($expected, $result);
  884. $this->assertEquals(2, $expected[0]['user_id']);
  885. $result = Hash::extract($data, '{n}.Comment.{n}[user_id <= 2]');
  886. $expected = [$data[0]['Comment'][0]];
  887. $this->assertEquals($expected, $result);
  888. $this->assertEquals(2, $expected[0]['user_id']);
  889. }
  890. /**
  891. * Test multiple attributes with conditions.
  892. *
  893. * @return void
  894. */
  895. public function testExtractAttributeMultiple()
  896. {
  897. $data = static::articleData();
  898. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=1]');
  899. $this->assertEmpty($result);
  900. $result = Hash::extract($data, '{n}.Comment.{n}[user_id > 2][id=2]');
  901. $expected = [$data[0]['Comment'][1]];
  902. $this->assertEquals($expected, $result);
  903. $this->assertEquals(4, $expected[0]['user_id']);
  904. }
  905. /**
  906. * Test attribute pattern matching.
  907. *
  908. * @return void
  909. */
  910. public function testExtractAttributePattern()
  911. {
  912. $data = static::articleData();
  913. $result = Hash::extract($data, '{n}.Article[title=/^First/]');
  914. $expected = [$data[0]['Article']];
  915. $this->assertEquals($expected, $result);
  916. $result = Hash::extract($data, '{n}.Article[title=/^Fir[a-z]+/]');
  917. $expected = [$data[0]['Article']];
  918. $this->assertEquals($expected, $result);
  919. }
  920. /**
  921. * Test that extract() + matching can hit null things.
  922. *
  923. * @return void
  924. */
  925. public function testExtractMatchesNull()
  926. {
  927. $data = [
  928. 'Country' => [
  929. ['name' => 'Canada'],
  930. ['name' => 'Australia'],
  931. ['name' => null],
  932. ]
  933. ];
  934. $result = Hash::extract($data, 'Country.{n}[name=/Canada|^$/]');
  935. $expected = [
  936. [
  937. 'name' => 'Canada',
  938. ],
  939. [
  940. 'name' => null,
  941. ],
  942. ];
  943. $this->assertEquals($expected, $result);
  944. }
  945. /**
  946. * Test that uneven keys are handled correctly.
  947. *
  948. * @return void
  949. */
  950. public function testExtractUnevenKeys()
  951. {
  952. $data = [
  953. 'Level1' => [
  954. 'Level2' => ['test1', 'test2'],
  955. 'Level2bis' => ['test3', 'test4']
  956. ]
  957. ];
  958. $this->assertEquals(
  959. ['test1', 'test2'],
  960. Hash::extract($data, 'Level1.Level2')
  961. );
  962. $this->assertEquals(
  963. ['test3', 'test4'],
  964. Hash::extract($data, 'Level1.Level2bis')
  965. );
  966. $data = [
  967. 'Level1' => [
  968. 'Level2bis' => [
  969. ['test3', 'test4'],
  970. ['test5', 'test6']
  971. ]
  972. ]
  973. ];
  974. $expected = [
  975. ['test3', 'test4'],
  976. ['test5', 'test6']
  977. ];
  978. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  979. $data['Level1']['Level2'] = ['test1', 'test2'];
  980. $this->assertEquals($expected, Hash::extract($data, 'Level1.Level2bis'));
  981. }
  982. /**
  983. * testSort method
  984. *
  985. * @return void
  986. */
  987. public function testSort()
  988. {
  989. $result = Hash::sort([], '{n}.name');
  990. $this->assertEquals([], $result);
  991. $a = [
  992. 0 => [
  993. 'Person' => ['name' => 'Jeff'],
  994. 'Friend' => [['name' => 'Nate']]
  995. ],
  996. 1 => [
  997. 'Person' => ['name' => 'Tracy'],
  998. 'Friend' => [['name' => 'Lindsay']]
  999. ]
  1000. ];
  1001. $b = [
  1002. 0 => [
  1003. 'Person' => ['name' => 'Tracy'],
  1004. 'Friend' => [['name' => 'Lindsay']]
  1005. ],
  1006. 1 => [
  1007. 'Person' => ['name' => 'Jeff'],
  1008. 'Friend' => [['name' => 'Nate']]
  1009. ]
  1010. ];
  1011. $a = Hash::sort($a, '{n}.Friend.{n}.name');
  1012. $this->assertEquals($a, $b);
  1013. $b = [
  1014. 0 => [
  1015. 'Person' => ['name' => 'Jeff'],
  1016. 'Friend' => [['name' => 'Nate']]
  1017. ],
  1018. 1 => [
  1019. 'Person' => ['name' => 'Tracy'],
  1020. 'Friend' => [['name' => 'Lindsay']]
  1021. ]
  1022. ];
  1023. $a = [
  1024. 0 => [
  1025. 'Person' => ['name' => 'Tracy'],
  1026. 'Friend' => [['name' => 'Lindsay']]
  1027. ],
  1028. 1 => [
  1029. 'Person' => ['name' => 'Jeff'],
  1030. 'Friend' => [['name' => 'Nate']]
  1031. ]
  1032. ];
  1033. $a = Hash::sort($a, '{n}.Friend.{n}.name', 'desc');
  1034. $this->assertEquals($a, $b);
  1035. $a = [
  1036. 0 => [
  1037. 'Person' => ['name' => 'Jeff'],
  1038. 'Friend' => [['name' => 'Nate']]
  1039. ],
  1040. 1 => [
  1041. 'Person' => ['name' => 'Tracy'],
  1042. 'Friend' => [['name' => 'Lindsay']]
  1043. ],
  1044. 2 => [
  1045. 'Person' => ['name' => 'Adam'],
  1046. 'Friend' => [['name' => 'Bob']]
  1047. ]
  1048. ];
  1049. $b = [
  1050. 0 => [
  1051. 'Person' => ['name' => 'Adam'],
  1052. 'Friend' => [['name' => 'Bob']]
  1053. ],
  1054. 1 => [
  1055. 'Person' => ['name' => 'Jeff'],
  1056. 'Friend' => [['name' => 'Nate']]
  1057. ],
  1058. 2 => [
  1059. 'Person' => ['name' => 'Tracy'],
  1060. 'Friend' => [['name' => 'Lindsay']]
  1061. ]
  1062. ];
  1063. $a = Hash::sort($a, '{n}.Person.name', 'asc');
  1064. $this->assertEquals($a, $b);
  1065. $a = [
  1066. 0 => ['Person' => ['name' => 'Jeff']],
  1067. 1 => ['Shirt' => ['color' => 'black']]
  1068. ];
  1069. $b = [
  1070. 0 => ['Shirt' => ['color' => 'black']],
  1071. 1 => ['Person' => ['name' => 'Jeff']],
  1072. ];
  1073. $a = Hash::sort($a, '{n}.Person.name', 'ASC', 'STRING');
  1074. $this->assertSame($a, $b);
  1075. $names = [
  1076. ['employees' => [
  1077. ['name' => ['first' => 'John', 'last' => 'Doe']]]
  1078. ],
  1079. ['employees' => [
  1080. ['name' => ['first' => 'Jane', 'last' => 'Doe']]]
  1081. ],
  1082. ['employees' => [['name' => []]]],
  1083. ['employees' => [['name' => []]]]
  1084. ];
  1085. $result = Hash::sort($names, '{n}.employees.0.name', 'asc');
  1086. $expected = [
  1087. ['employees' => [
  1088. ['name' => ['first' => 'John', 'last' => 'Doe']]]
  1089. ],
  1090. ['employees' => [
  1091. ['name' => ['first' => 'Jane', 'last' => 'Doe']]]
  1092. ],
  1093. ['employees' => [['name' => []]]],
  1094. ['employees' => [['name' => []]]]
  1095. ];
  1096. $this->assertSame($expected, $result);
  1097. $a = [
  1098. 'SU' => [
  1099. 'total_fulfillable' => 2
  1100. ],
  1101. 'AA' => [
  1102. 'total_fulfillable' => 1
  1103. ],
  1104. 'LX' => [
  1105. 'total_fulfillable' => 0
  1106. ],
  1107. 'BL' => [
  1108. 'total_fulfillable' => 3
  1109. ],
  1110. ];
  1111. $expected = [
  1112. 'LX' => [
  1113. 'total_fulfillable' => 0
  1114. ],
  1115. 'AA' => [
  1116. 'total_fulfillable' => 1
  1117. ],
  1118. 'SU' => [
  1119. 'total_fulfillable' => 2
  1120. ],
  1121. 'BL' => [
  1122. 'total_fulfillable' => 3
  1123. ],
  1124. ];
  1125. $result = Hash::sort($a, '{s}.total_fulfillable', 'asc');
  1126. $this->assertSame($expected, $result);
  1127. }
  1128. /**
  1129. * Test sort() with numeric option.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testSortNumeric()
  1134. {
  1135. $items = [
  1136. ['Item' => ['price' => '155,000']],
  1137. ['Item' => ['price' => '139,000']],
  1138. ['Item' => ['price' => '275,622']],
  1139. ['Item' => ['price' => '230,888']],
  1140. ['Item' => ['price' => '66,000']],
  1141. ];
  1142. $result = Hash::sort($items, '{n}.Item.price', 'asc', 'numeric');
  1143. $expected = [
  1144. ['Item' => ['price' => '66,000']],
  1145. ['Item' => ['price' => '139,000']],
  1146. ['Item' => ['price' => '155,000']],
  1147. ['Item' => ['price' => '230,888']],
  1148. ['Item' => ['price' => '275,622']],
  1149. ];
  1150. $this->assertEquals($expected, $result);
  1151. $result = Hash::sort($items, '{n}.Item.price', 'desc', 'numeric');
  1152. $expected = [
  1153. ['Item' => ['price' => '275,622']],
  1154. ['Item' => ['price' => '230,888']],
  1155. ['Item' => ['price' => '155,000']],
  1156. ['Item' => ['price' => '139,000']],
  1157. ['Item' => ['price' => '66,000']],
  1158. ];
  1159. $this->assertEquals($expected, $result);
  1160. }
  1161. /**
  1162. * Test natural sorting.
  1163. *
  1164. * @return void
  1165. */
  1166. public function testSortNatural()
  1167. {
  1168. $items = [
  1169. ['Item' => ['image' => 'img1.jpg']],
  1170. ['Item' => ['image' => 'img99.jpg']],
  1171. ['Item' => ['image' => 'img12.jpg']],
  1172. ['Item' => ['image' => 'img10.jpg']],
  1173. ['Item' => ['image' => 'img2.jpg']],
  1174. ];
  1175. $result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural');
  1176. $expected = [
  1177. ['Item' => ['image' => 'img99.jpg']],
  1178. ['Item' => ['image' => 'img12.jpg']],
  1179. ['Item' => ['image' => 'img10.jpg']],
  1180. ['Item' => ['image' => 'img2.jpg']],
  1181. ['Item' => ['image' => 'img1.jpg']],
  1182. ];
  1183. $this->assertEquals($expected, $result);
  1184. $result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural');
  1185. $expected = [
  1186. ['Item' => ['image' => 'img1.jpg']],
  1187. ['Item' => ['image' => 'img2.jpg']],
  1188. ['Item' => ['image' => 'img10.jpg']],
  1189. ['Item' => ['image' => 'img12.jpg']],
  1190. ['Item' => ['image' => 'img99.jpg']],
  1191. ];
  1192. $this->assertEquals($expected, $result);
  1193. }
  1194. /**
  1195. * Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
  1196. *
  1197. * @return void
  1198. */
  1199. public function testSortNaturalFallbackToRegular()
  1200. {
  1201. $a = [
  1202. 0 => ['Person' => ['name' => 'Jeff']],
  1203. 1 => ['Shirt' => ['color' => 'black']]
  1204. ];
  1205. $b = [
  1206. 0 => ['Shirt' => ['color' => 'black']],
  1207. 1 => ['Person' => ['name' => 'Jeff']],
  1208. ];
  1209. $sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
  1210. $this->assertEquals($sorted, $b);
  1211. }
  1212. /**
  1213. * test sorting with out of order keys.
  1214. *
  1215. * @return void
  1216. */
  1217. public function testSortWithOutOfOrderKeys()
  1218. {
  1219. $data = [
  1220. 9 => ['class' => 510, 'test2' => 2],
  1221. 1 => ['class' => 500, 'test2' => 1],
  1222. 2 => ['class' => 600, 'test2' => 2],
  1223. 5 => ['class' => 625, 'test2' => 4],
  1224. 0 => ['class' => 605, 'test2' => 3],
  1225. ];
  1226. $expected = [
  1227. ['class' => 500, 'test2' => 1],
  1228. ['class' => 510, 'test2' => 2],
  1229. ['class' => 600, 'test2' => 2],
  1230. ['class' => 605, 'test2' => 3],
  1231. ['class' => 625, 'test2' => 4],
  1232. ];
  1233. $result = Hash::sort($data, '{n}.class', 'asc');
  1234. $this->assertEquals($expected, $result);
  1235. $result = Hash::sort($data, '{n}.test2', 'asc');
  1236. $this->assertEquals($expected, $result);
  1237. }
  1238. /**
  1239. * test sorting with string keys.
  1240. *
  1241. * @return void
  1242. */
  1243. public function testSortString()
  1244. {
  1245. $toSort = [
  1246. 'four' => ['number' => 4, 'some' => 'foursome'],
  1247. 'six' => ['number' => 6, 'some' => 'sixsome'],
  1248. 'five' => ['number' => 5, 'some' => 'fivesome'],
  1249. 'two' => ['number' => 2, 'some' => 'twosome'],
  1250. 'three' => ['number' => 3, 'some' => 'threesome']
  1251. ];
  1252. $sorted = Hash::sort($toSort, '{s}.number', 'asc');
  1253. $expected = [
  1254. 'two' => ['number' => 2, 'some' => 'twosome'],
  1255. 'three' => ['number' => 3, 'some' => 'threesome'],
  1256. 'four' => ['number' => 4, 'some' => 'foursome'],
  1257. 'five' => ['number' => 5, 'some' => 'fivesome'],
  1258. 'six' => ['number' => 6, 'some' => 'sixsome']
  1259. ];
  1260. $this->assertEquals($expected, $sorted);
  1261. $menus = [
  1262. 'blogs' => ['title' => 'Blogs', 'weight' => 3],
  1263. 'comments' => ['title' => 'Comments', 'weight' => 2],
  1264. 'users' => ['title' => 'Users', 'weight' => 1],
  1265. ];
  1266. $expected = [
  1267. 'users' => ['title' => 'Users', 'weight' => 1],
  1268. 'comments' => ['title' => 'Comments', 'weight' => 2],
  1269. 'blogs' => ['title' => 'Blogs', 'weight' => 3],
  1270. ];
  1271. $result = Hash::sort($menus, '{s}.weight', 'ASC');
  1272. $this->assertEquals($expected, $result);
  1273. }
  1274. /**
  1275. * Test insert()
  1276. *
  1277. * @return void
  1278. */
  1279. public function testInsertSimple()
  1280. {
  1281. $a = [
  1282. 'pages' => ['name' => 'page']
  1283. ];
  1284. $result = Hash::insert($a, 'files', ['name' => 'files']);
  1285. $expected = [
  1286. 'pages' => ['name' => 'page'],
  1287. 'files' => ['name' => 'files']
  1288. ];
  1289. $this->assertEquals($expected, $result);
  1290. $a = [
  1291. 'pages' => ['name' => 'page']
  1292. ];
  1293. $result = Hash::insert($a, 'pages.name', []);
  1294. $expected = [
  1295. 'pages' => ['name' => []],
  1296. ];
  1297. $this->assertEquals($expected, $result);
  1298. $a = [
  1299. 'foo' => ['bar' => 'baz']
  1300. ];
  1301. $result = Hash::insert($a, 'some.0123.path', ['foo' => ['bar' => 'baz']]);
  1302. $expected = ['foo' => ['bar' => 'baz']];
  1303. $this->assertEquals($expected, Hash::get($result, 'some.0123.path'));
  1304. }
  1305. /**
  1306. * Test inserting with multiple values.
  1307. *
  1308. * @return void
  1309. */
  1310. public function testInsertMulti()
  1311. {
  1312. $data = static::articleData();
  1313. $result = Hash::insert($data, '{n}.Article.insert', 'value');
  1314. $this->assertEquals('value', $result[0]['Article']['insert']);
  1315. $this->assertEquals('value', $result[1]['Article']['insert']);
  1316. $result = Hash::insert($data, '{n}.Comment.{n}.insert', 'value');
  1317. $this->assertEquals('value', $result[0]['Comment'][0]['insert']);
  1318. $this->assertEquals('value', $result[0]['Comment'][1]['insert']);
  1319. $data = [
  1320. 0 => ['Item' => ['id' => 1, 'title' => 'first']],
  1321. 1 => ['Item' => ['id' => 2, 'title' => 'second']],
  1322. 2 => ['Item' => ['id' => 3, 'title' => 'third']],
  1323. 3 => ['Item' => ['id' => 4, 'title' => 'fourth']],
  1324. 4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
  1325. ];
  1326. $result = Hash::insert($data, '{n}.Item[id=/\b2|\b4/]', ['test' => 2]);
  1327. $expected = [
  1328. 0 => ['Item' => ['id' => 1, 'title' => 'first']],
  1329. 1 => ['Item' => ['id' => 2, 'title' => 'second', 'test' => 2]],
  1330. 2 => ['Item' => ['id' => 3, 'title' => 'third']],
  1331. 3 => ['Item' => ['id' => 4, 'title' => 'fourth', 'test' => 2]],
  1332. 4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
  1333. ];
  1334. $this->assertEquals($expected, $result);
  1335. }
  1336. /**
  1337. * Test that insert() can insert data over a string value.
  1338. *
  1339. * @return void
  1340. */
  1341. public function testInsertOverwriteStringValue()
  1342. {
  1343. $data = [
  1344. 'Some' => [
  1345. 'string' => 'value'
  1346. ]
  1347. ];
  1348. $result = Hash::insert($data, 'Some.string.value', ['values']);
  1349. $expected = [
  1350. 'Some' => [
  1351. 'string' => [
  1352. 'value' => ['values']
  1353. ]
  1354. ]
  1355. ];
  1356. $this->assertEquals($expected, $result);
  1357. }
  1358. /**
  1359. * Test remove() method.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testRemove()
  1364. {
  1365. $a = [
  1366. 'pages' => ['name' => 'page'],
  1367. 'files' => ['name' => 'files']
  1368. ];
  1369. $result = Hash::remove($a, 'files');
  1370. $expected = [
  1371. 'pages' => ['name' => 'page']
  1372. ];
  1373. $this->assertEquals($expected, $result);
  1374. $a = [
  1375. 'pages' => [
  1376. 0 => ['name' => 'main'],
  1377. 1 => [
  1378. 'name' => 'about',
  1379. 'vars' => ['title' => 'page title']
  1380. ]
  1381. ]
  1382. ];
  1383. $result = Hash::remove($a, 'pages.1.vars');
  1384. $expected = [
  1385. 'pages' => [
  1386. 0 => ['name' => 'main'],
  1387. 1 => ['name' => 'about']
  1388. ]
  1389. ];
  1390. $this->assertEquals($expected, $result);
  1391. $result = Hash::remove($a, 'pages.2.vars');
  1392. $expected = $a;
  1393. $this->assertEquals($expected, $result);
  1394. $a = [
  1395. 0 => [
  1396. 'name' => 'pages'
  1397. ],
  1398. 1 => [
  1399. 'name' => 'files'
  1400. ]
  1401. ];
  1402. $result = Hash::remove($a, '{n}[name=files]');
  1403. $expected = [
  1404. 0 => [
  1405. 'name' => 'pages'
  1406. ]
  1407. ];
  1408. $this->assertEquals($expected, $result);
  1409. $array = [
  1410. 0 => 'foo',
  1411. 1 => [
  1412. 0 => 'baz'
  1413. ]
  1414. ];
  1415. $expected = $array;
  1416. $result = Hash::remove($array, '{n}.part');
  1417. $this->assertEquals($expected, $result);
  1418. $result = Hash::remove($array, '{n}.{n}.part');
  1419. $this->assertEquals($expected, $result);
  1420. }
  1421. /**
  1422. * Test removing multiple values.
  1423. *
  1424. * @return void
  1425. */
  1426. public function testRemoveMulti()
  1427. {
  1428. $data = static::articleData();
  1429. $result = Hash::remove($data, '{n}.Article.title');
  1430. $this->assertFalse(isset($result[0]['Article']['title']));
  1431. $this->assertFalse(isset($result[1]['Article']['title']));
  1432. $result = Hash::remove($data, '{n}.Article.{s}');
  1433. $this->assertFalse(isset($result[0]['Article']['id']));
  1434. $this->assertFalse(isset($result[0]['Article']['user_id']));
  1435. $this->assertFalse(isset($result[0]['Article']['title']));
  1436. $this->assertFalse(isset($result[0]['Article']['body']));
  1437. $data = [
  1438. 0 => ['Item' => ['id' => 1, 'title' => 'first']],
  1439. 1 => ['Item' => ['id' => 2, 'title' => 'second']],
  1440. 2 => ['Item' => ['id' => 3, 'title' => 'third']],
  1441. 3 => ['Item' => ['id' => 4, 'title' => 'fourth']],
  1442. 4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
  1443. ];
  1444. $result = Hash::remove($data, '{n}.Item[id=/\b2|\b4/]');
  1445. $expected = [
  1446. 0 => ['Item' => ['id' => 1, 'title' => 'first']],
  1447. 2 => ['Item' => ['id' => 3, 'title' => 'third']],
  1448. 4 => ['Item' => ['id' => 5, 'title' => 'fifth']],
  1449. ];
  1450. $this->assertEquals($expected, $result);
  1451. }
  1452. /**
  1453. * testCheck method
  1454. *
  1455. * @return void
  1456. */
  1457. public function testCheck()
  1458. {
  1459. $set = [
  1460. 'My Index 1' => ['First' => 'The first item']
  1461. ];
  1462. $this->assertTrue(Hash::check($set, 'My Index 1.First'));
  1463. $this->assertTrue(Hash::check($set, 'My Index 1'));
  1464. $set = [
  1465. 'My Index 1' => [
  1466. 'First' => [
  1467. 'Second' => [
  1468. 'Third' => [
  1469. 'Fourth' => 'Heavy. Nesting.'
  1470. ]
  1471. ]
  1472. ]
  1473. ]
  1474. ];
  1475. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second'));
  1476. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third'));
  1477. $this->assertTrue(Hash::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1478. $this->assertFalse(Hash::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1479. }
  1480. /**
  1481. * testCombine method
  1482. *
  1483. * @return void
  1484. */
  1485. public function testCombine()
  1486. {
  1487. $result = Hash::combine([], '{n}.User.id', '{n}.User.Data');
  1488. $this->assertTrue(empty($result));
  1489. $a = static::userData();
  1490. $result = Hash::combine($a, '{n}.User.id');
  1491. $expected = [2 => null, 14 => null, 25 => null];
  1492. $this->assertEquals($expected, $result);
  1493. $result = Hash::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1494. $expected = [2 => null, 14 => null, 25 => null];
  1495. $this->assertEquals($expected, $result);
  1496. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data');
  1497. $expected = [
  1498. 2 => ['user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'],
  1499. 14 => ['user' => 'phpnut', 'name' => 'Larry E. Masters'],
  1500. 25 => ['user' => 'gwoo', 'name' => 'The Gwoo']];
  1501. $this->assertEquals($expected, $result);
  1502. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1503. $expected = [
  1504. 2 => 'Mariano Iglesias',
  1505. 14 => 'Larry E. Masters',
  1506. 25 => 'The Gwoo'];
  1507. $this->assertEquals($expected, $result);
  1508. }
  1509. /**
  1510. * test combine() giving errors on key/value length mismatches.
  1511. *
  1512. * @expectedException \RuntimeException
  1513. * @return void
  1514. */
  1515. public function testCombineErrorMissingValue()
  1516. {
  1517. $data = [
  1518. ['User' => ['id' => 1, 'name' => 'mark']],
  1519. ['User' => ['name' => 'jose']],
  1520. ];
  1521. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1522. }
  1523. /**
  1524. * test combine() giving errors on key/value length mismatches.
  1525. *
  1526. * @expectedException \RuntimeException
  1527. * @return void
  1528. */
  1529. public function testCombineErrorMissingKey()
  1530. {
  1531. $data = [
  1532. ['User' => ['id' => 1, 'name' => 'mark']],
  1533. ['User' => ['id' => 2]],
  1534. ];
  1535. Hash::combine($data, '{n}.User.id', '{n}.User.name');
  1536. }
  1537. /**
  1538. * test combine() with a group path.
  1539. *
  1540. * @return void
  1541. */
  1542. public function testCombineWithGroupPath()
  1543. {
  1544. $a = static::userData();
  1545. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1546. $expected = [
  1547. 1 => [
  1548. 2 => ['user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'],
  1549. 25 => ['user' => 'gwoo', 'name' => 'The Gwoo']
  1550. ],
  1551. 2 => [
  1552. 14 => ['user' => 'phpnut', 'name' => 'Larry E. Masters']
  1553. ]
  1554. ];
  1555. $this->assertEquals($expected, $result);
  1556. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1557. $expected = [
  1558. 1 => [
  1559. 2 => 'Mariano Iglesias',
  1560. 25 => 'The Gwoo'
  1561. ],
  1562. 2 => [
  1563. 14 => 'Larry E. Masters'
  1564. ]
  1565. ];
  1566. $this->assertEquals($expected, $result);
  1567. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1568. $expected = [
  1569. 1 => [
  1570. 2 => ['user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'],
  1571. 25 => ['user' => 'gwoo', 'name' => 'The Gwoo']
  1572. ],
  1573. 2 => [
  1574. 14 => ['user' => 'phpnut', 'name' => 'Larry E. Masters']
  1575. ]
  1576. ];
  1577. $this->assertEquals($expected, $result);
  1578. $result = Hash::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1579. $expected = [
  1580. 1 => [
  1581. 2 => 'Mariano Iglesias',
  1582. 25 => 'The Gwoo'
  1583. ],
  1584. 2 => [
  1585. 14 => 'Larry E. Masters'
  1586. ]
  1587. ];
  1588. $this->assertEquals($expected, $result);
  1589. }
  1590. /**
  1591. * Test combine with formatting rules.
  1592. *
  1593. * @return void
  1594. */
  1595. public function testCombineWithFormatting()
  1596. {
  1597. $a = static::userData();
  1598. $result = Hash::combine(
  1599. $a,
  1600. '{n}.User.id',
  1601. ['%1$s: %2$s', '{n}.User.Data.user', '{n}.User.Data.name'],
  1602. '{n}.User.group_id'
  1603. );
  1604. $expected = [
  1605. 1 => [
  1606. 2 => 'mariano.iglesias: Mariano Iglesias',
  1607. 25 => 'gwoo: The Gwoo'
  1608. ],
  1609. 2 => [
  1610. 14 => 'phpnut: Larry E. Masters'
  1611. ]
  1612. ];
  1613. $this->assertEquals($expected, $result);
  1614. $result = Hash::combine(
  1615. $a,
  1616. [
  1617. '%s: %s',
  1618. '{n}.User.Data.user',
  1619. '{n}.User.Data.name'
  1620. ],
  1621. '{n}.User.id'
  1622. );
  1623. $expected = [
  1624. 'mariano.iglesias: Mariano Iglesias' => 2,
  1625. 'phpnut: Larry E. Masters' => 14,
  1626. 'gwoo: The Gwoo' => 25
  1627. ];
  1628. $this->assertEquals($expected, $result);
  1629. $result = Hash::combine(
  1630. $a,
  1631. ['%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'],
  1632. '{n}.User.Data.name'
  1633. );
  1634. $expected = [
  1635. 'mariano.iglesias: 2' => 'Mariano Iglesias',
  1636. 'phpnut: 14' => 'Larry E. Masters',
  1637. 'gwoo: 25' => 'The Gwoo'
  1638. ];
  1639. $this->assertEquals($expected, $result);
  1640. $result = Hash::combine(
  1641. $a,
  1642. ['%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'],
  1643. '{n}.User.Data.name'
  1644. );
  1645. $expected = [
  1646. '2: mariano.iglesias' => 'Mariano Iglesias',
  1647. '14: phpnut' => 'Larry E. Masters',
  1648. '25: gwoo' => 'The Gwoo'
  1649. ];
  1650. $this->assertEquals($expected, $result);
  1651. }
  1652. /**
  1653. * testFormat method
  1654. *
  1655. * @return void
  1656. */
  1657. public function testFormat()
  1658. {
  1659. $data = static::userData();
  1660. $result = Hash::format(
  1661. $data,
  1662. ['{n}.User.Data.user', '{n}.User.id'],
  1663. '%s, %s'
  1664. );
  1665. $expected = [
  1666. 'mariano.iglesias, 2',
  1667. 'phpnut, 14',
  1668. 'gwoo, 25'
  1669. ];
  1670. $this->assertEquals($expected, $result);
  1671. $result = Hash::format(
  1672. $data,
  1673. ['{n}.User.Data.user', '{n}.User.id'],
  1674. '%2$s, %1$s'
  1675. );
  1676. $expected = [
  1677. '2, mariano.iglesias',
  1678. '14, phpnut',
  1679. '25, gwoo'
  1680. ];
  1681. $this->assertEquals($expected, $result);
  1682. }
  1683. /**
  1684. * testFormattingNullValues method
  1685. *
  1686. * @return void
  1687. */
  1688. public function testFormatNullValues()
  1689. {
  1690. $data = [
  1691. ['Person' => [
  1692. 'first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42'
  1693. ]],
  1694. ['Person' => [
  1695. 'first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null
  1696. ]],
  1697. ['Person' => [
  1698. 'first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null
  1699. ]]
  1700. ];
  1701. $result = Hash::format($data, ['{n}.Person.something'], '%s');
  1702. $expected = ['42', '', ''];
  1703. $this->assertEquals($expected, $result);
  1704. $result = Hash::format($data, ['{n}.Person.city', '{n}.Person.something'], '%s, %s');
  1705. $expected = ['Boston, 42', 'Boondock, ', 'Venice Beach, '];
  1706. $this->assertEquals($expected, $result);
  1707. }
  1708. /**
  1709. * Test map()
  1710. *
  1711. * @return void
  1712. */
  1713. public function testMap()
  1714. {
  1715. $data = static::articleData();
  1716. $result = Hash::map($data, '{n}.Article.id', [$this, 'mapCallback']);
  1717. $expected = [2, 4, 6, 8, 10];
  1718. $this->assertEquals($expected, $result);
  1719. }
  1720. /**
  1721. * testApply
  1722. *
  1723. * @return void
  1724. */
  1725. public function testApply()
  1726. {
  1727. $data = static::articleData();
  1728. $result = Hash::apply($data, '{n}.Article.id', 'array_sum');
  1729. $this->assertEquals(15, $result);
  1730. }
  1731. /**
  1732. * Test reduce()
  1733. *
  1734. * @return void
  1735. */
  1736. public function testReduce()
  1737. {
  1738. $data = static::articleData();
  1739. $result = Hash::reduce($data, '{n}.Article.id', [$this, 'reduceCallback']);
  1740. $this->assertEquals(15, $result);
  1741. }
  1742. /**
  1743. * testing method for map callbacks.
  1744. *
  1745. * @param mixed $value Value
  1746. * @return mixed
  1747. */
  1748. public function mapCallback($value)
  1749. {
  1750. return $value * 2;
  1751. }
  1752. /**
  1753. * testing method for reduce callbacks.
  1754. *
  1755. * @param mixed $one First param
  1756. * @param mixed $two Second param
  1757. * @return mixed
  1758. */
  1759. public function reduceCallback($one, $two)
  1760. {
  1761. return $one + $two;
  1762. }
  1763. /**
  1764. * test Hash nest with a normal model result set. For kicks rely on Hash nest detecting the key names
  1765. * automatically
  1766. *
  1767. * @return void
  1768. */
  1769. public function testNestModel()
  1770. {
  1771. $input = [
  1772. [
  1773. 'ModelName' => [
  1774. 'id' => 1,
  1775. 'parent_id' => null
  1776. ],
  1777. ],
  1778. [
  1779. 'ModelName' => [
  1780. 'id' => 2,
  1781. 'parent_id' => 1
  1782. ],
  1783. ],
  1784. [
  1785. 'ModelName' => [
  1786. 'id' => 3,
  1787. 'parent_id' => 1
  1788. ],
  1789. ],
  1790. [
  1791. 'ModelName' => [
  1792. 'id' => 4,
  1793. 'parent_id' => 1
  1794. ],
  1795. ],
  1796. [
  1797. 'ModelName' => [
  1798. 'id' => 5,
  1799. 'parent_id' => 1
  1800. ],
  1801. ],
  1802. [
  1803. 'ModelName' => [
  1804. 'id' => 6,
  1805. 'parent_id' => null
  1806. ],
  1807. ],
  1808. [
  1809. 'ModelName' => [
  1810. 'id' => 7,
  1811. 'parent_id' => 6
  1812. ],
  1813. ],
  1814. [
  1815. 'ModelName' => [
  1816. 'id' => 8,
  1817. 'parent_id' => 6
  1818. ],
  1819. ],
  1820. [
  1821. 'ModelName' => [
  1822. 'id' => 9,
  1823. 'parent_id' => 6
  1824. ],
  1825. ],
  1826. [
  1827. 'ModelName' => [
  1828. 'id' => 10,
  1829. 'parent_id' => 6
  1830. ]
  1831. ]
  1832. ];
  1833. $expected = [
  1834. [
  1835. 'ModelName' => [
  1836. 'id' => 1,
  1837. 'parent_id' => null
  1838. ],
  1839. 'children' => [
  1840. [
  1841. 'ModelName' => [
  1842. 'id' => 2,
  1843. 'parent_id' => 1
  1844. ],
  1845. 'children' => []
  1846. ],
  1847. [
  1848. 'ModelName' => [
  1849. 'id' => 3,
  1850. 'parent_id' => 1
  1851. ],
  1852. 'children' => []
  1853. ],
  1854. [
  1855. 'ModelName' => [
  1856. 'id' => 4,
  1857. 'parent_id' => 1
  1858. ],
  1859. 'children' => []
  1860. ],
  1861. [
  1862. 'ModelName' => [
  1863. 'id' => 5,
  1864. 'parent_id' => 1
  1865. ],
  1866. 'children' => []
  1867. ],
  1868. ]
  1869. ],
  1870. [
  1871. 'ModelName' => [
  1872. 'id' => 6,
  1873. 'parent_id' => null
  1874. ],
  1875. 'children' => [
  1876. [
  1877. 'ModelName' => [
  1878. 'id' => 7,
  1879. 'parent_id' => 6
  1880. ],
  1881. 'children' => []
  1882. ],
  1883. [
  1884. 'ModelName' => [
  1885. 'id' => 8,
  1886. 'parent_id' => 6
  1887. ],
  1888. 'children' => []
  1889. ],
  1890. [
  1891. 'ModelName' => [
  1892. 'id' => 9,
  1893. 'parent_id' => 6
  1894. ],
  1895. 'children' => []
  1896. ],
  1897. [
  1898. 'ModelName' => [
  1899. 'id' => 10,
  1900. 'parent_id' => 6
  1901. ],
  1902. 'children' => []
  1903. ]
  1904. ]
  1905. ]
  1906. ];
  1907. $result = Hash::nest($input);
  1908. $this->assertEquals($expected, $result);
  1909. }
  1910. /**
  1911. * test Hash nest with a normal model result set, and a nominated root id
  1912. *
  1913. * @return void
  1914. */
  1915. public function testNestModelExplicitRoot()
  1916. {
  1917. $input = [
  1918. [
  1919. 'ModelName' => [
  1920. 'id' => 1,
  1921. 'parent_id' => null
  1922. ],
  1923. ],
  1924. [
  1925. 'ModelName' => [
  1926. 'id' => 2,
  1927. 'parent_id' => 1
  1928. ],
  1929. ],
  1930. [
  1931. 'ModelName' => [
  1932. 'id' => 3,
  1933. 'parent_id' => 1
  1934. ],
  1935. ],
  1936. [
  1937. 'ModelName' => [
  1938. 'id' => 4,
  1939. 'parent_id' => 1
  1940. ],
  1941. ],
  1942. [
  1943. 'ModelName' => [
  1944. 'id' => 5,
  1945. 'parent_id' => 1
  1946. ],
  1947. ],
  1948. [
  1949. 'ModelName' => [
  1950. 'id' => 6,
  1951. 'parent_id' => null
  1952. ],
  1953. ],
  1954. [
  1955. 'ModelName' => [
  1956. 'id' => 7,
  1957. 'parent_id' => 6
  1958. ],
  1959. ],
  1960. [
  1961. 'ModelName' => [
  1962. 'id' => 8,
  1963. 'parent_id' => 6
  1964. ],
  1965. ],
  1966. [
  1967. 'ModelName' => [
  1968. 'id' => 9,
  1969. 'parent_id' => 6
  1970. ],
  1971. ],
  1972. [
  1973. 'ModelName' => [
  1974. 'id' => 10,
  1975. 'parent_id' => 6
  1976. ]
  1977. ]
  1978. ];
  1979. $expected = [
  1980. [
  1981. 'ModelName' => [
  1982. 'id' => 6,
  1983. 'parent_id' => null
  1984. ],
  1985. 'children' => [
  1986. [
  1987. 'ModelName' => [
  1988. 'id' => 7,
  1989. 'parent_id' => 6
  1990. ],
  1991. 'children' => []
  1992. ],
  1993. [
  1994. 'ModelName' => [
  1995. 'id' => 8,
  1996. 'parent_id' => 6
  1997. ],
  1998. 'children' => []
  1999. ],
  2000. [
  2001. 'ModelName' => [
  2002. 'id' => 9,
  2003. 'parent_id' => 6
  2004. ],
  2005. 'children' => []
  2006. ],
  2007. [
  2008. 'ModelName' => [
  2009. 'id' => 10,
  2010. 'parent_id' => 6
  2011. ],
  2012. 'children' => []
  2013. ]
  2014. ]
  2015. ]
  2016. ];
  2017. $result = Hash::nest($input, ['root' => 6]);
  2018. $this->assertEquals($expected, $result);
  2019. }
  2020. /**
  2021. * test Hash nest with a 1d array - this method should be able to handle any type of array input
  2022. *
  2023. * @return void
  2024. */
  2025. public function testNest1Dimensional()
  2026. {
  2027. $input = [
  2028. [
  2029. 'id' => 1,
  2030. 'parent_id' => null
  2031. ],
  2032. [
  2033. 'id' => 2,
  2034. 'parent_id' => 1
  2035. ],
  2036. [
  2037. 'id' => 3,
  2038. 'parent_id' => 1
  2039. ],
  2040. [
  2041. 'id' => 4,
  2042. 'parent_id' => 1
  2043. ],
  2044. [
  2045. 'id' => 5,
  2046. 'parent_id' => 1
  2047. ],
  2048. [
  2049. 'id' => 6,
  2050. 'parent_id' => null
  2051. ],
  2052. [
  2053. 'id' => 7,
  2054. 'parent_id' => 6
  2055. ],
  2056. [
  2057. 'id' => 8,
  2058. 'parent_id' => 6
  2059. ],
  2060. [
  2061. 'id' => 9,
  2062. 'parent_id' => 6
  2063. ],
  2064. [
  2065. 'id' => 10,
  2066. 'parent_id' => 6
  2067. ]
  2068. ];
  2069. $expected = [
  2070. [
  2071. 'id' => 1,
  2072. 'parent_id' => null,
  2073. 'children' => [
  2074. [
  2075. 'id' => 2,
  2076. 'parent_id' => 1,
  2077. 'children' => []
  2078. ],
  2079. [
  2080. 'id' => 3,
  2081. 'parent_id' => 1,
  2082. 'children' => []
  2083. ],
  2084. [
  2085. 'id' => 4,
  2086. 'parent_id' => 1,
  2087. 'children' => []
  2088. ],
  2089. [
  2090. 'id' => 5,
  2091. 'parent_id' => 1,
  2092. 'children' => []
  2093. ],
  2094. ]
  2095. ],
  2096. [
  2097. 'id' => 6,
  2098. 'parent_id' => null,
  2099. 'children' => [
  2100. [
  2101. 'id' => 7,
  2102. 'parent_id' => 6,
  2103. 'children' => []
  2104. ],
  2105. [
  2106. 'id' => 8,
  2107. 'parent_id' => 6,
  2108. 'children' => []
  2109. ],
  2110. [
  2111. 'id' => 9,
  2112. 'parent_id' => 6,
  2113. 'children' => []
  2114. ],
  2115. [
  2116. 'id' => 10,
  2117. 'parent_id' => 6,
  2118. 'children' => []
  2119. ]
  2120. ]
  2121. ]
  2122. ];
  2123. $result = Hash::nest($input, ['idPath' => '{n}.id', 'parentPath' => '{n}.parent_id']);
  2124. $this->assertEquals($expected, $result);
  2125. }
  2126. /**
  2127. * test Hash nest with no specified parent data.
  2128. *
  2129. * The result should be the same as the input.
  2130. * For an easier comparison, unset all the empty children arrays from the result
  2131. *
  2132. * @return void
  2133. */
  2134. public function testMissingParent()
  2135. {
  2136. $input = [
  2137. [
  2138. 'id' => 1,
  2139. ],
  2140. [
  2141. 'id' => 2,
  2142. ],
  2143. [
  2144. 'id' => 3,
  2145. ],
  2146. [
  2147. 'id' => 4,
  2148. ],
  2149. [
  2150. 'id' => 5,
  2151. ],
  2152. [
  2153. 'id' => 6,
  2154. ],
  2155. [
  2156. 'id' => 7,
  2157. ],
  2158. [
  2159. 'id' => 8,
  2160. ],
  2161. [
  2162. 'id' => 9,
  2163. ],
  2164. [
  2165. 'id' => 10,
  2166. ]
  2167. ];
  2168. $result = Hash::nest($input, ['idPath' => '{n}.id', 'parentPath' => '{n}.parent_id']);
  2169. foreach ($result as &$row) {
  2170. if (empty($row['children'])) {
  2171. unset($row['children']);
  2172. }
  2173. }
  2174. $this->assertEquals($input, $result);
  2175. }
  2176. /**
  2177. * Tests that nest() throws an InvalidArgumentException when providing an invalid input.
  2178. *
  2179. * @expectedException \InvalidArgumentException
  2180. * @return void
  2181. */
  2182. public function testNestInvalid()
  2183. {
  2184. $input = [
  2185. [
  2186. 'ParentCategory' => [
  2187. 'id' => '1',
  2188. 'name' => 'Lorem ipsum dolor sit amet',
  2189. 'parent_id' => '1'
  2190. ]
  2191. ]
  2192. ];
  2193. Hash::nest($input);
  2194. }
  2195. /**
  2196. * testMergeDiff method
  2197. *
  2198. * @return void
  2199. */
  2200. public function testMergeDiff()
  2201. {
  2202. $first = [
  2203. 'ModelOne' => [
  2204. 'id' => 1001,
  2205. 'field_one' => 'a1.m1.f1',
  2206. 'field_two' => 'a1.m1.f2'
  2207. ]
  2208. ];
  2209. $second = [
  2210. 'ModelTwo' => [
  2211. 'id' => 1002,
  2212. 'field_one' => 'a2.m2.f1',
  2213. 'field_two' => 'a2.m2.f2'
  2214. ]
  2215. ];
  2216. $result = Hash::mergeDiff($first, $second);
  2217. $this->assertEquals($result, $first + $second);
  2218. $result = Hash::mergeDiff($first, []);
  2219. $this->assertEquals($result, $first);
  2220. $result = Hash::mergeDiff([], $first);
  2221. $this->assertEquals($result, $first);
  2222. $third = [
  2223. 'ModelOne' => [
  2224. 'id' => 1003,
  2225. 'field_one' => 'a3.m1.f1',
  2226. 'field_two' => 'a3.m1.f2',
  2227. 'field_three' => 'a3.m1.f3'
  2228. ]
  2229. ];
  2230. $result = Hash::mergeDiff($first, $third);
  2231. $expected = [
  2232. 'ModelOne' => [
  2233. 'id' => 1001,
  2234. 'field_one' => 'a1.m1.f1',
  2235. 'field_two' => 'a1.m1.f2',
  2236. 'field_three' => 'a3.m1.f3'
  2237. ]
  2238. ];
  2239. $this->assertEquals($expected, $result);
  2240. $first = [
  2241. 0 => ['ModelOne' => ['id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2']],
  2242. 1 => ['ModelTwo' => ['id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2']]
  2243. ];
  2244. $second = [
  2245. 0 => ['ModelOne' => ['id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2']],
  2246. 1 => ['ModelTwo' => ['id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2']]
  2247. ];
  2248. $result = Hash::mergeDiff($first, $second);
  2249. $this->assertEquals($result, $first);
  2250. $third = [
  2251. 0 => [
  2252. 'ModelThree' => [
  2253. 'id' => 1003,
  2254. 'field_one' => 's3.0.m3.f1',
  2255. 'field_two' => 's3.0.m3.f2'
  2256. ]
  2257. ]
  2258. ];
  2259. $result = Hash::mergeDiff($first, $third);
  2260. $expected = [
  2261. 0 => [
  2262. 'ModelOne' => [
  2263. 'id' => 1001,
  2264. 'field_one' => 's1.0.m1.f1',
  2265. 'field_two' => 's1.0.m1.f2'
  2266. ],
  2267. 'ModelThree' => [
  2268. 'id' => 1003,
  2269. 'field_one' => 's3.0.m3.f1',
  2270. 'field_two' => 's3.0.m3.f2'
  2271. ]
  2272. ],
  2273. 1 => [
  2274. 'ModelTwo' => [
  2275. 'id' => 1002,
  2276. 'field_one' => 's1.1.m2.f2',
  2277. 'field_two' => 's1.1.m2.f2'
  2278. ]
  2279. ]
  2280. ];
  2281. $this->assertEquals($expected, $result);
  2282. $result = Hash::mergeDiff($first, []);
  2283. $this->assertEquals($result, $first);
  2284. $result = Hash::mergeDiff($first, $second);
  2285. $this->assertEquals($result, $first + $second);
  2286. }
  2287. /**
  2288. * Tests Hash::expand
  2289. *
  2290. * @return void
  2291. */
  2292. public function testExpand()
  2293. {
  2294. $data = ['My', 'Array', 'To', 'Flatten'];
  2295. $flat = Hash::flatten($data);
  2296. $result = Hash::expand($flat);
  2297. $this->assertEquals($data, $result);
  2298. $data = [
  2299. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2300. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2301. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2302. '1.Author.user' => 'larry', '1.Author.password' => null
  2303. ];
  2304. $result = Hash::expand($data);
  2305. $expected = [
  2306. [
  2307. 'Post' => ['id' => '1', 'author_id' => '1', 'title' => 'First Post'],
  2308. 'Author' => ['id' => '1', 'user' => 'nate', 'password' => 'foo'],
  2309. ],
  2310. [
  2311. 'Post' => ['id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'],
  2312. 'Author' => ['id' => '3', 'user' => 'larry', 'password' => null],
  2313. ]
  2314. ];
  2315. $this->assertEquals($expected, $result);
  2316. $data = [
  2317. '0/Post/id' => 1,
  2318. '0/Post/name' => 'test post'
  2319. ];
  2320. $result = Hash::expand($data, '/');
  2321. $expected = [
  2322. [
  2323. 'Post' => [
  2324. 'id' => 1,
  2325. 'name' => 'test post'
  2326. ]
  2327. ]
  2328. ];
  2329. $this->assertEquals($expected, $result);
  2330. $data = ['a.b.100.a' => null, 'a.b.200.a' => null];
  2331. $expected = [
  2332. 'a' => [
  2333. 'b' => [
  2334. 100 => ['a' => null],
  2335. 200 => ['a' => null]
  2336. ]
  2337. ]
  2338. ];
  2339. $result = Hash::expand($data);
  2340. $this->assertEquals($expected, $result);
  2341. }
  2342. /**
  2343. * Test that flattening a large complex set doesn't loop forever.
  2344. *
  2345. * @return void
  2346. */
  2347. public function testFlattenInfiniteLoop()
  2348. {
  2349. $data = [
  2350. 'Order.ASI' => '0',
  2351. 'Order.Accounting' => '0',
  2352. 'Order.Admin' => '0',
  2353. 'Order.Art' => '0',
  2354. 'Order.ArtChecker' => '0',
  2355. 'Order.Canned' => '0',
  2356. 'Order.Customer_Tags' => '',
  2357. 'Order.Embroidery' => '0',
  2358. 'Order.Item.0.Product.style_number' => 'a11222',
  2359. 'Order.Item.0.Product.slug' => 'a11222',
  2360. 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
  2361. 'Order.Item.0.Product.Color.slug' => 'kelly_green',
  2362. 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
  2363. 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
  2364. 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
  2365. 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
  2366. 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
  2367. 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
  2368. 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
  2369. 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
  2370. 'Order.Item.0.Product.brand' => 'OUTER BANKS',
  2371. 'Order.Item.0.Product.style' => 'T-shirt',
  2372. 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
  2373. 'Order.Item.0.Product.sizes.0.Size.qty' => '',
  2374. 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
  2375. 'Order.Item.0.Product.sizes.0.Size.id' => '38',
  2376. 'Order.Item.0.Product.sizes.1.Size.qty' => '',
  2377. 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
  2378. 'Order.Item.0.Product.sizes.1.Size.id' => '39',
  2379. 'Order.Item.0.Product.sizes.2.Size.qty' => '78',
  2380. 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
  2381. 'Order.Item.0.Product.sizes.2.Size.id' => '40',
  2382. 'Order.Item.0.Product.sizes.3.Size.qty' => '',
  2383. 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
  2384. 'Order.Item.0.Product.sizes.3.Size.id' => '41',
  2385. 'Order.Item.0.Product.sizes.4.Size.qty' => '',
  2386. 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
  2387. 'Order.Item.0.Product.sizes.4.Size.id' => '42',
  2388. 'Order.Item.0.Art.imprint_locations.0.id' => 2,
  2389. 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
  2390. 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
  2391. 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
  2392. 'Order.Item.0.Art.imprint_locations.0.art' => '',
  2393. 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
  2394. 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
  2395. 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
  2396. 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
  2397. 'Order.Item.0.Art.imprint_locations.0.height' => 4,
  2398. 'Order.Item.0.Art.imprint_locations.0.width' => 5,
  2399. 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
  2400. 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
  2401. 'Order.Item.0.Art.imprint_locations.1.id' => 4,
  2402. 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
  2403. 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
  2404. 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
  2405. 'Order.Item.0.Art.imprint_locations.1.art' => '',
  2406. 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
  2407. 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
  2408. 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
  2409. 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
  2410. 'Order.Item.0.Art.imprint_locations.2.id' => 26,
  2411. 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
  2412. 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
  2413. 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
  2414. 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
  2415. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
  2416. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
  2417. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
  2418. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
  2419. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
  2420. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
  2421. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
  2422. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
  2423. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
  2424. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
  2425. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
  2426. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
  2427. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
  2428. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
  2429. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
  2430. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
  2431. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
  2432. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
  2433. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
  2434. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
  2435. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
  2436. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
  2437. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
  2438. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
  2439. 'Order.Job.0._id' => 'job-1',
  2440. 'Order.Job.0.type' => 'screenprinting',
  2441. 'Order.Job.0.postPress' => 'job-2',
  2442. 'Order.Job.1._id' => 'job-2',
  2443. 'Order.Job.1.type' => 'embroidery',
  2444. 'Order.Postpress' => '0',
  2445. 'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
  2446. 'Order.PriceAdjustment.0.adjustment' => '-20',
  2447. 'Order.PriceAdjustment.0.adjustment_type' => 'percent',
  2448. 'Order.PriceAdjustment.0.type' => 'grand_total',
  2449. 'Order.PriceAdjustment.1.adjustment' => '20',
  2450. 'Order.PriceAdjustment.1.adjustment_type' => 'flat',
  2451. 'Order.PriceAdjustment.1.min-items' => '10',
  2452. 'Order.PriceAdjustment.1.type' => 'min-items',
  2453. 'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
  2454. 'Order.Purchasing' => '0',
  2455. 'Order.QualityControl' => '0',
  2456. 'Order.Receiving' => '0',
  2457. 'Order.ScreenPrinting' => '0',
  2458. 'Order.Stage.art_approval' => 0,
  2459. 'Order.Stage.draft' => 1,
  2460. 'Order.Stage.quote' => 1,
  2461. 'Order.Stage.order' => 1,
  2462. 'Order.StoreLiason' => '0',
  2463. 'Order.Tag_UI_Email' => '',
  2464. 'Order.Tags' => '',
  2465. 'Order._id' => 'test-2',
  2466. 'Order.add_print_location' => '',
  2467. 'Order.created' => '2011-Dec-29 05:40:18',
  2468. 'Order.force_admin' => '0',
  2469. 'Order.modified' => '2012-Jul-25 01:24:49',
  2470. 'Order.name' => 'towering power',
  2471. 'Order.order_id' => '135961',
  2472. 'Order.slug' => 'test-2',
  2473. 'Order.title' => 'test job 2',
  2474. 'Order.type' => 'ttt'
  2475. ];
  2476. $expanded = Hash::expand($data);
  2477. $flattened = Hash::flatten($expanded);
  2478. $this->assertEquals($data, $flattened);
  2479. }
  2480. }