HashTest.php 103 KB

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