HashTest.php 105 KB

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