HashTest.php 60 KB

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