HashTest.php 98 KB

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