HashTest.php 101 KB

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