HashTest.php 81 KB

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