HashTest.php 84 KB

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