HashTest.php 65 KB

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