HashTest.php 94 KB

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