HashTest.php 105 KB

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