HashTest.php 82 KB

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