HashTest.php 66 KB

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