HashTest.php 97 KB

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