HashTest.php 98 KB

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