HashTest.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. * testMergeDiff method
  2040. *
  2041. * @return void
  2042. */
  2043. public function testMergeDiff() {
  2044. $first = array(
  2045. 'ModelOne' => array(
  2046. 'id' => 1001,
  2047. 'field_one' => 'a1.m1.f1',
  2048. 'field_two' => 'a1.m1.f2'
  2049. )
  2050. );
  2051. $second = array(
  2052. 'ModelTwo' => array(
  2053. 'id' => 1002,
  2054. 'field_one' => 'a2.m2.f1',
  2055. 'field_two' => 'a2.m2.f2'
  2056. )
  2057. );
  2058. $result = Hash::mergeDiff($first, $second);
  2059. $this->assertEquals($result, $first + $second);
  2060. $result = Hash::mergeDiff($first, array());
  2061. $this->assertEquals($result, $first);
  2062. $result = Hash::mergeDiff(array(), $first);
  2063. $this->assertEquals($result, $first);
  2064. $third = array(
  2065. 'ModelOne' => array(
  2066. 'id' => 1003,
  2067. 'field_one' => 'a3.m1.f1',
  2068. 'field_two' => 'a3.m1.f2',
  2069. 'field_three' => 'a3.m1.f3'
  2070. )
  2071. );
  2072. $result = Hash::mergeDiff($first, $third);
  2073. $expected = array(
  2074. 'ModelOne' => array(
  2075. 'id' => 1001,
  2076. 'field_one' => 'a1.m1.f1',
  2077. 'field_two' => 'a1.m1.f2',
  2078. 'field_three' => 'a3.m1.f3'
  2079. )
  2080. );
  2081. $this->assertEquals($expected, $result);
  2082. $first = array(
  2083. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2084. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'))
  2085. );
  2086. $second = array(
  2087. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2088. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'))
  2089. );
  2090. $result = Hash::mergeDiff($first, $second);
  2091. $this->assertEquals($result, $first);
  2092. $third = array(
  2093. 0 => array(
  2094. 'ModelThree' => array(
  2095. 'id' => 1003,
  2096. 'field_one' => 's3.0.m3.f1',
  2097. 'field_two' => 's3.0.m3.f2'
  2098. )
  2099. )
  2100. );
  2101. $result = Hash::mergeDiff($first, $third);
  2102. $expected = array(
  2103. 0 => array(
  2104. 'ModelOne' => array(
  2105. 'id' => 1001,
  2106. 'field_one' => 's1.0.m1.f1',
  2107. 'field_two' => 's1.0.m1.f2'
  2108. ),
  2109. 'ModelThree' => array(
  2110. 'id' => 1003,
  2111. 'field_one' => 's3.0.m3.f1',
  2112. 'field_two' => 's3.0.m3.f2'
  2113. )
  2114. ),
  2115. 1 => array(
  2116. 'ModelTwo' => array(
  2117. 'id' => 1002,
  2118. 'field_one' => 's1.1.m2.f2',
  2119. 'field_two' => 's1.1.m2.f2'
  2120. )
  2121. )
  2122. );
  2123. $this->assertEquals($expected, $result);
  2124. $result = Hash::mergeDiff($first, array());
  2125. $this->assertEquals($result, $first);
  2126. $result = Hash::mergeDiff($first, $second);
  2127. $this->assertEquals($result, $first + $second);
  2128. }
  2129. /**
  2130. * Tests Hash::expand
  2131. *
  2132. * @return void
  2133. */
  2134. public function testExpand() {
  2135. $data = array('My', 'Array', 'To', 'Flatten');
  2136. $flat = Hash::flatten($data);
  2137. $result = Hash::expand($flat);
  2138. $this->assertEquals($data, $result);
  2139. $data = array(
  2140. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2141. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2142. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2143. '1.Author.user' => 'larry', '1.Author.password' => null
  2144. );
  2145. $result = Hash::expand($data);
  2146. $expected = array(
  2147. array(
  2148. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2149. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2150. ),
  2151. array(
  2152. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2153. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2154. )
  2155. );
  2156. $this->assertEquals($expected, $result);
  2157. $data = array(
  2158. '0/Post/id' => 1,
  2159. '0/Post/name' => 'test post'
  2160. );
  2161. $result = Hash::expand($data, '/');
  2162. $expected = array(
  2163. array(
  2164. 'Post' => array(
  2165. 'id' => 1,
  2166. 'name' => 'test post'
  2167. )
  2168. )
  2169. );
  2170. $this->assertEquals($expected, $result);
  2171. $data = array('a.b.100.a' => null, 'a.b.200.a' => null);
  2172. $expected = array(
  2173. 'a' => array(
  2174. 'b' => array(
  2175. 100 => array('a' => null),
  2176. 200 => array('a' => null)
  2177. )
  2178. )
  2179. );
  2180. $result = Hash::expand($data);
  2181. $this->assertEquals($expected, $result);
  2182. }
  2183. /**
  2184. * Test that flattening a large complex set doesn't loop forever.
  2185. *
  2186. * @return void
  2187. */
  2188. public function testFlattenInfiniteLoop() {
  2189. $data = array(
  2190. 'Order.ASI' => '0',
  2191. 'Order.Accounting' => '0',
  2192. 'Order.Admin' => '0',
  2193. 'Order.Art' => '0',
  2194. 'Order.ArtChecker' => '0',
  2195. 'Order.Canned' => '0',
  2196. 'Order.Customer_Tags' => '',
  2197. 'Order.Embroidery' => '0',
  2198. 'Order.Item.0.Product.style_number' => 'a11222',
  2199. 'Order.Item.0.Product.slug' => 'a11222',
  2200. 'Order.Item.0.Product._id' => '4ff8b8d3d7bbe8ad30000000',
  2201. 'Order.Item.0.Product.Color.slug' => 'kelly_green',
  2202. 'Order.Item.0.Product.ColorSizes.0.Color.color' => 'Sport Grey',
  2203. 'Order.Item.0.Product.ColorSizes.0.Color.slug' => 'sport_grey',
  2204. 'Order.Item.0.Product.ColorSizes.1.Color.color' => 'Kelly Green',
  2205. 'Order.Item.0.Product.ColorSizes.1.Color.slug' => 'kelly_green',
  2206. 'Order.Item.0.Product.ColorSizes.2.Color.color' => 'Orange',
  2207. 'Order.Item.0.Product.ColorSizes.2.Color.slug' => 'orange',
  2208. 'Order.Item.0.Product.ColorSizes.3.Color.color' => 'Yellow Haze',
  2209. 'Order.Item.0.Product.ColorSizes.3.Color.slug' => 'yellow_haze',
  2210. 'Order.Item.0.Product.brand' => 'OUTER BANKS',
  2211. 'Order.Item.0.Product.style' => 'T-shirt',
  2212. 'Order.Item.0.Product.description' => 'uhiuhuih oin ooi ioo ioio',
  2213. 'Order.Item.0.Product.sizes.0.Size.qty' => '',
  2214. 'Order.Item.0.Product.sizes.0.Size.size' => '0-3mo',
  2215. 'Order.Item.0.Product.sizes.0.Size.id' => '38',
  2216. 'Order.Item.0.Product.sizes.1.Size.qty' => '',
  2217. 'Order.Item.0.Product.sizes.1.Size.size' => '3-6mo',
  2218. 'Order.Item.0.Product.sizes.1.Size.id' => '39',
  2219. 'Order.Item.0.Product.sizes.2.Size.qty' => '78',
  2220. 'Order.Item.0.Product.sizes.2.Size.size' => '6-9mo',
  2221. 'Order.Item.0.Product.sizes.2.Size.id' => '40',
  2222. 'Order.Item.0.Product.sizes.3.Size.qty' => '',
  2223. 'Order.Item.0.Product.sizes.3.Size.size' => '6-12mo',
  2224. 'Order.Item.0.Product.sizes.3.Size.id' => '41',
  2225. 'Order.Item.0.Product.sizes.4.Size.qty' => '',
  2226. 'Order.Item.0.Product.sizes.4.Size.size' => '12-18mo',
  2227. 'Order.Item.0.Product.sizes.4.Size.id' => '42',
  2228. 'Order.Item.0.Art.imprint_locations.0.id' => 2,
  2229. 'Order.Item.0.Art.imprint_locations.0.name' => 'Left Chest',
  2230. 'Order.Item.0.Art.imprint_locations.0.imprint_type.id' => 7,
  2231. 'Order.Item.0.Art.imprint_locations.0.imprint_type.type' => 'Embroidery',
  2232. 'Order.Item.0.Art.imprint_locations.0.art' => '',
  2233. 'Order.Item.0.Art.imprint_locations.0.num_colors' => 3,
  2234. 'Order.Item.0.Art.imprint_locations.0.description' => 'Wooo! This is Embroidery!!',
  2235. 'Order.Item.0.Art.imprint_locations.0.lines.0' => 'Platen',
  2236. 'Order.Item.0.Art.imprint_locations.0.lines.1' => 'Logo',
  2237. 'Order.Item.0.Art.imprint_locations.0.height' => 4,
  2238. 'Order.Item.0.Art.imprint_locations.0.width' => 5,
  2239. 'Order.Item.0.Art.imprint_locations.0.stitch_density' => 'Light',
  2240. 'Order.Item.0.Art.imprint_locations.0.metallic_thread' => true,
  2241. 'Order.Item.0.Art.imprint_locations.1.id' => 4,
  2242. 'Order.Item.0.Art.imprint_locations.1.name' => 'Full Back',
  2243. 'Order.Item.0.Art.imprint_locations.1.imprint_type.id' => 6,
  2244. 'Order.Item.0.Art.imprint_locations.1.imprint_type.type' => 'Screenprinting',
  2245. 'Order.Item.0.Art.imprint_locations.1.art' => '',
  2246. 'Order.Item.0.Art.imprint_locations.1.num_colors' => 3,
  2247. 'Order.Item.0.Art.imprint_locations.1.description' => 'Wooo! This is Screenprinting!!',
  2248. 'Order.Item.0.Art.imprint_locations.1.lines.0' => 'Platen',
  2249. 'Order.Item.0.Art.imprint_locations.1.lines.1' => 'Logo',
  2250. 'Order.Item.0.Art.imprint_locations.2.id' => 26,
  2251. 'Order.Item.0.Art.imprint_locations.2.name' => 'HS - JSY Name Below',
  2252. 'Order.Item.0.Art.imprint_locations.2.imprint_type.id' => 9,
  2253. 'Order.Item.0.Art.imprint_locations.2.imprint_type.type' => 'Names',
  2254. 'Order.Item.0.Art.imprint_locations.2.description' => 'Wooo! This is Names!!',
  2255. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.active' => 1,
  2256. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.name' => 'Benjamin Talavera',
  2257. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.color' => 'Red',
  2258. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.height' => '3',
  2259. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.layout' => 'Arched',
  2260. 'Order.Item.0.Art.imprint_locations.2.sizes.S.0.style' => 'Classic',
  2261. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.active' => 0,
  2262. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.name' => 'Rishi Narayan',
  2263. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.color' => 'Cardinal',
  2264. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.height' => '4',
  2265. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.layout' => 'Straight',
  2266. 'Order.Item.0.Art.imprint_locations.2.sizes.S.1.style' => 'Team US',
  2267. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.active' => 1,
  2268. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.name' => 'Brandon Plasters',
  2269. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.color' => 'Red',
  2270. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.height' => '3',
  2271. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.layout' => 'Arched',
  2272. 'Order.Item.0.Art.imprint_locations.2.sizes.M.0.style' => 'Classic',
  2273. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.active' => 0,
  2274. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.name' => 'Andrew Reed',
  2275. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.color' => 'Cardinal',
  2276. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.height' => '4',
  2277. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.layout' => 'Straight',
  2278. 'Order.Item.0.Art.imprint_locations.2.sizes.M.1.style' => 'Team US',
  2279. 'Order.Job.0._id' => 'job-1',
  2280. 'Order.Job.0.type' => 'screenprinting',
  2281. 'Order.Job.0.postPress' => 'job-2',
  2282. 'Order.Job.1._id' => 'job-2',
  2283. 'Order.Job.1.type' => 'embroidery',
  2284. 'Order.Postpress' => '0',
  2285. 'Order.PriceAdjustment.0._id' => 'price-adjustment-1',
  2286. 'Order.PriceAdjustment.0.adjustment' => '-20',
  2287. 'Order.PriceAdjustment.0.adjustment_type' => 'percent',
  2288. 'Order.PriceAdjustment.0.type' => 'grand_total',
  2289. 'Order.PriceAdjustment.1.adjustment' => '20',
  2290. 'Order.PriceAdjustment.1.adjustment_type' => 'flat',
  2291. 'Order.PriceAdjustment.1.min-items' => '10',
  2292. 'Order.PriceAdjustment.1.type' => 'min-items',
  2293. 'Order.PriceAdjustment.1._id' => 'another-test-adjustment',
  2294. 'Order.Purchasing' => '0',
  2295. 'Order.QualityControl' => '0',
  2296. 'Order.Receiving' => '0',
  2297. 'Order.ScreenPrinting' => '0',
  2298. 'Order.Stage.art_approval' => 0,
  2299. 'Order.Stage.draft' => 1,
  2300. 'Order.Stage.quote' => 1,
  2301. 'Order.Stage.order' => 1,
  2302. 'Order.StoreLiason' => '0',
  2303. 'Order.Tag_UI_Email' => '',
  2304. 'Order.Tags' => '',
  2305. 'Order._id' => 'test-2',
  2306. 'Order.add_print_location' => '',
  2307. 'Order.created' => '2011-Dec-29 05:40:18',
  2308. 'Order.force_admin' => '0',
  2309. 'Order.modified' => '2012-Jul-25 01:24:49',
  2310. 'Order.name' => 'towering power',
  2311. 'Order.order_id' => '135961',
  2312. 'Order.slug' => 'test-2',
  2313. 'Order.title' => 'test job 2',
  2314. 'Order.type' => 'ttt'
  2315. );
  2316. $expanded = Hash::expand($data);
  2317. $flattened = Hash::flatten($expanded);
  2318. $this->assertEquals($data, $flattened);
  2319. }
  2320. }