HashTest.php 96 KB

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