HashTest.php 98 KB

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