HashTest.php 98 KB

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