HashTest.php 96 KB

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