HashTest.php 83 KB

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