HashTest.php 107 KB

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