HashTest.php 83 KB

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