HashTest.php 98 KB

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