HashTest.php 95 KB

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