SetTest.php 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564
  1. <?php
  2. /**
  3. * SetTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Set', 'Utility');
  20. App::uses('Model', 'Model');
  21. /**
  22. * SetTest class
  23. *
  24. * @package Cake.Test.Case.Utility
  25. */
  26. class SetTest extends CakeTestCase {
  27. /**
  28. * testNumericKeyExtraction method
  29. *
  30. * @return void
  31. */
  32. public function testNumericKeyExtraction() {
  33. $data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
  34. $this->assertEquals(array(1, 'whatever'), Set::extract($data, '{n}'));
  35. $this->assertEquals(array('plugin' => null, 'controller' => '', 'action' => ''), Set::diff($data, Set::extract($data, '{n}')));
  36. }
  37. /**
  38. * testEnum method
  39. *
  40. * @return void
  41. */
  42. public function testEnum() {
  43. $result = Set::enum(1, 'one, two');
  44. $this->assertEquals('two', $result);
  45. $result = Set::enum(2, 'one, two');
  46. $this->assertNull($result);
  47. $set = array('one', 'two');
  48. $result = Set::enum(0, $set);
  49. $this->assertEquals('one', $result);
  50. $result = Set::enum(1, $set);
  51. $this->assertEquals('two', $result);
  52. $result = Set::enum(1, array('one', 'two'));
  53. $this->assertEquals('two', $result);
  54. $result = Set::enum(2, array('one', 'two'));
  55. $this->assertNull($result);
  56. $result = Set::enum('first', array('first' => 'one', 'second' => 'two'));
  57. $this->assertEquals('one', $result);
  58. $result = Set::enum('third', array('first' => 'one', 'second' => 'two'));
  59. $this->assertNull($result);
  60. $result = Set::enum('no', array('no' => 0, 'yes' => 1));
  61. $this->assertEquals(0, $result);
  62. $result = Set::enum('not sure', array('no' => 0, 'yes' => 1));
  63. $this->assertNull($result);
  64. $result = Set::enum(0);
  65. $this->assertEquals('no', $result);
  66. $result = Set::enum(1);
  67. $this->assertEquals('yes', $result);
  68. $result = Set::enum(2);
  69. $this->assertNull($result);
  70. }
  71. /**
  72. * testFilter method
  73. *
  74. * @see Hash test cases, as Set::filter() is just a proxy.
  75. * @return void
  76. */
  77. public function testFilter() {
  78. $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  79. $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
  80. $this->assertSame($expected, $result);
  81. }
  82. /**
  83. * testNumericArrayCheck method
  84. *
  85. * @see Hash test cases, as Set::numeric() is just a proxy.
  86. * @return void
  87. */
  88. public function testNumericArrayCheck() {
  89. $data = array('one');
  90. $this->assertTrue(Set::numeric(array_keys($data)));
  91. }
  92. /**
  93. * testKeyCheck method
  94. *
  95. * @return void
  96. */
  97. public function testKeyCheck() {
  98. $data = array('Multi' => array('dimensonal' => array('array')));
  99. $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
  100. $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
  101. $data = array(
  102. array(
  103. 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  104. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  105. 'Comment' => array(
  106. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  107. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  108. ),
  109. 'Tag' => array(
  110. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  111. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  112. )
  113. ),
  114. array(
  115. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  116. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  117. 'Comment' => array(),
  118. 'Tag' => array()
  119. )
  120. );
  121. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  122. $this->assertTrue(Set::check($data, '0.Comment.0.id'));
  123. $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
  124. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  125. $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
  126. }
  127. /**
  128. * testMerge method
  129. *
  130. * @return void
  131. */
  132. public function testMerge() {
  133. $r = Set::merge(array('foo'));
  134. $this->assertEquals(array('foo'), $r);
  135. $r = Set::merge('foo');
  136. $this->assertEquals(array('foo'), $r);
  137. $r = Set::merge('foo', 'bar');
  138. $this->assertEquals(array('foo', 'bar'), $r);
  139. $r = Set::merge(array('foo'), array(), array('bar'));
  140. $this->assertEquals(array('foo', 'bar'), $r);
  141. $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');
  142. $this->assertEquals(array('foo', 'user' => 'bob', 'no-bar', 'bar'), $r);
  143. $a = array('foo', 'foo2');
  144. $b = array('bar', 'bar2');
  145. $this->assertEquals(array('foo', 'foo2', 'bar', 'bar2'), Set::merge($a, $b));
  146. $a = array('foo' => 'bar', 'bar' => 'foo');
  147. $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
  148. $this->assertEquals(array('foo' => 'no-bar', 'bar' => 'no-foo'), Set::merge($a, $b));
  149. $a = array('users' => array('bob', 'jim'));
  150. $b = array('users' => array('lisa', 'tina'));
  151. $this->assertEquals(array('users' => array('bob', 'jim', 'lisa', 'tina')), Set::merge($a, $b));
  152. $a = array('users' => array('jim', 'bob'));
  153. $b = array('users' => 'none');
  154. $this->assertEquals(array('users' => 'none'), Set::merge($a, $b));
  155. $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
  156. $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
  157. $this->assertEquals(array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream'), Set::merge($a, $b));
  158. $c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate');
  159. $expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate');
  160. $this->assertEquals($expected, Set::merge($a, $b, $c));
  161. $this->assertEquals(Set::merge($a, $b, array(), $c), $expected);
  162. $r = Set::merge($a, $b, $c);
  163. $this->assertEquals($expected, $r);
  164. $a = array('Tree', 'CounterCache',
  165. 'Upload' => array('folder' => 'products',
  166. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));
  167. $b = array('Cacheable' => array('enabled' => false),
  168. 'Limit',
  169. 'Bindable',
  170. 'Validator',
  171. 'Transactional');
  172. $expected = array('Tree', 'CounterCache',
  173. 'Upload' => array('folder' => 'products',
  174. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
  175. 'Cacheable' => array('enabled' => false),
  176. 'Limit',
  177. 'Bindable',
  178. 'Validator',
  179. 'Transactional');
  180. $this->assertEquals($expected, Set::merge($a, $b));
  181. $expected = array('Tree' => null, 'CounterCache' => null,
  182. 'Upload' => array('folder' => 'products',
  183. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
  184. 'Cacheable' => array('enabled' => false),
  185. 'Limit' => null,
  186. 'Bindable' => null,
  187. 'Validator' => null,
  188. 'Transactional' => null);
  189. $this->assertEquals($expected, Set::normalize(Set::merge($a, $b)));
  190. }
  191. /**
  192. * testSort method
  193. *
  194. * @return void
  195. */
  196. public function testSort() {
  197. $result = Set::sort(array(), '{n}.name', 'asc');
  198. $this->assertEquals(array(), $result);
  199. $a = array(
  200. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  201. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  202. );
  203. $b = array(
  204. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  205. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  206. );
  207. $a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
  208. $this->assertEquals($a, $b);
  209. $b = array(
  210. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  211. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  212. );
  213. $a = array(
  214. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  215. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  216. );
  217. $a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
  218. $this->assertEquals($a, $b);
  219. $a = array(
  220. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  221. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  222. 2 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob')))
  223. );
  224. $b = array(
  225. 0 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),
  226. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  227. 2 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  228. );
  229. $a = Set::sort($a, '{n}.Person.name', 'asc');
  230. $this->assertEquals($a, $b);
  231. $a = array(
  232. array(7,6,4),
  233. array(3,4,5),
  234. array(3,2,1),
  235. );
  236. $b = array(
  237. array(3,2,1),
  238. array(3,4,5),
  239. array(7,6,4),
  240. );
  241. $a = Set::sort($a, '{n}.{n}', 'asc');
  242. $this->assertEquals($a, $b);
  243. $a = array(
  244. array(7,6,4),
  245. array(3,4,5),
  246. array(3,2,array(1,1,1)),
  247. );
  248. $b = array(
  249. array(3,2,array(1,1,1)),
  250. array(3,4,5),
  251. array(7,6,4),
  252. );
  253. $a = Set::sort($a, '{n}', 'asc');
  254. $this->assertEquals($a, $b);
  255. $a = array(
  256. 0 => array('Person' => array('name' => 'Jeff')),
  257. 1 => array('Shirt' => array('color' => 'black'))
  258. );
  259. $b = array(
  260. 0 => array('Shirt' => array('color' => 'black')),
  261. 1 => array('Person' => array('name' => 'Jeff')),
  262. );
  263. $a = Set::sort($a, '{n}.Person.name', 'ASC');
  264. $this->assertEquals($a, $b);
  265. $names = array(
  266. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  267. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  268. array('employees' => array(array('name' => array()))),
  269. array('employees' => array(array('name' => array())))
  270. );
  271. $result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
  272. $expected = array(
  273. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  274. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  275. array('employees' => array(array('name' => array()))),
  276. array('employees' => array(array('name' => array())))
  277. );
  278. $this->assertEquals($expected, $result);
  279. $menus = array(
  280. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  281. 'comments' => array('title' => 'Comments', 'weight' => 2),
  282. 'users' => array('title' => 'Users', 'weight' => 1),
  283. );
  284. $expected = array(
  285. 'users' => array('title' => 'Users', 'weight' => 1),
  286. 'comments' => array('title' => 'Comments', 'weight' => 2),
  287. 'blogs' => array('title' => 'Blogs', 'weight' => 3),
  288. );
  289. $result = Set::sort($menus, '{[a-z]+}.weight', 'ASC');
  290. $this->assertEquals($expected, $result);
  291. }
  292. /**
  293. * test sorting with string keys.
  294. *
  295. * @return void
  296. */
  297. public function testSortString() {
  298. $toSort = array(
  299. 'four' => array('number' => 4, 'some' => 'foursome'),
  300. 'six' => array('number' => 6, 'some' => 'sixsome'),
  301. 'five' => array('number' => 5, 'some' => 'fivesome'),
  302. 'two' => array('number' => 2, 'some' => 'twosome'),
  303. 'three' => array('number' => 3, 'some' => 'threesome')
  304. );
  305. $sorted = Set::sort($toSort, '{s}.number', 'asc');
  306. $expected = array(
  307. 'two' => array('number' => 2, 'some' => 'twosome'),
  308. 'three' => array('number' => 3, 'some' => 'threesome'),
  309. 'four' => array('number' => 4, 'some' => 'foursome'),
  310. 'five' => array('number' => 5, 'some' => 'fivesome'),
  311. 'six' => array('number' => 6, 'some' => 'sixsome')
  312. );
  313. $this->assertEquals($expected, $sorted);
  314. }
  315. /**
  316. * test sorting with out of order keys.
  317. *
  318. * @return void
  319. */
  320. public function testSortWithOutOfOrderKeys() {
  321. $data = array(
  322. 9 => array('class' => 510, 'test2' => 2),
  323. 1 => array('class' => 500, 'test2' => 1),
  324. 2 => array('class' => 600, 'test2' => 2),
  325. 5 => array('class' => 625, 'test2' => 4),
  326. 0 => array('class' => 605, 'test2' => 3),
  327. );
  328. $expected = array(
  329. array('class' => 500, 'test2' => 1),
  330. array('class' => 510, 'test2' => 2),
  331. array('class' => 600, 'test2' => 2),
  332. array('class' => 605, 'test2' => 3),
  333. array('class' => 625, 'test2' => 4),
  334. );
  335. $result = Set::sort($data, '{n}.class', 'asc');
  336. $this->assertEquals($expected, $result);
  337. $result = Set::sort($data, '{n}.test2', 'asc');
  338. $this->assertEquals($expected, $result);
  339. }
  340. /**
  341. * testExtract method
  342. *
  343. * @return void
  344. */
  345. public function testExtract() {
  346. $a = array(
  347. array(
  348. 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  349. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  350. 'Comment' => array(
  351. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  352. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  353. ),
  354. 'Tag' => array(
  355. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  356. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  357. ),
  358. 'Deep' => array(
  359. 'Nesting' => array(
  360. 'test' => array(
  361. 1 => 'foo',
  362. 2 => array(
  363. 'and' => array('more' => 'stuff')
  364. )
  365. )
  366. )
  367. )
  368. ),
  369. array(
  370. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  371. 'User' => array('id' => '2', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  372. 'Comment' => array(),
  373. 'Tag' => array()
  374. ),
  375. array(
  376. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  377. 'User' => array('id' => '3', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  378. 'Comment' => array(),
  379. 'Tag' => array()
  380. ),
  381. array(
  382. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  383. 'User' => array('id' => '4', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  384. 'Comment' => array(),
  385. 'Tag' => array()
  386. ),
  387. array(
  388. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  389. 'User' => array('id' => '5', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  390. 'Comment' => array(),
  391. 'Tag' => array()
  392. )
  393. );
  394. $b = array('Deep' => $a[0]['Deep']);
  395. $c = array(
  396. array('a' => array('I' => array('a' => 1))),
  397. array(
  398. 'a' => array(
  399. 2
  400. )
  401. ),
  402. array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))),
  403. );
  404. $expected = array(array('a' => $c[2]['a']));
  405. $r = Set::extract('/a/II[a=3]/..', $c);
  406. $this->assertEquals($expected, $r);
  407. $expected = array(1, 2, 3, 4, 5);
  408. $this->assertEquals($expected, Set::extract('/User/id', $a));
  409. $expected = array(1, 2, 3, 4, 5);
  410. $this->assertEquals($expected, Set::extract('/User/id', $a));
  411. $expected = array(
  412. array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
  413. );
  414. $r = Set::extract('/User/id', $a, array('flatten' => false));
  415. $this->assertEquals($expected, $r);
  416. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  417. $this->assertEquals($expected, Set::extract('/Deep/Nesting/test', $a));
  418. $this->assertEquals($expected, Set::extract('/Deep/Nesting/test', $b));
  419. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  420. $r = Set::extract('/Deep/Nesting/test/1/..', $a);
  421. $this->assertEquals($expected, $r);
  422. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  423. $r = Set::extract('/Deep/Nesting/test/2/and/../..', $a);
  424. $this->assertEquals($expected, $r);
  425. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  426. $r = Set::extract('/Deep/Nesting/test/2/../../../Nesting/test/2/..', $a);
  427. $this->assertEquals($expected, $r);
  428. $expected = array(2);
  429. $r = Set::extract('/User[2]/id', $a);
  430. $this->assertEquals($expected, $r);
  431. $expected = array(4, 5);
  432. $r = Set::extract('/User[id>3]/id', $a);
  433. $this->assertEquals($expected, $r);
  434. $expected = array(2, 3);
  435. $r = Set::extract('/User[id>1][id<=3]/id', $a);
  436. $this->assertEquals($expected, $r);
  437. $expected = array(array('I'), array('II'));
  438. $r = Set::extract('/a/@*', $c);
  439. $this->assertEquals($expected, $r);
  440. $single = array(
  441. 'User' => array(
  442. 'id' => 4,
  443. 'name' => 'Neo',
  444. )
  445. );
  446. $tricky = array(
  447. 0 => array(
  448. 'User' => array(
  449. 'id' => 1,
  450. 'name' => 'John',
  451. )
  452. ),
  453. 1 => array(
  454. 'User' => array(
  455. 'id' => 2,
  456. 'name' => 'Bob',
  457. )
  458. ),
  459. 2 => array(
  460. 'User' => array(
  461. 'id' => 3,
  462. 'name' => 'Tony',
  463. )
  464. ),
  465. 'User' => array(
  466. 'id' => 4,
  467. 'name' => 'Neo',
  468. )
  469. );
  470. $expected = array(1, 2, 3, 4);
  471. $r = Set::extract('/User/id', $tricky);
  472. $this->assertEquals($expected, $r);
  473. $expected = array(4);
  474. $r = Set::extract('/User/id', $single);
  475. $this->assertEquals($expected, $r);
  476. $expected = array(1, 3);
  477. $r = Set::extract('/User[name=/n/]/id', $tricky);
  478. $this->assertEquals($expected, $r);
  479. $expected = array(4);
  480. $r = Set::extract('/User[name=/N/]/id', $tricky);
  481. $this->assertEquals($expected, $r);
  482. $expected = array(1, 3, 4);
  483. $r = Set::extract('/User[name=/N/i]/id', $tricky);
  484. $this->assertEquals($expected, $r);
  485. $expected = array(array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name'));
  486. $r = Set::extract('/User/@*', $tricky);
  487. $this->assertEquals($expected, $r);
  488. $common = array(
  489. array(
  490. 'Article' => array(
  491. 'id' => 1,
  492. 'name' => 'Article 1',
  493. ),
  494. 'Comment' => array(
  495. array(
  496. 'id' => 1,
  497. 'user_id' => 5,
  498. 'article_id' => 1,
  499. 'text' => 'Comment 1',
  500. ),
  501. array(
  502. 'id' => 2,
  503. 'user_id' => 23,
  504. 'article_id' => 1,
  505. 'text' => 'Comment 2',
  506. ),
  507. array(
  508. 'id' => 3,
  509. 'user_id' => 17,
  510. 'article_id' => 1,
  511. 'text' => 'Comment 3',
  512. ),
  513. ),
  514. ),
  515. array(
  516. 'Article' => array(
  517. 'id' => 2,
  518. 'name' => 'Article 2',
  519. ),
  520. 'Comment' => array(
  521. array(
  522. 'id' => 4,
  523. 'user_id' => 2,
  524. 'article_id' => 2,
  525. 'text' => 'Comment 4',
  526. 'addition' => '',
  527. ),
  528. array(
  529. 'id' => 5,
  530. 'user_id' => 23,
  531. 'article_id' => 2,
  532. 'text' => 'Comment 5',
  533. 'addition' => 'foo',
  534. ),
  535. ),
  536. ),
  537. array(
  538. 'Article' => array(
  539. 'id' => 3,
  540. 'name' => 'Article 3',
  541. ),
  542. 'Comment' => array(),
  543. )
  544. );
  545. $r = Set::extract('/Comment/id', $common);
  546. $expected = array(1, 2, 3, 4, 5);
  547. $this->assertEquals($expected, $r);
  548. $expected = array(1, 2, 4, 5);
  549. $r = Set::extract('/Comment[id!=3]/id', $common);
  550. $this->assertEquals($expected, $r);
  551. $r = Set::extract('/', $common);
  552. $this->assertEquals($r, $common);
  553. $expected = array(1, 2, 4, 5);
  554. $r = Set::extract($common, '/Comment[id!=3]/id');
  555. $this->assertEquals($expected, $r);
  556. $expected = array($common[0]['Comment'][2]);
  557. $r = Set::extract($common, '/Comment/2');
  558. $this->assertEquals($expected, $r);
  559. $expected = array($common[0]['Comment'][0]);
  560. $r = Set::extract($common, '/Comment[1]/.[id=1]');
  561. $this->assertEquals($expected, $r);
  562. $expected = array($common[1]['Comment'][1]);
  563. $r = Set::extract($common, '/1/Comment/.[2]');
  564. $this->assertEquals($expected, $r);
  565. $expected = array();
  566. $r = Set::extract('/User/id', array());
  567. $this->assertEquals($expected, $r);
  568. $expected = array(5);
  569. $r = Set::extract('/Comment/id[:last]', $common);
  570. $this->assertEquals($expected, $r);
  571. $expected = array(1);
  572. $r = Set::extract('/Comment/id[:first]', $common);
  573. $this->assertEquals($expected, $r);
  574. $expected = array(3);
  575. $r = Set::extract('/Article[:last]/id', $common);
  576. $this->assertEquals($expected, $r);
  577. $expected = array(array('Comment' => $common[1]['Comment'][0]));
  578. $r = Set::extract('/Comment[addition=]', $common);
  579. $this->assertEquals($expected, $r);
  580. $habtm = array(
  581. array(
  582. 'Post' => array(
  583. 'id' => 1,
  584. 'title' => 'great post',
  585. ),
  586. 'Comment' => array(
  587. array(
  588. 'id' => 1,
  589. 'text' => 'foo',
  590. 'User' => array(
  591. 'id' => 1,
  592. 'name' => 'bob'
  593. ),
  594. ),
  595. array(
  596. 'id' => 2,
  597. 'text' => 'bar',
  598. 'User' => array(
  599. 'id' => 2,
  600. 'name' => 'tod'
  601. ),
  602. ),
  603. ),
  604. ),
  605. array(
  606. 'Post' => array(
  607. 'id' => 2,
  608. 'title' => 'fun post',
  609. ),
  610. 'Comment' => array(
  611. array(
  612. 'id' => 3,
  613. 'text' => '123',
  614. 'User' => array(
  615. 'id' => 3,
  616. 'name' => 'dan'
  617. ),
  618. ),
  619. array(
  620. 'id' => 4,
  621. 'text' => '987',
  622. 'User' => array(
  623. 'id' => 4,
  624. 'name' => 'jim'
  625. ),
  626. ),
  627. ),
  628. ),
  629. );
  630. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  631. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  632. $this->assertEquals('dan', $r[1]['Comment']['User']['name']);
  633. $this->assertEquals(2, count($r));
  634. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  635. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  636. $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
  637. $this->assertEquals(2, count($r));
  638. $tree = array(
  639. array(
  640. 'Category' => array('name' => 'Category 1'),
  641. 'children' => array(array('Category' => array('name' => 'Category 1.1')))
  642. ),
  643. array(
  644. 'Category' => array('name' => 'Category 2'),
  645. 'children' => array(
  646. array('Category' => array('name' => 'Category 2.1')),
  647. array('Category' => array('name' => 'Category 2.2'))
  648. )
  649. ),
  650. array(
  651. 'Category' => array('name' => 'Category 3'),
  652. 'children' => array(array('Category' => array('name' => 'Category 3.1')))
  653. )
  654. );
  655. $expected = array(array('Category' => $tree[1]['Category']));
  656. $r = Set::extract('/Category[name=Category 2]', $tree);
  657. $this->assertEquals($expected, $r);
  658. $expected = array(
  659. array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children'])
  660. );
  661. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  662. $this->assertEquals($expected, $r);
  663. $expected = array(
  664. array('children' => $tree[1]['children'][0]),
  665. array('children' => $tree[1]['children'][1])
  666. );
  667. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  668. $this->assertEquals($expected, $r);
  669. $habtm = array(
  670. array(
  671. 'Post' => array(
  672. 'id' => 1,
  673. 'title' => 'great post',
  674. ),
  675. 'Comment' => array(
  676. array(
  677. 'id' => 1,
  678. 'text' => 'foo',
  679. 'User' => array(
  680. 'id' => 1,
  681. 'name' => 'bob'
  682. ),
  683. ),
  684. array(
  685. 'id' => 2,
  686. 'text' => 'bar',
  687. 'User' => array(
  688. 'id' => 2,
  689. 'name' => 'tod'
  690. ),
  691. ),
  692. ),
  693. ),
  694. array(
  695. 'Post' => array(
  696. 'id' => 2,
  697. 'title' => 'fun post',
  698. ),
  699. 'Comment' => array(
  700. array(
  701. 'id' => 3,
  702. 'text' => '123',
  703. 'User' => array(
  704. 'id' => 3,
  705. 'name' => 'dan'
  706. ),
  707. ),
  708. array(
  709. 'id' => 4,
  710. 'text' => '987',
  711. 'User' => array(
  712. 'id' => 4,
  713. 'name' => 'jim'
  714. ),
  715. ),
  716. ),
  717. ),
  718. );
  719. $r = Set::extract('/Comment/User[name=/\w+/]/..', $habtm);
  720. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  721. $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
  722. $this->assertEquals('dan', $r[2]['Comment']['User']['name']);
  723. $this->assertEquals('dan', $r[3]['Comment']['User']['name']);
  724. $this->assertEquals(4, count($r));
  725. $r = Set::extract('/Comment/User[name=/[a-z]+/]/..', $habtm);
  726. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  727. $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
  728. $this->assertEquals('dan', $r[2]['Comment']['User']['name']);
  729. $this->assertEquals('dan', $r[3]['Comment']['User']['name']);
  730. $this->assertEquals(4, count($r));
  731. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  732. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  733. $this->assertEquals('dan', $r[1]['Comment']['User']['name']);
  734. $this->assertEquals(2, count($r));
  735. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  736. $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
  737. $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
  738. $this->assertEquals(2, count($r));
  739. $mixedKeys = array(
  740. 'User' => array(
  741. 0 => array(
  742. 'id' => 4,
  743. 'name' => 'Neo'
  744. ),
  745. 1 => array(
  746. 'id' => 5,
  747. 'name' => 'Morpheus'
  748. ),
  749. 'stringKey' => array()
  750. )
  751. );
  752. $expected = array('Neo', 'Morpheus');
  753. $r = Set::extract('/User/name', $mixedKeys);
  754. $this->assertEquals($expected, $r);
  755. $f = array(
  756. array(
  757. 'file' => array(
  758. 'name' => 'zipfile.zip',
  759. 'type' => 'application/zip',
  760. 'tmp_name' => '/tmp/php178.tmp',
  761. 'error' => 0,
  762. 'size' => '564647'
  763. )
  764. ),
  765. array(
  766. 'file' => array(
  767. 'name' => 'zipfile2.zip',
  768. 'type' => 'application/x-zip-compressed',
  769. 'tmp_name' => '/tmp/php179.tmp',
  770. 'error' => 0,
  771. 'size' => '354784'
  772. )
  773. ),
  774. array(
  775. 'file' => array(
  776. 'name' => 'picture.jpg',
  777. 'type' => 'image/jpeg',
  778. 'tmp_name' => '/tmp/php180.tmp',
  779. 'error' => 0,
  780. 'size' => '21324'
  781. )
  782. )
  783. );
  784. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x-zip-compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  785. $r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
  786. $this->assertEquals($expected, $r);
  787. $expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
  788. $r = Set::extract('/file/.[type=application/zip]', $f);
  789. $this->assertEquals($expected, $r);
  790. $f = array(
  791. array(
  792. 'file' => array(
  793. 'name' => 'zipfile.zip',
  794. 'type' => 'application/zip',
  795. 'tmp_name' => '/tmp/php178.tmp',
  796. 'error' => 0,
  797. 'size' => '564647'
  798. )
  799. ),
  800. array(
  801. 'file' => array(
  802. 'name' => 'zipfile2.zip',
  803. 'type' => 'application/x zip compressed',
  804. 'tmp_name' => '/tmp/php179.tmp',
  805. 'error' => 0,
  806. 'size' => '354784'
  807. )
  808. ),
  809. array(
  810. 'file' => array(
  811. 'name' => 'picture.jpg',
  812. 'type' => 'image/jpeg',
  813. 'tmp_name' => '/tmp/php180.tmp',
  814. 'error' => 0,
  815. 'size' => '21324'
  816. )
  817. )
  818. );
  819. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  820. $r = Set::extract('/file/.[type=application/x zip compressed]', $f);
  821. $this->assertEquals($expected, $r);
  822. $expected = array(
  823. array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'),
  824. array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784')
  825. );
  826. $r = Set::extract('/file/.[tmp_name=/tmp\/php17/]', $f);
  827. $this->assertEquals($expected, $r);
  828. $hasMany = array(
  829. 'Node' => array(
  830. 'id' => 1,
  831. 'name' => 'First',
  832. 'state' => 50
  833. ),
  834. 'ParentNode' => array(
  835. 0 => array(
  836. 'id' => 2,
  837. 'name' => 'Second',
  838. 'state' => 60,
  839. )
  840. )
  841. );
  842. $result = Set::extract('/ParentNode/name', $hasMany);
  843. $expected = array('Second');
  844. $this->assertEquals($expected, $result);
  845. $data = array(
  846. array(
  847. 'Category' => array(
  848. 'id' => 1,
  849. 'name' => 'First'
  850. ),
  851. 0 => array(
  852. 'value' => 50
  853. )
  854. ),
  855. array(
  856. 'Category' => array(
  857. 'id' => 2,
  858. 'name' => 'Second'
  859. ),
  860. 0 => array(
  861. 'value' => 60
  862. )
  863. )
  864. );
  865. $expected = array(
  866. array(
  867. 'Category' => array(
  868. 'id' => 1,
  869. 'name' => 'First'
  870. ),
  871. 0 => array(
  872. 'value' => 50
  873. )
  874. )
  875. );
  876. $result = Set::extract('/Category[id=1]/..', $data);
  877. $this->assertEquals($expected, $result);
  878. $data = array(
  879. array(
  880. 'ChildNode' => array('id' => 1),
  881. array('name' => 'Item 1')
  882. ),
  883. array(
  884. 'ChildNode' => array('id' => 2),
  885. array('name' => 'Item 2')
  886. ),
  887. );
  888. $expected = array(
  889. 'Item 1',
  890. 'Item 2'
  891. );
  892. $result = Set::extract('/0/name', $data);
  893. $this->assertEquals($expected, $result);
  894. $data = array(
  895. array('A1', 'B1'),
  896. array('A2', 'B2')
  897. );
  898. $expected = array('A1', 'A2');
  899. $result = Set::extract('/0', $data);
  900. $this->assertEquals($expected, $result);
  901. }
  902. /**
  903. * test parent selectors with extract
  904. *
  905. * @return void
  906. */
  907. public function testExtractParentSelector() {
  908. $tree = array(
  909. array(
  910. 'Category' => array(
  911. 'name' => 'Category 1'
  912. ),
  913. 'children' => array(
  914. array(
  915. 'Category' => array(
  916. 'name' => 'Category 1.1'
  917. )
  918. )
  919. )
  920. ),
  921. array(
  922. 'Category' => array(
  923. 'name' => 'Category 2'
  924. ),
  925. 'children' => array(
  926. array(
  927. 'Category' => array(
  928. 'name' => 'Category 2.1'
  929. )
  930. ),
  931. array(
  932. 'Category' => array(
  933. 'name' => 'Category 2.2'
  934. )
  935. ),
  936. )
  937. ),
  938. array(
  939. 'Category' => array(
  940. 'name' => 'Category 3'
  941. ),
  942. 'children' => array(
  943. array(
  944. 'Category' => array(
  945. 'name' => 'Category 3.1'
  946. )
  947. )
  948. )
  949. )
  950. );
  951. $expected = array(array('Category' => $tree[1]['Category']));
  952. $r = Set::extract('/Category[name=Category 2]', $tree);
  953. $this->assertEquals($expected, $r);
  954. $expected = array(array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children']));
  955. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  956. $this->assertEquals($expected, $r);
  957. $expected = array(array('children' => $tree[1]['children'][0]), array('children' => $tree[1]['children'][1]));
  958. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  959. $this->assertEquals($expected, $r);
  960. $single = array(
  961. array(
  962. 'CallType' => array(
  963. 'name' => 'Internal Voice'
  964. ),
  965. 'x' => array(
  966. 'hour' => 7
  967. )
  968. )
  969. );
  970. $expected = array(7);
  971. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
  972. $this->assertEquals($expected, $r);
  973. $multiple = array(
  974. array(
  975. 'CallType' => array(
  976. 'name' => 'Internal Voice'
  977. ),
  978. 'x' => array(
  979. 'hour' => 7
  980. )
  981. ),
  982. array(
  983. 'CallType' => array(
  984. 'name' => 'Internal Voice'
  985. ),
  986. 'x' => array(
  987. 'hour' => 2
  988. )
  989. ),
  990. array(
  991. 'CallType' => array(
  992. 'name' => 'Internal Voice'
  993. ),
  994. 'x' => array(
  995. 'hour' => 1
  996. )
  997. )
  998. );
  999. $expected = array(7,2,1);
  1000. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
  1001. $this->assertEquals($expected, $r);
  1002. $a = array(
  1003. 'Model' => array(
  1004. '0' => array(
  1005. 'id' => 18,
  1006. 'SubModelsModel' => array(
  1007. 'id' => 1,
  1008. 'submodel_id' => 66,
  1009. 'model_id' => 18,
  1010. 'type' => 1
  1011. ),
  1012. ),
  1013. '1' => array(
  1014. 'id' => 0,
  1015. 'SubModelsModel' => array(
  1016. 'id' => 2,
  1017. 'submodel_id' => 66,
  1018. 'model_id' => 0,
  1019. 'type' => 1
  1020. ),
  1021. ),
  1022. '2' => array(
  1023. 'id' => 17,
  1024. 'SubModelsModel' => array(
  1025. 'id' => 3,
  1026. 'submodel_id' => 66,
  1027. 'model_id' => 17,
  1028. 'type' => 2
  1029. ),
  1030. ),
  1031. '3' => array(
  1032. 'id' => 0,
  1033. 'SubModelsModel' => array(
  1034. 'id' => 4,
  1035. 'submodel_id' => 66,
  1036. 'model_id' => 0,
  1037. 'type' => 2
  1038. )
  1039. )
  1040. )
  1041. );
  1042. $expected = array(
  1043. array(
  1044. 'Model' => array(
  1045. 'id' => 17,
  1046. 'SubModelsModel' => array(
  1047. 'id' => 3,
  1048. 'submodel_id' => 66,
  1049. 'model_id' => 17,
  1050. 'type' => 2
  1051. ),
  1052. )
  1053. ),
  1054. array(
  1055. 'Model' => array(
  1056. 'id' => 0,
  1057. 'SubModelsModel' => array(
  1058. 'id' => 4,
  1059. 'submodel_id' => 66,
  1060. 'model_id' => 0,
  1061. 'type' => 2
  1062. )
  1063. )
  1064. )
  1065. );
  1066. $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
  1067. $this->assertEquals($expected, $r);
  1068. }
  1069. /**
  1070. * test that extract() still works when arrays don't contain a 0 index.
  1071. *
  1072. * @return void
  1073. */
  1074. public function testExtractWithNonZeroArrays() {
  1075. $nonZero = array(
  1076. 1 => array(
  1077. 'User' => array(
  1078. 'id' => 1,
  1079. 'name' => 'John',
  1080. )
  1081. ),
  1082. 2 => array(
  1083. 'User' => array(
  1084. 'id' => 2,
  1085. 'name' => 'Bob',
  1086. )
  1087. ),
  1088. 3 => array(
  1089. 'User' => array(
  1090. 'id' => 3,
  1091. 'name' => 'Tony',
  1092. )
  1093. )
  1094. );
  1095. $expected = array(1, 2, 3);
  1096. $r = Set::extract('/User/id', $nonZero);
  1097. $this->assertEquals($expected, $r);
  1098. $expected = array(
  1099. array('User' => array('id' => 1, 'name' => 'John')),
  1100. array('User' => array('id' => 2, 'name' => 'Bob')),
  1101. array('User' => array('id' => 3, 'name' => 'Tony')),
  1102. );
  1103. $result = Set::extract('/User', $nonZero);
  1104. $this->assertEquals($expected, $result);
  1105. $nonSequential = array(
  1106. 'User' => array(
  1107. 0 => array('id' => 1),
  1108. 2 => array('id' => 2),
  1109. 6 => array('id' => 3),
  1110. 9 => array('id' => 4),
  1111. 3 => array('id' => 5),
  1112. ),
  1113. );
  1114. $nonZero = array(
  1115. 'User' => array(
  1116. 2 => array('id' => 1),
  1117. 4 => array('id' => 2),
  1118. 6 => array('id' => 3),
  1119. 9 => array('id' => 4),
  1120. 3 => array('id' => 5),
  1121. ),
  1122. );
  1123. $expected = array(1, 2, 3, 4, 5);
  1124. $this->assertEquals($expected, Set::extract('/User/id', $nonSequential));
  1125. $result = Set::extract('/User/id', $nonZero);
  1126. $this->assertEquals($expected, $result, 'Failed non zero array key extract');
  1127. $expected = array(1, 2, 3, 4, 5);
  1128. $this->assertEquals($expected, Set::extract('/User/id', $nonSequential));
  1129. $result = Set::extract('/User/id', $nonZero);
  1130. $this->assertEquals($expected, $result, 'Failed non zero array key extract');
  1131. $startingAtOne = array(
  1132. 'Article' => array(
  1133. 1 => array(
  1134. 'id' => 1,
  1135. 'approved' => 1,
  1136. ),
  1137. )
  1138. );
  1139. $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1)));
  1140. $result = Set::extract('/Article[approved=1]', $startingAtOne);
  1141. $this->assertEquals($expected, $result);
  1142. $items = array(
  1143. 240 => array(
  1144. 'A' => array(
  1145. 'field1' => 'a240',
  1146. 'field2' => 'a240',
  1147. ),
  1148. 'B' => array(
  1149. 'field1' => 'b240',
  1150. 'field2' => 'b240'
  1151. ),
  1152. )
  1153. );
  1154. $expected = array(
  1155. 0 => 'b240'
  1156. );
  1157. $result = Set::extract('/B/field1', $items);
  1158. $this->assertSame($expected, $result);
  1159. $this->assertSame($result, Set::extract('{n}.B.field1', $items));
  1160. }
  1161. /**
  1162. * testExtractWithArrays method
  1163. *
  1164. * @return void
  1165. */
  1166. public function testExtractWithArrays() {
  1167. $data = array(
  1168. 'Level1' => array(
  1169. 'Level2' => array('test1', 'test2'),
  1170. 'Level2bis' => array('test3', 'test4')
  1171. )
  1172. );
  1173. $this->assertEquals(array(array('Level2' => array('test1', 'test2'))), Set::extract('/Level1/Level2', $data));
  1174. $this->assertEquals(array(array('Level2bis' => array('test3', 'test4'))), Set::extract('/Level1/Level2bis', $data));
  1175. }
  1176. /**
  1177. * test extract() with elements that have non-array children.
  1178. *
  1179. * @return void
  1180. */
  1181. public function testExtractWithNonArrayElements() {
  1182. $data = array(
  1183. 'node' => array(
  1184. array('foo'),
  1185. 'bar'
  1186. )
  1187. );
  1188. $result = Set::extract('/node', $data);
  1189. $expected = array(
  1190. array('node' => array('foo')),
  1191. 'bar'
  1192. );
  1193. $this->assertEquals($expected, $result);
  1194. $data = array(
  1195. 'node' => array(
  1196. 'foo' => array('bar'),
  1197. 'bar' => array('foo')
  1198. )
  1199. );
  1200. $result = Set::extract('/node', $data);
  1201. $expected = array(
  1202. array('foo' => array('bar')),
  1203. array('bar' => array('foo')),
  1204. );
  1205. $this->assertEquals($expected, $result);
  1206. $data = array(
  1207. 'node' => array(
  1208. 'foo' => array(
  1209. 'bar'
  1210. ),
  1211. 'bar' => 'foo'
  1212. )
  1213. );
  1214. $result = Set::extract('/node', $data);
  1215. $expected = array(
  1216. array('foo' => array('bar')),
  1217. 'foo'
  1218. );
  1219. $this->assertEquals($expected, $result);
  1220. }
  1221. /**
  1222. * Test that extract() + matching can hit null things.
  1223. */
  1224. public function testExtractMatchesNull() {
  1225. $data = array(
  1226. 'Country' => array(
  1227. array('name' => 'Canada'),
  1228. array('name' => 'Australia'),
  1229. array('name' => null),
  1230. )
  1231. );
  1232. $result = Set::extract('/Country[name=/Canada|^$/]', $data);
  1233. $expected = array(
  1234. array(
  1235. 'Country' => array(
  1236. 'name' => 'Canada',
  1237. ),
  1238. ),
  1239. array(
  1240. 'Country' => array(
  1241. 'name' => null,
  1242. ),
  1243. ),
  1244. );
  1245. $this->assertEquals($expected, $result);
  1246. }
  1247. /**
  1248. * testMatches method
  1249. *
  1250. * @return void
  1251. */
  1252. public function testMatches() {
  1253. $a = array(
  1254. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1255. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1256. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1257. );
  1258. $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
  1259. $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
  1260. $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
  1261. $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
  1262. $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
  1263. $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
  1264. $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
  1265. $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
  1266. $this->assertTrue(Set::matches(array('3'), null, 3));
  1267. $this->assertTrue(Set::matches(array('5'), null, 5));
  1268. $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
  1269. $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
  1270. $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
  1271. $this->assertTrue(Set::matches('/Article[id=2]', $a));
  1272. $this->assertFalse(Set::matches('/Article[id=4]', $a));
  1273. $this->assertTrue(Set::matches(array(), $a));
  1274. $r = array(
  1275. 'Attachment' => array(
  1276. 'keep' => array()
  1277. ),
  1278. 'Comment' => array(
  1279. 'keep' => array(
  1280. 'Attachment' => array(
  1281. 'fields' => array(
  1282. 0 => 'attachment',
  1283. ),
  1284. ),
  1285. )
  1286. ),
  1287. 'User' => array(
  1288. 'keep' => array()
  1289. ),
  1290. 'Article' => array(
  1291. 'keep' => array(
  1292. 'Comment' => array(
  1293. 'fields' => array(
  1294. 0 => 'comment',
  1295. 1 => 'published',
  1296. ),
  1297. ),
  1298. 'User' => array(
  1299. 'fields' => array(
  1300. 0 => 'user',
  1301. ),
  1302. ),
  1303. )
  1304. )
  1305. );
  1306. $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
  1307. $this->assertEquals(array('comment', 'published'), Set::extract('/Article/keep/Comment/fields', $r));
  1308. $this->assertEquals(array('user'), Set::extract('/Article/keep/User/fields', $r));
  1309. }
  1310. /**
  1311. * testSetExtractReturnsEmptyArray method
  1312. *
  1313. * @return void
  1314. */
  1315. public function testSetExtractReturnsEmptyArray() {
  1316. $this->assertEquals(Set::extract(array(), '/Post/id'), array());
  1317. $this->assertEquals(Set::extract('/Post/id', array()), array());
  1318. $this->assertEquals(Set::extract('/Post/id', array(
  1319. array('Post' => array('name' => 'bob')),
  1320. array('Post' => array('name' => 'jim'))
  1321. )), array());
  1322. $this->assertEquals(Set::extract(array(), 'Message.flash'), null);
  1323. }
  1324. /**
  1325. * testClassicExtract method
  1326. *
  1327. * @return void
  1328. */
  1329. public function testClassicExtract() {
  1330. $a = array(
  1331. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1332. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1333. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1334. );
  1335. $result = Set::extract($a, '{n}.Article.id');
  1336. $expected = array(1, 2, 3);
  1337. $this->assertEquals($expected, $result);
  1338. $result = Set::extract($a, '{n}.Article.title');
  1339. $expected = array('Article 1', 'Article 2', 'Article 3');
  1340. $this->assertEquals($expected, $result);
  1341. $result = Set::extract($a, '1.Article.title');
  1342. $expected = 'Article 2';
  1343. $this->assertEquals($expected, $result);
  1344. $result = Set::extract($a, '3.Article.title');
  1345. $expected = null;
  1346. $this->assertEquals($expected, $result);
  1347. $a = array(
  1348. array(
  1349. 'Article' => array('id' => 1, 'title' => 'Article 1',
  1350. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1351. ),
  1352. array(
  1353. 'Article' => array('id' => 2, 'title' => 'Article 2',
  1354. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1355. ),
  1356. array(
  1357. 'Article' => array('id' => 3, 'title' => 'Article 3',
  1358. 'User' => array('id' => 2, 'username' => 'phpnut'))
  1359. )
  1360. );
  1361. $result = Set::extract($a, '{n}.Article.User.username');
  1362. $expected = array('mariano.iglesias', 'mariano.iglesias', 'phpnut');
  1363. $this->assertEquals($expected, $result);
  1364. $a = array(
  1365. array(
  1366. 'Article' => array(
  1367. 'id' => 1, 'title' => 'Article 1',
  1368. 'Comment' => array(
  1369. array('id' => 10, 'title' => 'Comment 10'),
  1370. array('id' => 11, 'title' => 'Comment 11'),
  1371. array('id' => 12, 'title' => 'Comment 12')
  1372. )
  1373. )
  1374. ),
  1375. array(
  1376. 'Article' => array(
  1377. 'id' => 2, 'title' => 'Article 2',
  1378. 'Comment' => array(
  1379. array('id' => 13, 'title' => 'Comment 13'),
  1380. array('id' => 14, 'title' => 'Comment 14')
  1381. )
  1382. )
  1383. ),
  1384. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1385. );
  1386. $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
  1387. $expected = array(array(10, 11, 12), array(13, 14), null);
  1388. $this->assertEquals($expected, $result);
  1389. $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
  1390. $expected = array(
  1391. array('Comment 10', 'Comment 11', 'Comment 12'),
  1392. array('Comment 13', 'Comment 14'),
  1393. null
  1394. );
  1395. $this->assertEquals($expected, $result);
  1396. $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
  1397. $result = Set::extract($a, '{n}.1day');
  1398. $expected = array('20 sales', '2 sales');
  1399. $this->assertEquals($expected, $result);
  1400. $a = array(
  1401. 'pages' => array('name' => 'page'),
  1402. 'fruites' => array('name' => 'fruit'),
  1403. 0 => array('name' => 'zero')
  1404. );
  1405. $result = Set::extract($a, '{s}.name');
  1406. $expected = array('page','fruit');
  1407. $this->assertEquals($expected, $result);
  1408. $a = array(
  1409. 0 => array('pages' => array('name' => 'page')),
  1410. 1 => array('fruites' => array('name' => 'fruit')),
  1411. 'test' => array(array('name' => 'jippi')),
  1412. 'dot.test' => array(array('name' => 'jippi'))
  1413. );
  1414. $result = Set::extract($a, '{n}.{s}.name');
  1415. $expected = array(0 => array('page'), 1 => array('fruit'));
  1416. $this->assertEquals($expected, $result);
  1417. $result = Set::extract($a, '{s}.{n}.name');
  1418. $expected = array(array('jippi'), array('jippi'));
  1419. $this->assertEquals($expected, $result);
  1420. $result = Set::extract($a, '{\w+}.{\w+}.name');
  1421. $expected = array(
  1422. array('pages' => 'page'),
  1423. array('fruites' => 'fruit'),
  1424. 'test' => array('jippi'),
  1425. 'dot.test' => array('jippi')
  1426. );
  1427. $this->assertEquals($expected, $result);
  1428. $result = Set::extract($a, '{\d+}.{\w+}.name');
  1429. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1430. $this->assertEquals($expected, $result);
  1431. $result = Set::extract($a, '{n}.{\w+}.name');
  1432. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1433. $this->assertEquals($expected, $result);
  1434. $result = Set::extract($a, '{s}.{\d+}.name');
  1435. $expected = array(array('jippi'), array('jippi'));
  1436. $this->assertEquals($expected, $result);
  1437. $result = Set::extract($a, '{s}');
  1438. $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
  1439. $this->assertEquals($expected, $result);
  1440. $result = Set::extract($a, '{[a-z]}');
  1441. $expected = array(
  1442. 'test' => array(array('name' => 'jippi')),
  1443. 'dot.test' => array(array('name' => 'jippi'))
  1444. );
  1445. $this->assertEquals($expected, $result);
  1446. $result = Set::extract($a, '{dot\.test}.{n}');
  1447. $expected = array('dot.test' => array(array('name' => 'jippi')));
  1448. $this->assertEquals($expected, $result);
  1449. $a = new stdClass();
  1450. $a->articles = array(
  1451. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1452. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1453. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1454. );
  1455. $result = Set::extract($a, 'articles.{n}.Article.id');
  1456. $expected = array(1, 2, 3);
  1457. $this->assertEquals($expected, $result);
  1458. $result = Set::extract($a, 'articles.{n}.Article.title');
  1459. $expected = array('Article 1', 'Article 2', 'Article 3');
  1460. $this->assertEquals($expected, $result);
  1461. $a = new ArrayObject();
  1462. $a['articles'] = array(
  1463. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1464. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1465. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1466. );
  1467. $result = Set::extract($a, 'articles.{n}.Article.id');
  1468. $expected = array(1, 2, 3);
  1469. $this->assertEquals($expected, $result);
  1470. $result = Set::extract($a, 'articles.{n}.Article.title');
  1471. $expected = array('Article 1', 'Article 2', 'Article 3');
  1472. $this->assertEquals($expected, $result);
  1473. $result = Set::extract($a, 'articles.0.Article.title');
  1474. $expected = 'Article 1';
  1475. $this->assertEquals($expected, $result);
  1476. }
  1477. /**
  1478. * test classicExtract with keys that exceed 32bit max int.
  1479. *
  1480. * @return void
  1481. */
  1482. public function testClassicExtractMaxInt() {
  1483. $data = array(
  1484. 'Data' => array(
  1485. '13376924712' => 'abc'
  1486. )
  1487. );
  1488. $this->assertEquals('abc', Set::classicExtract($data, 'Data.13376924712'));
  1489. }
  1490. /**
  1491. * testInsert method
  1492. *
  1493. * @see Hash tests, as Set::insert() is just a proxy.
  1494. * @return void
  1495. */
  1496. public function testInsert() {
  1497. $a = array(
  1498. 'pages' => array('name' => 'page')
  1499. );
  1500. $result = Set::insert($a, 'files', array('name' => 'files'));
  1501. $expected = array(
  1502. 'pages' => array('name' => 'page'),
  1503. 'files' => array('name' => 'files')
  1504. );
  1505. $this->assertEquals($expected, $result);
  1506. }
  1507. /**
  1508. * testRemove method
  1509. *
  1510. * @return void
  1511. */
  1512. public function testRemove() {
  1513. $a = array(
  1514. 'pages' => array('name' => 'page'),
  1515. 'files' => array('name' => 'files')
  1516. );
  1517. $result = Set::remove($a, 'files');
  1518. $expected = array(
  1519. 'pages' => array('name' => 'page')
  1520. );
  1521. $this->assertEquals($expected, $result);
  1522. }
  1523. /**
  1524. * testCheck method
  1525. *
  1526. * @return void
  1527. */
  1528. public function testCheck() {
  1529. $set = array(
  1530. 'My Index 1' => array('First' => 'The first item')
  1531. );
  1532. $this->assertTrue(Set::check($set, 'My Index 1.First'));
  1533. $this->assertTrue(Set::check($set, 'My Index 1'));
  1534. $this->assertEquals(Set::check($set, array()), $set);
  1535. $set = array(
  1536. 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
  1537. );
  1538. $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
  1539. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
  1540. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1541. $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1542. }
  1543. /**
  1544. * testWritingWithFunkyKeys method
  1545. *
  1546. * @return void
  1547. */
  1548. public function testWritingWithFunkyKeys() {
  1549. $set = Set::insert(array(), 'Session Test', "test");
  1550. $this->assertEquals('test', Set::extract($set, 'Session Test'));
  1551. $set = Set::remove($set, 'Session Test');
  1552. $this->assertFalse(Set::check($set, 'Session Test'));
  1553. $expected = array('Session Test' => array('Test Case' => 'test'));
  1554. $this->assertEquals(Set::insert(array(), 'Session Test.Test Case', "test"), $expected);
  1555. $this->assertTrue(Set::check($expected, 'Session Test.Test Case'));
  1556. }
  1557. /**
  1558. * testDiff method
  1559. *
  1560. * @return void
  1561. */
  1562. public function testDiff() {
  1563. $a = array(
  1564. 0 => array('name' => 'main'),
  1565. 1 => array('name' => 'about')
  1566. );
  1567. $b = array(
  1568. 0 => array('name' => 'main'),
  1569. 1 => array('name' => 'about'),
  1570. 2 => array('name' => 'contact')
  1571. );
  1572. $result = Set::diff($a, $b);
  1573. $expected = array(
  1574. 2 => array('name' => 'contact')
  1575. );
  1576. $this->assertEquals($expected, $result);
  1577. $result = Set::diff($a, array());
  1578. $expected = $a;
  1579. $this->assertEquals($expected, $result);
  1580. $result = Set::diff(array(), $b);
  1581. $expected = $b;
  1582. $this->assertEquals($expected, $result);
  1583. $b = array(
  1584. 0 => array('name' => 'me'),
  1585. 1 => array('name' => 'about')
  1586. );
  1587. $result = Set::diff($a, $b);
  1588. $expected = array(
  1589. 0 => array('name' => 'main')
  1590. );
  1591. $this->assertEquals($expected, $result);
  1592. $a = array();
  1593. $b = array('name' => 'bob', 'address' => 'home');
  1594. $result = Set::diff($a, $b);
  1595. $this->assertEquals($b, $result);
  1596. $a = array('name' => 'bob', 'address' => 'home');
  1597. $b = array();
  1598. $result = Set::diff($a, $b);
  1599. $this->assertEquals($a, $result);
  1600. $a = array('key' => true, 'another' => false, 'name' => 'me');
  1601. $b = array('key' => 1, 'another' => 0);
  1602. $expected = array('name' => 'me');
  1603. $result = Set::diff($a, $b);
  1604. $this->assertEquals($expected, $result);
  1605. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1606. $b = array('key' => 'differentValue', 'another' => null);
  1607. $expected = array('key' => 'value', 'name' => 'me');
  1608. $result = Set::diff($a, $b);
  1609. $this->assertEquals($expected, $result);
  1610. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1611. $b = array('key' => 'differentValue', 'another' => 'value');
  1612. $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
  1613. $result = Set::diff($a, $b);
  1614. $this->assertEquals($expected, $result);
  1615. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1616. $b = array('key' => 'differentValue', 'another' => 'value');
  1617. $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
  1618. $result = Set::diff($b, $a);
  1619. $this->assertEquals($expected, $result);
  1620. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1621. $b = array(0 => 'differentValue', 1 => 'value');
  1622. $expected = $a + $b;
  1623. $result = Set::diff($a, $b);
  1624. $this->assertEquals($expected, $result);
  1625. }
  1626. /**
  1627. * testContains method
  1628. *
  1629. * @return void
  1630. */
  1631. public function testContains() {
  1632. $a = array(
  1633. 0 => array('name' => 'main'),
  1634. 1 => array('name' => 'about')
  1635. );
  1636. $b = array(
  1637. 0 => array('name' => 'main'),
  1638. 1 => array('name' => 'about'),
  1639. 2 => array('name' => 'contact'),
  1640. 'a' => 'b'
  1641. );
  1642. $this->assertTrue(Set::contains($a, $a));
  1643. $this->assertFalse(Set::contains($a, $b));
  1644. $this->assertTrue(Set::contains($b, $a));
  1645. }
  1646. /**
  1647. * testCombine method
  1648. *
  1649. * @return void
  1650. */
  1651. public function testCombine() {
  1652. $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
  1653. $this->assertTrue(empty($result));
  1654. $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
  1655. $this->assertTrue(empty($result));
  1656. $a = array(
  1657. array('User' => array('id' => 2, 'group_id' => 1,
  1658. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1659. array('User' => array('id' => 14, 'group_id' => 2,
  1660. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1661. array('User' => array('id' => 25, 'group_id' => 1,
  1662. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1663. $result = Set::combine($a, '{n}.User.id');
  1664. $expected = array(2 => null, 14 => null, 25 => null);
  1665. $this->assertEquals($expected, $result);
  1666. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1667. $expected = array(2 => null, 14 => null, 25 => null);
  1668. $this->assertEquals($expected, $result);
  1669. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1670. $expected = array(
  1671. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1672. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1673. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1674. $this->assertEquals($expected, $result);
  1675. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1676. $expected = array(
  1677. 2 => 'Mariano Iglesias',
  1678. 14 => 'Larry E. Masters',
  1679. 25 => 'The Gwoo');
  1680. $this->assertEquals($expected, $result);
  1681. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1682. $expected = array(
  1683. 1 => array(
  1684. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1685. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1686. 2 => array(
  1687. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1688. $this->assertEquals($expected, $result);
  1689. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1690. $expected = array(
  1691. 1 => array(
  1692. 2 => 'Mariano Iglesias',
  1693. 25 => 'The Gwoo'),
  1694. 2 => array(
  1695. 14 => 'Larry E. Masters'));
  1696. $this->assertEquals($expected, $result);
  1697. $result = Set::combine($a, '{n}.User.id');
  1698. $expected = array(2 => null, 14 => null, 25 => null);
  1699. $this->assertEquals($expected, $result);
  1700. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1701. $expected = array(
  1702. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1703. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1704. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1705. $this->assertEquals($expected, $result);
  1706. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1707. $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
  1708. $this->assertEquals($expected, $result);
  1709. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1710. $expected = array(
  1711. 1 => array(
  1712. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1713. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1714. 2 => array(
  1715. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1716. $this->assertEquals($expected, $result);
  1717. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1718. $expected = array(
  1719. 1 => array(
  1720. 2 => 'Mariano Iglesias',
  1721. 25 => 'The Gwoo'),
  1722. 2 => array(
  1723. 14 => 'Larry E. Masters'));
  1724. $this->assertEquals($expected, $result);
  1725. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  1726. $expected = array(
  1727. 1 => array(
  1728. 2 => 'mariano.iglesias: Mariano Iglesias',
  1729. 25 => 'gwoo: The Gwoo'),
  1730. 2 => array(14 => 'phpnut: Larry E. Masters'));
  1731. $this->assertEquals($expected, $result);
  1732. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1733. $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
  1734. $this->assertEquals($expected, $result);
  1735. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1736. $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
  1737. $this->assertEquals($expected, $result);
  1738. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1739. $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
  1740. $this->assertEquals($expected, $result);
  1741. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1742. $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
  1743. $this->assertEquals($expected, $result);
  1744. $b = new stdClass();
  1745. $b->users = array(
  1746. array('User' => array('id' => 2, 'group_id' => 1,
  1747. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1748. array('User' => array('id' => 14, 'group_id' => 2,
  1749. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1750. array('User' => array('id' => 25, 'group_id' => 1,
  1751. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1752. $result = Set::combine($b, 'users.{n}.User.id');
  1753. $expected = array(2 => null, 14 => null, 25 => null);
  1754. $this->assertEquals($expected, $result);
  1755. $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
  1756. $expected = array(2 => null, 14 => null, 25 => null);
  1757. $this->assertEquals($expected, $result);
  1758. $result = Set::combine($a, 'fail', 'fail');
  1759. $this->assertSame(array(), $result);
  1760. }
  1761. /**
  1762. * testMapReverse method
  1763. *
  1764. * @return void
  1765. */
  1766. public function testMapReverse() {
  1767. $result = Set::reverse(null);
  1768. $this->assertEquals(null, $result);
  1769. $result = Set::reverse(false);
  1770. $this->assertEquals(false, $result);
  1771. $expected = array(
  1772. 'Array1' => array(
  1773. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
  1774. 'Array2' => array(
  1775. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1776. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1777. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1778. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1779. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1780. 'Array3' => array(
  1781. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1782. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1783. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1784. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1785. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1786. $map = Set::map($expected, true);
  1787. $this->assertEquals($expected['Array1']['Array1Data1'], $map->Array1->Array1Data1);
  1788. $this->assertEquals($expected['Array2'][0]['Array2Data1'], $map->Array2[0]->Array2Data1);
  1789. $result = Set::reverse($map);
  1790. $this->assertEquals($expected, $result);
  1791. $expected = array(
  1792. 'Post' => array('id' => 1, 'title' => 'First Post'),
  1793. 'Comment' => array(
  1794. array('id' => 1, 'title' => 'First Comment'),
  1795. array('id' => 2, 'title' => 'Second Comment')
  1796. ),
  1797. 'Tag' => array(
  1798. array('id' => 1, 'title' => 'First Tag'),
  1799. array('id' => 2, 'title' => 'Second Tag')
  1800. ),
  1801. );
  1802. $map = Set::map($expected);
  1803. $this->assertEquals($expected['Post']['title'], $map->title);
  1804. foreach ($map->Comment as $comment) {
  1805. $ids[] = $comment->id;
  1806. }
  1807. $this->assertEquals(array(1, 2), $ids);
  1808. $expected = array(
  1809. 'Array1' => array(
  1810. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1811. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1812. 'string' => 1,
  1813. 'another' => 'string',
  1814. 'some' => 'thing else',
  1815. 'Array2' => array(
  1816. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1817. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1818. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1819. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1820. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1821. 'Array3' => array(
  1822. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1823. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1824. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1825. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1826. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1827. $map = Set::map($expected, true);
  1828. $result = Set::reverse($map);
  1829. $this->assertEquals($expected, $result);
  1830. $expected = array(
  1831. 'Array1' => array(
  1832. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1833. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1834. 'string' => 1,
  1835. 'another' => 'string',
  1836. 'some' => 'thing else',
  1837. 'Array2' => array(
  1838. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1839. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1840. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1841. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1842. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1843. 'string2' => 1,
  1844. 'another2' => 'string',
  1845. 'some2' => 'thing else',
  1846. 'Array3' => array(
  1847. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1848. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1849. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1850. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1851. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
  1852. 'string3' => 1,
  1853. 'another3' => 'string',
  1854. 'some3' => 'thing else');
  1855. $map = Set::map($expected, true);
  1856. $result = Set::reverse($map);
  1857. $this->assertEquals($expected, $result);
  1858. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
  1859. $map = Set::map($expected);
  1860. $result = Set::reverse($map);
  1861. $this->assertEquals($expected, $result);
  1862. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
  1863. $class = new stdClass;
  1864. $class->User = new stdClass;
  1865. $class->User->psword = 'whatever';
  1866. $class->User->Icon = new stdClass;
  1867. $class->User->Icon->id = 851;
  1868. $result = Set::reverse($class);
  1869. $this->assertEquals($expected, $result);
  1870. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
  1871. $class = new stdClass;
  1872. $class->User = new stdClass;
  1873. $class->User->psword = 'whatever';
  1874. $class->User->Icon = new stdClass;
  1875. $class->User->Icon->id = 851;
  1876. $class->User->Profile = new stdClass;
  1877. $class->User->Profile->name = 'Some Name';
  1878. $class->User->Profile->address = 'Some Address';
  1879. $result = Set::reverse($class);
  1880. $this->assertEquals($expected, $result);
  1881. $expected = array('User' => array('psword' => 'whatever',
  1882. 'Icon' => array('id' => 851),
  1883. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1884. 'Comment' => array(
  1885. array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1886. array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1887. $class = new stdClass;
  1888. $class->User = new stdClass;
  1889. $class->User->psword = 'whatever';
  1890. $class->User->Icon = new stdClass;
  1891. $class->User->Icon->id = 851;
  1892. $class->User->Profile = new stdClass;
  1893. $class->User->Profile->name = 'Some Name';
  1894. $class->User->Profile->address = 'Some Address';
  1895. $class->User->Comment = new stdClass;
  1896. $class->User->Comment->{'0'} = new stdClass;
  1897. $class->User->Comment->{'0'}->id = 1;
  1898. $class->User->Comment->{'0'}->article_id = 1;
  1899. $class->User->Comment->{'0'}->user_id = 1;
  1900. $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
  1901. $class->User->Comment->{'0'}->published = 'Y';
  1902. $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
  1903. $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
  1904. $class->User->Comment->{'1'} = new stdClass;
  1905. $class->User->Comment->{'1'}->id = 2;
  1906. $class->User->Comment->{'1'}->article_id = 1;
  1907. $class->User->Comment->{'1'}->user_id = 2;
  1908. $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
  1909. $class->User->Comment->{'1'}->published = 'Y';
  1910. $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
  1911. $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
  1912. $result = Set::reverse($class);
  1913. $this->assertEquals($expected, $result);
  1914. $expected = array('User' => array('psword' => 'whatever',
  1915. 'Icon' => array('id' => 851),
  1916. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1917. 'Comment' => array(
  1918. array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1919. array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1920. // @codingStandardsIgnoreStart
  1921. $class = new stdClass;
  1922. $class->User = new stdClass;
  1923. $class->User->psword = 'whatever';
  1924. $class->User->Icon = new stdClass;
  1925. $class->User->Icon->id = 851;
  1926. $class->User->Profile = new stdClass;
  1927. $class->User->Profile->name = 'Some Name';
  1928. $class->User->Profile->address = 'Some Address';
  1929. $class->User->Comment = array();
  1930. $comment = new stdClass;
  1931. $comment->id = 1;
  1932. $comment->article_id = 1;
  1933. $comment->user_id = 1;
  1934. $comment->comment = 'First Comment for First Article';
  1935. $comment->published = 'Y';
  1936. $comment->created = '2007-03-18 10:47:23';
  1937. $comment->updated = '2007-03-18 10:49:31';
  1938. $comment2 = new stdClass;
  1939. $comment2->id = 2;
  1940. $comment2->article_id = 1;
  1941. $comment2->user_id = 2;
  1942. $comment2->comment = 'Second Comment for First Article';
  1943. $comment2->published = 'Y';
  1944. $comment2->created = '2007-03-18 10:47:23';
  1945. $comment2->updated = '2007-03-18 10:49:31';
  1946. // @codingStandardsIgnoreEnd
  1947. $class->User->Comment = array($comment, $comment2);
  1948. $result = Set::reverse($class);
  1949. $this->assertEquals($expected, $result);
  1950. $class = new stdClass;
  1951. $class->User = new stdClass;
  1952. $class->User->id = 100;
  1953. $class->someString = 'this is some string';
  1954. $class->Profile = new stdClass;
  1955. $class->Profile->name = 'Joe Mamma';
  1956. $result = Set::reverse($class);
  1957. $expected = array(
  1958. 'User' => array('id' => '100'),
  1959. 'someString' => 'this is some string',
  1960. 'Profile' => array('name' => 'Joe Mamma')
  1961. );
  1962. $this->assertEquals($expected, $result);
  1963. // @codingStandardsIgnoreStart
  1964. $class = new stdClass;
  1965. $class->User = new stdClass;
  1966. $class->User->id = 100;
  1967. $class->User->_name_ = 'User';
  1968. $class->Profile = new stdClass;
  1969. $class->Profile->name = 'Joe Mamma';
  1970. $class->Profile->_name_ = 'Profile';
  1971. // @codingStandardsIgnoreEnd
  1972. $result = Set::reverse($class);
  1973. $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
  1974. $this->assertEquals($expected, $result);
  1975. }
  1976. /**
  1977. * testFormatting method
  1978. *
  1979. * @return void
  1980. */
  1981. public function testFormatting() {
  1982. $data = array(
  1983. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  1984. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
  1985. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
  1986. $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
  1987. $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
  1988. $this->assertEquals($expected, $result);
  1989. $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
  1990. $this->assertEquals($expected, $result);
  1991. $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
  1992. $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
  1993. $this->assertEquals($expected, $result);
  1994. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
  1995. $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
  1996. $this->assertEquals($expected, $result);
  1997. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
  1998. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  1999. $this->assertEquals($expected, $result);
  2000. $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  2001. $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
  2002. $this->assertEquals($expected, $result);
  2003. $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  2004. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  2005. $this->assertEquals($expected, $result);
  2006. $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
  2007. $expected = array('42, Nate', '0, Larry', '0, Garrett');
  2008. $this->assertEquals($expected, $result);
  2009. $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
  2010. $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
  2011. $this->assertEquals($expected, $result);
  2012. }
  2013. /**
  2014. * testFormattingNullValues method
  2015. *
  2016. * @return void
  2017. */
  2018. public function testFormattingNullValues() {
  2019. $data = array(
  2020. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  2021. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null)),
  2022. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null)));
  2023. $result = Set::format($data, '%s', array('{n}.Person.something'));
  2024. $expected = array('42', '', '');
  2025. $this->assertEquals($expected, $result);
  2026. $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something'));
  2027. $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
  2028. $this->assertEquals($expected, $result);
  2029. }
  2030. /**
  2031. * testCountDim method
  2032. *
  2033. * @return void
  2034. */
  2035. public function testCountDim() {
  2036. $data = array('one', '2', 'three');
  2037. $result = Set::countDim($data);
  2038. $this->assertEquals(1, $result);
  2039. $data = array('1' => '1.1', '2', '3');
  2040. $result = Set::countDim($data);
  2041. $this->assertEquals(1, $result);
  2042. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  2043. $result = Set::countDim($data);
  2044. $this->assertEquals(2, $result);
  2045. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  2046. $result = Set::countDim($data);
  2047. $this->assertEquals(1, $result);
  2048. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  2049. $result = Set::countDim($data, true);
  2050. $this->assertEquals(2, $result);
  2051. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2052. $result = Set::countDim($data);
  2053. $this->assertEquals(2, $result);
  2054. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2055. $result = Set::countDim($data, true);
  2056. $this->assertEquals(3, $result);
  2057. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2058. $result = Set::countDim($data, true);
  2059. $this->assertEquals(4, $result);
  2060. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2061. $result = Set::countDim($data, true);
  2062. $this->assertEquals(5, $result);
  2063. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2064. $result = Set::countDim($data, true);
  2065. $this->assertEquals(5, $result);
  2066. $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2067. $result = Set::countDim($set, false, 0);
  2068. $this->assertEquals(2, $result);
  2069. $result = Set::countDim($set, true);
  2070. $this->assertEquals(5, $result);
  2071. }
  2072. /**
  2073. * testMapNesting method
  2074. *
  2075. * @return void
  2076. */
  2077. public function testMapNesting() {
  2078. $expected = array(
  2079. array(
  2080. "IndexedPage" => array(
  2081. "id" => 1,
  2082. "url" => 'http://blah.com/',
  2083. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2084. 'headers' => array(
  2085. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  2086. 'Server' => "Apache",
  2087. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  2088. 'Cache-Control' => "private",
  2089. 'Pragma' => "no-cache",
  2090. 'Content-Type' => "text/html; charset=UTF-8",
  2091. 'X-Original-Transfer-Encoding' => "chunked",
  2092. 'Content-Length' => "50210",
  2093. ),
  2094. 'meta' => array(
  2095. 'keywords' => array('testing','tests'),
  2096. 'description' => 'describe me',
  2097. ),
  2098. 'get_vars' => '',
  2099. 'post_vars' => array(),
  2100. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2101. 'redirect' => '',
  2102. 'created' => "1195055503",
  2103. 'updated' => "1195055503",
  2104. )
  2105. ),
  2106. array(
  2107. "IndexedPage" => array(
  2108. "id" => 2,
  2109. "url" => 'http://blah.com/',
  2110. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2111. 'headers' => array(
  2112. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  2113. 'Server' => "Apache",
  2114. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  2115. 'Cache-Control' => "private",
  2116. 'Pragma' => "no-cache",
  2117. 'Content-Type' => "text/html; charset=UTF-8",
  2118. 'X-Original-Transfer-Encoding' => "chunked",
  2119. 'Content-Length' => "50210",
  2120. ),
  2121. 'meta' => array(
  2122. 'keywords' => array('testing','tests'),
  2123. 'description' => 'describe me',
  2124. ),
  2125. 'get_vars' => '',
  2126. 'post_vars' => array(),
  2127. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2128. 'redirect' => '',
  2129. 'created' => "1195055503",
  2130. 'updated' => "1195055503",
  2131. ),
  2132. )
  2133. );
  2134. $mapped = Set::map($expected);
  2135. $ids = array();
  2136. foreach ($mapped as $object) {
  2137. $ids[] = $object->id;
  2138. }
  2139. $this->assertEquals(array(1, 2), $ids);
  2140. $this->assertEquals($expected[0]['IndexedPage']['headers'], get_object_vars($mapped[0]->headers));
  2141. $result = Set::reverse($mapped);
  2142. $this->assertEquals($expected, $result);
  2143. $data = array(
  2144. array(
  2145. "IndexedPage" => array(
  2146. "id" => 1,
  2147. "url" => 'http://blah.com/',
  2148. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2149. 'get_vars' => '',
  2150. 'redirect' => '',
  2151. 'created' => "1195055503",
  2152. 'updated' => "1195055503",
  2153. )
  2154. ),
  2155. array(
  2156. "IndexedPage" => array(
  2157. "id" => 2,
  2158. "url" => 'http://blah.com/',
  2159. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2160. 'get_vars' => '',
  2161. 'redirect' => '',
  2162. 'created' => "1195055503",
  2163. 'updated' => "1195055503",
  2164. ),
  2165. )
  2166. );
  2167. $mapped = Set::map($data);
  2168. // @codingStandardsIgnoreStart
  2169. $expected = new stdClass();
  2170. $expected->_name_ = 'IndexedPage';
  2171. $expected->id = 2;
  2172. $expected->url = 'http://blah.com/';
  2173. $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
  2174. $expected->get_vars = '';
  2175. $expected->redirect = '';
  2176. $expected->created = "1195055503";
  2177. $expected->updated = "1195055503";
  2178. // @codingStandardsIgnoreEnd
  2179. $this->assertEquals($expected, $mapped[1]);
  2180. $ids = array();
  2181. foreach ($mapped as $object) {
  2182. $ids[] = $object->id;
  2183. }
  2184. $this->assertEquals(array(1, 2), $ids);
  2185. $result = Set::map(null);
  2186. $expected = null;
  2187. $this->assertEquals($expected, $result);
  2188. }
  2189. /**
  2190. * testNestedMappedData method
  2191. *
  2192. * @return void
  2193. */
  2194. public function testNestedMappedData() {
  2195. $result = Set::map(array(
  2196. array(
  2197. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  2198. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2199. )
  2200. , array(
  2201. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  2202. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
  2203. )
  2204. ));
  2205. // @codingStandardsIgnoreStart
  2206. $expected = new stdClass;
  2207. $expected->_name_ = 'Post';
  2208. $expected->id = '1';
  2209. $expected->author_id = '1';
  2210. $expected->title = 'First Post';
  2211. $expected->body = 'First Post Body';
  2212. $expected->published = 'Y';
  2213. $expected->created = "2007-03-18 10:39:23";
  2214. $expected->updated = "2007-03-18 10:41:31";
  2215. $expected->Author = new stdClass;
  2216. $expected->Author->id = '1';
  2217. $expected->Author->user = 'mariano';
  2218. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2219. $expected->Author->created = "2007-03-17 01:16:23";
  2220. $expected->Author->updated = "2007-03-17 01:18:31";
  2221. $expected->Author->test = "working";
  2222. $expected->Author->_name_ = 'Author';
  2223. $expected2 = new stdClass;
  2224. $expected2->_name_ = 'Post';
  2225. $expected2->id = '2';
  2226. $expected2->author_id = '3';
  2227. $expected2->title = 'Second Post';
  2228. $expected2->body = 'Second Post Body';
  2229. $expected2->published = 'Y';
  2230. $expected2->created = "2007-03-18 10:41:23";
  2231. $expected2->updated = "2007-03-18 10:43:31";
  2232. $expected2->Author = new stdClass;
  2233. $expected2->Author->id = '3';
  2234. $expected2->Author->user = 'larry';
  2235. $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2236. $expected2->Author->created = "2007-03-17 01:20:23";
  2237. $expected2->Author->updated = "2007-03-17 01:22:31";
  2238. $expected2->Author->test = "working";
  2239. $expected2->Author->_name_ = 'Author';
  2240. // @codingStandardsIgnoreEnd
  2241. $test = array();
  2242. $test[0] = $expected;
  2243. $test[1] = $expected2;
  2244. $this->assertEquals($test, $result);
  2245. $result = Set::map(
  2246. array(
  2247. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  2248. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2249. )
  2250. );
  2251. // @codingStandardsIgnoreStart
  2252. $expected = new stdClass;
  2253. $expected->_name_ = 'Post';
  2254. $expected->id = '1';
  2255. $expected->author_id = '1';
  2256. $expected->title = 'First Post';
  2257. $expected->body = 'First Post Body';
  2258. $expected->published = 'Y';
  2259. $expected->created = "2007-03-18 10:39:23";
  2260. $expected->updated = "2007-03-18 10:41:31";
  2261. $expected->Author = new stdClass;
  2262. $expected->Author->id = '1';
  2263. $expected->Author->user = 'mariano';
  2264. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2265. $expected->Author->created = "2007-03-17 01:16:23";
  2266. $expected->Author->updated = "2007-03-17 01:18:31";
  2267. $expected->Author->test = "working";
  2268. $expected->Author->_name_ = 'Author';
  2269. // @codingStandardsIgnoreEnd
  2270. $this->assertEquals($expected, $result);
  2271. //Case where extra HABTM fields come back in a result
  2272. $data = array(
  2273. 'User' => array(
  2274. 'id' => 1,
  2275. 'email' => '[email protected]',
  2276. 'first_name' => 'John',
  2277. 'last_name' => 'Smith',
  2278. ),
  2279. 'Piece' => array(
  2280. array(
  2281. 'id' => 1,
  2282. 'title' => 'Moonlight Sonata',
  2283. 'composer' => 'Ludwig van Beethoven',
  2284. 'PiecesUser' => array(
  2285. 'id' => 1,
  2286. 'created' => '2008-01-01 00:00:00',
  2287. 'modified' => '2008-01-01 00:00:00',
  2288. 'piece_id' => 1,
  2289. 'user_id' => 2,
  2290. )
  2291. ),
  2292. array(
  2293. 'id' => 2,
  2294. 'title' => 'Moonlight Sonata 2',
  2295. 'composer' => 'Ludwig van Beethoven',
  2296. 'PiecesUser' => array(
  2297. 'id' => 2,
  2298. 'created' => '2008-01-01 00:00:00',
  2299. 'modified' => '2008-01-01 00:00:00',
  2300. 'piece_id' => 2,
  2301. 'user_id' => 2,
  2302. )
  2303. )
  2304. )
  2305. );
  2306. $result = Set::map($data);
  2307. // @codingStandardsIgnoreStart
  2308. $expected = new stdClass();
  2309. $expected->_name_ = 'User';
  2310. $expected->id = 1;
  2311. $expected->email = '[email protected]';
  2312. $expected->first_name = 'John';
  2313. $expected->last_name = 'Smith';
  2314. $piece = new stdClass();
  2315. $piece->id = 1;
  2316. $piece->title = 'Moonlight Sonata';
  2317. $piece->composer = 'Ludwig van Beethoven';
  2318. $piece->PiecesUser = new stdClass();
  2319. $piece->PiecesUser->id = 1;
  2320. $piece->PiecesUser->created = '2008-01-01 00:00:00';
  2321. $piece->PiecesUser->modified = '2008-01-01 00:00:00';
  2322. $piece->PiecesUser->piece_id = 1;
  2323. $piece->PiecesUser->user_id = 2;
  2324. $piece->PiecesUser->_name_ = 'PiecesUser';
  2325. $piece->_name_ = 'Piece';
  2326. $piece2 = new stdClass();
  2327. $piece2->id = 2;
  2328. $piece2->title = 'Moonlight Sonata 2';
  2329. $piece2->composer = 'Ludwig van Beethoven';
  2330. $piece2->PiecesUser = new stdClass();
  2331. $piece2->PiecesUser->id = 2;
  2332. $piece2->PiecesUser->created = '2008-01-01 00:00:00';
  2333. $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
  2334. $piece2->PiecesUser->piece_id = 2;
  2335. $piece2->PiecesUser->user_id = 2;
  2336. $piece2->PiecesUser->_name_ = 'PiecesUser';
  2337. $piece2->_name_ = 'Piece';
  2338. // @codingStandardsIgnoreEnd
  2339. $expected->Piece = array($piece, $piece2);
  2340. $this->assertEquals($expected, $result);
  2341. //Same data, but should work if _name_ has been manually defined:
  2342. $data = array(
  2343. 'User' => array(
  2344. 'id' => 1,
  2345. 'email' => '[email protected]',
  2346. 'first_name' => 'John',
  2347. 'last_name' => 'Smith',
  2348. '_name_' => 'FooUser',
  2349. ),
  2350. 'Piece' => array(
  2351. array(
  2352. 'id' => 1,
  2353. 'title' => 'Moonlight Sonata',
  2354. 'composer' => 'Ludwig van Beethoven',
  2355. '_name_' => 'FooPiece',
  2356. 'PiecesUser' => array(
  2357. 'id' => 1,
  2358. 'created' => '2008-01-01 00:00:00',
  2359. 'modified' => '2008-01-01 00:00:00',
  2360. 'piece_id' => 1,
  2361. 'user_id' => 2,
  2362. '_name_' => 'FooPiecesUser',
  2363. )
  2364. ),
  2365. array(
  2366. 'id' => 2,
  2367. 'title' => 'Moonlight Sonata 2',
  2368. 'composer' => 'Ludwig van Beethoven',
  2369. '_name_' => 'FooPiece',
  2370. 'PiecesUser' => array(
  2371. 'id' => 2,
  2372. 'created' => '2008-01-01 00:00:00',
  2373. 'modified' => '2008-01-01 00:00:00',
  2374. 'piece_id' => 2,
  2375. 'user_id' => 2,
  2376. '_name_' => 'FooPiecesUser',
  2377. )
  2378. )
  2379. )
  2380. );
  2381. $result = Set::map($data);
  2382. // @codingStandardsIgnoreStart
  2383. $expected = new stdClass();
  2384. $expected->_name_ = 'FooUser';
  2385. $expected->id = 1;
  2386. $expected->email = '[email protected]';
  2387. $expected->first_name = 'John';
  2388. $expected->last_name = 'Smith';
  2389. $piece = new stdClass();
  2390. $piece->id = 1;
  2391. $piece->title = 'Moonlight Sonata';
  2392. $piece->composer = 'Ludwig van Beethoven';
  2393. $piece->_name_ = 'FooPiece';
  2394. $piece->PiecesUser = new stdClass();
  2395. $piece->PiecesUser->id = 1;
  2396. $piece->PiecesUser->created = '2008-01-01 00:00:00';
  2397. $piece->PiecesUser->modified = '2008-01-01 00:00:00';
  2398. $piece->PiecesUser->piece_id = 1;
  2399. $piece->PiecesUser->user_id = 2;
  2400. $piece->PiecesUser->_name_ = 'FooPiecesUser';
  2401. $piece2 = new stdClass();
  2402. $piece2->id = 2;
  2403. $piece2->title = 'Moonlight Sonata 2';
  2404. $piece2->composer = 'Ludwig van Beethoven';
  2405. $piece2->_name_ = 'FooPiece';
  2406. $piece2->PiecesUser = new stdClass();
  2407. $piece2->PiecesUser->id = 2;
  2408. $piece2->PiecesUser->created = '2008-01-01 00:00:00';
  2409. $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
  2410. $piece2->PiecesUser->piece_id = 2;
  2411. $piece2->PiecesUser->user_id = 2;
  2412. $piece2->PiecesUser->_name_ = 'FooPiecesUser';
  2413. // @codingStandardsIgnoreEnd
  2414. $expected->Piece = array($piece, $piece2);
  2415. $this->assertEquals($expected, $result);
  2416. }
  2417. /**
  2418. * testPushDiff method
  2419. *
  2420. * @return void
  2421. */
  2422. public function testPushDiff() {
  2423. $array1 = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2'));
  2424. $array2 = array('ModelTwo' => array('id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2'));
  2425. $result = Set::pushDiff($array1, $array2);
  2426. $this->assertEquals($array1 + $array2, $result);
  2427. $array3 = array('ModelOne' => array('id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3'));
  2428. $result = Set::pushDiff($array1, $array3);
  2429. $expected = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3'));
  2430. $this->assertEquals($expected, $result);
  2431. $array1 = array(
  2432. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2433. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
  2434. $array2 = array(
  2435. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2436. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2')));
  2437. $result = Set::pushDiff($array1, $array2);
  2438. $this->assertEquals($array1, $result);
  2439. $array3 = array(0 => array('ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')));
  2440. $result = Set::pushDiff($array1, $array3);
  2441. $expected = array(
  2442. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'),
  2443. 'ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')),
  2444. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
  2445. $this->assertEquals($expected, $result);
  2446. $result = Set::pushDiff($array1, null);
  2447. $this->assertEquals($array1, $result);
  2448. $result = Set::pushDiff($array1, $array2);
  2449. $this->assertEquals($array1 + $array2, $result);
  2450. }
  2451. /**
  2452. * testSetApply method
  2453. * @return void
  2454. *
  2455. */
  2456. public function testApply() {
  2457. $data = array(
  2458. array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)),
  2459. array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)),
  2460. array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3))
  2461. );
  2462. $result = Set::apply('/Movie/rating', $data, 'array_sum');
  2463. $expected = 9;
  2464. $this->assertEquals($expected, $result);
  2465. $result = Set::apply('/Movie/rating', $data, 'array_product');
  2466. $expected = 15;
  2467. $this->assertEquals($expected, $result);
  2468. $result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
  2469. $expected = array('Movie 3', 'Movie 1', 'Movie 2');
  2470. $this->assertEquals($expected, $result);
  2471. $result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
  2472. $expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
  2473. $this->assertEquals($expected, $result);
  2474. $result = Set::apply('/Movie/rating', $data, array('SetTest', 'method'), array('type' => 'reduce'));
  2475. $expected = 9;
  2476. $this->assertEquals($expected, $result);
  2477. $result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
  2478. $expected = null;
  2479. $this->assertEquals($expected, $result);
  2480. }
  2481. /**
  2482. * Helper method to test Set::apply()
  2483. *
  2484. * @return void
  2485. */
  2486. public static function method($val1, $val2) {
  2487. $val1 += $val2;
  2488. return $val1;
  2489. }
  2490. /**
  2491. * testXmlSetReverse method
  2492. *
  2493. * @return void
  2494. */
  2495. public function testXmlSetReverse() {
  2496. App::uses('Xml', 'Utility');
  2497. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2498. <rss version="2.0">
  2499. <channel>
  2500. <title>Cake PHP Google Group</title>
  2501. <link>http://groups.google.com/group/cake-php</link>
  2502. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  2503. <language>en</language>
  2504. <item>
  2505. <title>constructng result array when using findall</title>
  2506. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2507. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  2508. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2509. <author>[email protected](bpscrugs)</author>
  2510. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2511. </item>
  2512. <item>
  2513. <title>Re: share views between actions?</title>
  2514. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2515. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  2516. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2517. <author>[email protected](subtropolis zijn)</author>
  2518. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2519. </item>
  2520. </channel>
  2521. </rss>';
  2522. $xml = Xml::build($string);
  2523. $result = Set::reverse($xml);
  2524. $expected = array('rss' => array(
  2525. '@version' => '2.0',
  2526. 'channel' => array(
  2527. 'title' => 'Cake PHP Google Group',
  2528. 'link' => 'http://groups.google.com/group/cake-php',
  2529. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  2530. 'language' => 'en',
  2531. 'item' => array(
  2532. array(
  2533. 'title' => 'constructng result array when using findall',
  2534. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2535. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
  2536. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2537. 'author' => '[email protected](bpscrugs)',
  2538. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2539. ),
  2540. array(
  2541. 'title' => 'Re: share views between actions?',
  2542. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2543. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  2544. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2545. 'author' => '[email protected](subtropolis zijn)',
  2546. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2547. )
  2548. )
  2549. )
  2550. ));
  2551. $this->assertEquals($expected, $result);
  2552. $string = '<data><post title="Title of this post" description="cool"/></data>';
  2553. $xml = Xml::build($string);
  2554. $result = Set::reverse($xml);
  2555. $expected = array('data' => array('post' => array('@title' => 'Title of this post', '@description' => 'cool')));
  2556. $this->assertEquals($expected, $result);
  2557. $xml = Xml::build('<example><item><title>An example of a correctly reversed SimpleXMLElement</title><desc/></item></example>');
  2558. $result = Set::reverse($xml);
  2559. $expected = array('example' =>
  2560. array(
  2561. 'item' => array(
  2562. 'title' => 'An example of a correctly reversed SimpleXMLElement',
  2563. 'desc' => '',
  2564. )
  2565. )
  2566. );
  2567. $this->assertEquals($expected, $result);
  2568. $xml = Xml::build('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
  2569. $result = Set::reverse($xml);
  2570. $expected =
  2571. array('example' => array(
  2572. 'item' => array(
  2573. '@attr' => '123',
  2574. 'titles' => array(
  2575. 'title' => array('title1', 'title2')
  2576. )
  2577. )
  2578. )
  2579. );
  2580. $this->assertEquals($expected, $result);
  2581. $xml = Xml::build('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
  2582. $result = Set::reverse($xml);
  2583. $expected =
  2584. array('example' => array(
  2585. '@attr' => 'ex_attr',
  2586. 'item' => array(
  2587. '@attr' => '123',
  2588. 'titles' => 'list',
  2589. '@' => 'textforitems'
  2590. )
  2591. )
  2592. );
  2593. $this->assertEquals($expected, $result);
  2594. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2595. <rss version="2.0" xmlns:dc="http://www.cakephp.org/">
  2596. <channel>
  2597. <title>Cake PHP Google Group</title>
  2598. <link>http://groups.google.com/group/cake-php</link>
  2599. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  2600. <language>en</language>
  2601. <item>
  2602. <title>constructng result array when using findall</title>
  2603. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2604. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  2605. <dc:creator>cakephp</dc:creator>
  2606. <category><![CDATA[cakephp]]></category>
  2607. <category><![CDATA[model]]></category>
  2608. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2609. <author>[email protected](bpscrugs)</author>
  2610. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2611. </item>
  2612. <item>
  2613. <title>Re: share views between actions?</title>
  2614. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2615. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  2616. <dc:creator>cakephp</dc:creator>
  2617. <category><![CDATA[cakephp]]></category>
  2618. <category><![CDATA[model]]></category>
  2619. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2620. <author>[email protected](subtropolis zijn)</author>
  2621. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2622. </item>
  2623. </channel>
  2624. </rss>';
  2625. $xml = Xml::build($string);
  2626. $result = Set::reverse($xml);
  2627. $expected = array('rss' => array(
  2628. '@version' => '2.0',
  2629. 'channel' => array(
  2630. 'title' => 'Cake PHP Google Group',
  2631. 'link' => 'http://groups.google.com/group/cake-php',
  2632. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  2633. 'language' => 'en',
  2634. 'item' => array(
  2635. array(
  2636. 'title' => 'constructng result array when using findall',
  2637. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2638. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
  2639. 'dc:creator' => 'cakephp',
  2640. 'category' => array('cakephp', 'model'),
  2641. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2642. 'author' => '[email protected](bpscrugs)',
  2643. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2644. ),
  2645. array(
  2646. 'title' => 'Re: share views between actions?',
  2647. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2648. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  2649. 'dc:creator' => 'cakephp',
  2650. 'category' => array('cakephp', 'model'),
  2651. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2652. 'author' => '[email protected](subtropolis zijn)',
  2653. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2654. )
  2655. )
  2656. )
  2657. ));
  2658. $this->assertEquals($expected, $result);
  2659. $text = '<?xml version="1.0" encoding="UTF-8"?>
  2660. <XRDS xmlns="xri://$xrds">
  2661. <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2662. <Type>xri://$xrds*simple</Type>
  2663. <Expires>2008-04-13T07:34:58Z</Expires>
  2664. <Service>
  2665. <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
  2666. <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
  2667. <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
  2668. <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
  2669. <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
  2670. </Service>
  2671. </XRD>
  2672. <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2673. <Type>xri://$xrds*simple</Type>
  2674. <Service priority="10">
  2675. <Type>http://oauth.net/discovery/1.0</Type>
  2676. <URI>#oauth</URI>
  2677. </Service>
  2678. </XRD>
  2679. </XRDS>';
  2680. $xml = Xml::build($text);
  2681. $result = Set::reverse($xml);
  2682. $expected = array('XRDS' => array(
  2683. 'XRD' => array(
  2684. array(
  2685. '@xml:id' => 'oauth',
  2686. '@version' => '2.0',
  2687. 'Type' => 'xri://$xrds*simple',
  2688. 'Expires' => '2008-04-13T07:34:58Z',
  2689. 'Service' => array(
  2690. 'Type' => array(
  2691. 'http://oauth.net/core/1.0/endpoint/authorize',
  2692. 'http://oauth.net/core/1.0/parameters/auth-header',
  2693. 'http://oauth.net/core/1.0/parameters/uri-query'
  2694. ),
  2695. 'URI' => array(
  2696. array(
  2697. '@' => 'https://ma.gnolia.com/oauth/authorize',
  2698. '@priority' => '10',
  2699. ),
  2700. array(
  2701. '@' => 'http://ma.gnolia.com/oauth/authorize',
  2702. '@priority' => '20'
  2703. )
  2704. )
  2705. )
  2706. ),
  2707. array(
  2708. '@version' => '2.0',
  2709. 'Type' => 'xri://$xrds*simple',
  2710. 'Service' => array(
  2711. '@priority' => '10',
  2712. 'Type' => 'http://oauth.net/discovery/1.0',
  2713. 'URI' => '#oauth'
  2714. )
  2715. )
  2716. )
  2717. ));
  2718. $this->assertEquals($expected, $result);
  2719. }
  2720. /**
  2721. * testStrictKeyCheck method
  2722. *
  2723. * @return void
  2724. */
  2725. public function testStrictKeyCheck() {
  2726. $set = array('a' => 'hi');
  2727. $this->assertFalse(Set::check($set, 'a.b'));
  2728. }
  2729. /**
  2730. * Tests Set::flatten
  2731. *
  2732. * @see Hash test cases, as Set::flatten() is just a proxy.
  2733. * @return void
  2734. */
  2735. public function testFlatten() {
  2736. $data = array('Larry', 'Curly', 'Moe');
  2737. $result = Set::flatten($data);
  2738. $this->assertEquals($data, $result);
  2739. $data[9] = 'Shemp';
  2740. $result = Set::flatten($data);
  2741. $this->assertEquals($data, $result);
  2742. $data = array(
  2743. array(
  2744. 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
  2745. 'Author' => array(),
  2746. )
  2747. );
  2748. $result = Set::flatten($data);
  2749. $expected = array(
  2750. '0.Post.id' => '1',
  2751. '0.Post.author_id' => null,
  2752. '0.Post.title' => 'First Post',
  2753. '0.Author' => array()
  2754. );
  2755. $this->assertEquals($expected, $result);
  2756. }
  2757. /**
  2758. * Tests Set::expand
  2759. *
  2760. * @return void
  2761. */
  2762. public function testExpand() {
  2763. $data = array('My', 'Array', 'To', 'Flatten');
  2764. $flat = Set::flatten($data);
  2765. $result = Set::expand($flat);
  2766. $this->assertEquals($data, $result);
  2767. }
  2768. /**
  2769. * test normalization
  2770. *
  2771. * @return void
  2772. */
  2773. public function testNormalizeStrings() {
  2774. $result = Set::normalize('one,two,three');
  2775. $expected = array('one' => null, 'two' => null, 'three' => null);
  2776. $this->assertEquals($expected, $result);
  2777. $result = Set::normalize('one two three', true, ' ');
  2778. $expected = array('one' => null, 'two' => null, 'three' => null);
  2779. $this->assertEquals($expected, $result);
  2780. $result = Set::normalize('one , two , three ', true, ',', true);
  2781. $expected = array('one' => null, 'two' => null, 'three' => null);
  2782. $this->assertEquals($expected, $result);
  2783. }
  2784. /**
  2785. * test normalizing arrays
  2786. *
  2787. * @return void
  2788. */
  2789. public function testNormalizeArrays() {
  2790. $result = Set::normalize(array('one', 'two', 'three'));
  2791. $expected = array('one' => null, 'two' => null, 'three' => null);
  2792. $this->assertEquals($expected, $result);
  2793. $result = Set::normalize(array('one', 'two', 'three'), false);
  2794. $expected = array('one', 'two', 'three');
  2795. $this->assertEquals($expected, $result);
  2796. $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
  2797. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  2798. $this->assertEquals($expected, $result);
  2799. $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
  2800. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  2801. $this->assertEquals($expected, $result);
  2802. $result = Set::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
  2803. $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
  2804. $this->assertEquals($expected, $result);
  2805. }
  2806. /**
  2807. * test Set nest with a normal model result set. For kicks rely on Set nest detecting the key names
  2808. * automatically
  2809. *
  2810. * @return void
  2811. */
  2812. public function testNestModel() {
  2813. $input = array(
  2814. array(
  2815. 'ModelName' => array(
  2816. 'id' => 1,
  2817. 'parent_id' => null
  2818. ),
  2819. ),
  2820. array(
  2821. 'ModelName' => array(
  2822. 'id' => 2,
  2823. 'parent_id' => 1
  2824. ),
  2825. ),
  2826. array(
  2827. 'ModelName' => array(
  2828. 'id' => 3,
  2829. 'parent_id' => 1
  2830. ),
  2831. ),
  2832. array(
  2833. 'ModelName' => array(
  2834. 'id' => 4,
  2835. 'parent_id' => 1
  2836. ),
  2837. ),
  2838. array(
  2839. 'ModelName' => array(
  2840. 'id' => 5,
  2841. 'parent_id' => 1
  2842. ),
  2843. ),
  2844. array(
  2845. 'ModelName' => array(
  2846. 'id' => 6,
  2847. 'parent_id' => null
  2848. ),
  2849. ),
  2850. array(
  2851. 'ModelName' => array(
  2852. 'id' => 7,
  2853. 'parent_id' => 6
  2854. ),
  2855. ),
  2856. array(
  2857. 'ModelName' => array(
  2858. 'id' => 8,
  2859. 'parent_id' => 6
  2860. ),
  2861. ),
  2862. array(
  2863. 'ModelName' => array(
  2864. 'id' => 9,
  2865. 'parent_id' => 6
  2866. ),
  2867. ),
  2868. array(
  2869. 'ModelName' => array(
  2870. 'id' => 10,
  2871. 'parent_id' => 6
  2872. )
  2873. )
  2874. );
  2875. $expected = array(
  2876. array(
  2877. 'ModelName' => array(
  2878. 'id' => 1,
  2879. 'parent_id' => null
  2880. ),
  2881. 'children' => array(
  2882. array(
  2883. 'ModelName' => array(
  2884. 'id' => 2,
  2885. 'parent_id' => 1
  2886. ),
  2887. 'children' => array()
  2888. ),
  2889. array(
  2890. 'ModelName' => array(
  2891. 'id' => 3,
  2892. 'parent_id' => 1
  2893. ),
  2894. 'children' => array()
  2895. ),
  2896. array(
  2897. 'ModelName' => array(
  2898. 'id' => 4,
  2899. 'parent_id' => 1
  2900. ),
  2901. 'children' => array()
  2902. ),
  2903. array(
  2904. 'ModelName' => array(
  2905. 'id' => 5,
  2906. 'parent_id' => 1
  2907. ),
  2908. 'children' => array()
  2909. ),
  2910. )
  2911. ),
  2912. array(
  2913. 'ModelName' => array(
  2914. 'id' => 6,
  2915. 'parent_id' => null
  2916. ),
  2917. 'children' => array(
  2918. array(
  2919. 'ModelName' => array(
  2920. 'id' => 7,
  2921. 'parent_id' => 6
  2922. ),
  2923. 'children' => array()
  2924. ),
  2925. array(
  2926. 'ModelName' => array(
  2927. 'id' => 8,
  2928. 'parent_id' => 6
  2929. ),
  2930. 'children' => array()
  2931. ),
  2932. array(
  2933. 'ModelName' => array(
  2934. 'id' => 9,
  2935. 'parent_id' => 6
  2936. ),
  2937. 'children' => array()
  2938. ),
  2939. array(
  2940. 'ModelName' => array(
  2941. 'id' => 10,
  2942. 'parent_id' => 6
  2943. ),
  2944. 'children' => array()
  2945. )
  2946. )
  2947. )
  2948. );
  2949. $result = Set::nest($input);
  2950. $this->assertEquals($expected, $result);
  2951. }
  2952. /**
  2953. * test Set nest with a normal model result set, and a nominated root id
  2954. *
  2955. * @return void
  2956. */
  2957. public function testNestModelExplicitRoot() {
  2958. $input = array(
  2959. array(
  2960. 'ModelName' => array(
  2961. 'id' => 1,
  2962. 'parent_id' => null
  2963. ),
  2964. ),
  2965. array(
  2966. 'ModelName' => array(
  2967. 'id' => 2,
  2968. 'parent_id' => 1
  2969. ),
  2970. ),
  2971. array(
  2972. 'ModelName' => array(
  2973. 'id' => 3,
  2974. 'parent_id' => 1
  2975. ),
  2976. ),
  2977. array(
  2978. 'ModelName' => array(
  2979. 'id' => 4,
  2980. 'parent_id' => 1
  2981. ),
  2982. ),
  2983. array(
  2984. 'ModelName' => array(
  2985. 'id' => 5,
  2986. 'parent_id' => 1
  2987. ),
  2988. ),
  2989. array(
  2990. 'ModelName' => array(
  2991. 'id' => 6,
  2992. 'parent_id' => null
  2993. ),
  2994. ),
  2995. array(
  2996. 'ModelName' => array(
  2997. 'id' => 7,
  2998. 'parent_id' => 6
  2999. ),
  3000. ),
  3001. array(
  3002. 'ModelName' => array(
  3003. 'id' => 8,
  3004. 'parent_id' => 6
  3005. ),
  3006. ),
  3007. array(
  3008. 'ModelName' => array(
  3009. 'id' => 9,
  3010. 'parent_id' => 6
  3011. ),
  3012. ),
  3013. array(
  3014. 'ModelName' => array(
  3015. 'id' => 10,
  3016. 'parent_id' => 6
  3017. )
  3018. )
  3019. );
  3020. $expected = array(
  3021. array(
  3022. 'ModelName' => array(
  3023. 'id' => 6,
  3024. 'parent_id' => null
  3025. ),
  3026. 'children' => array(
  3027. array(
  3028. 'ModelName' => array(
  3029. 'id' => 7,
  3030. 'parent_id' => 6
  3031. ),
  3032. 'children' => array()
  3033. ),
  3034. array(
  3035. 'ModelName' => array(
  3036. 'id' => 8,
  3037. 'parent_id' => 6
  3038. ),
  3039. 'children' => array()
  3040. ),
  3041. array(
  3042. 'ModelName' => array(
  3043. 'id' => 9,
  3044. 'parent_id' => 6
  3045. ),
  3046. 'children' => array()
  3047. ),
  3048. array(
  3049. 'ModelName' => array(
  3050. 'id' => 10,
  3051. 'parent_id' => 6
  3052. ),
  3053. 'children' => array()
  3054. )
  3055. )
  3056. )
  3057. );
  3058. $result = Set::nest($input, array('root' => 6));
  3059. $this->assertEquals($expected, $result);
  3060. }
  3061. /**
  3062. * test Set nest with a 1d array - this method should be able to handle any type of array input
  3063. *
  3064. * @return void
  3065. */
  3066. public function testNest1Dimensional() {
  3067. $input = array(
  3068. array(
  3069. 'id' => 1,
  3070. 'parent_id' => null
  3071. ),
  3072. array(
  3073. 'id' => 2,
  3074. 'parent_id' => 1
  3075. ),
  3076. array(
  3077. 'id' => 3,
  3078. 'parent_id' => 1
  3079. ),
  3080. array(
  3081. 'id' => 4,
  3082. 'parent_id' => 1
  3083. ),
  3084. array(
  3085. 'id' => 5,
  3086. 'parent_id' => 1
  3087. ),
  3088. array(
  3089. 'id' => 6,
  3090. 'parent_id' => null
  3091. ),
  3092. array(
  3093. 'id' => 7,
  3094. 'parent_id' => 6
  3095. ),
  3096. array(
  3097. 'id' => 8,
  3098. 'parent_id' => 6
  3099. ),
  3100. array(
  3101. 'id' => 9,
  3102. 'parent_id' => 6
  3103. ),
  3104. array(
  3105. 'id' => 10,
  3106. 'parent_id' => 6
  3107. )
  3108. );
  3109. $expected = array(
  3110. array(
  3111. 'id' => 1,
  3112. 'parent_id' => null,
  3113. 'children' => array(
  3114. array(
  3115. 'id' => 2,
  3116. 'parent_id' => 1,
  3117. 'children' => array()
  3118. ),
  3119. array(
  3120. 'id' => 3,
  3121. 'parent_id' => 1,
  3122. 'children' => array()
  3123. ),
  3124. array(
  3125. 'id' => 4,
  3126. 'parent_id' => 1,
  3127. 'children' => array()
  3128. ),
  3129. array(
  3130. 'id' => 5,
  3131. 'parent_id' => 1,
  3132. 'children' => array()
  3133. ),
  3134. )
  3135. ),
  3136. array(
  3137. 'id' => 6,
  3138. 'parent_id' => null,
  3139. 'children' => array(
  3140. array(
  3141. 'id' => 7,
  3142. 'parent_id' => 6,
  3143. 'children' => array()
  3144. ),
  3145. array(
  3146. 'id' => 8,
  3147. 'parent_id' => 6,
  3148. 'children' => array()
  3149. ),
  3150. array(
  3151. 'id' => 9,
  3152. 'parent_id' => 6,
  3153. 'children' => array()
  3154. ),
  3155. array(
  3156. 'id' => 10,
  3157. 'parent_id' => 6,
  3158. 'children' => array()
  3159. )
  3160. )
  3161. )
  3162. );
  3163. $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
  3164. $this->assertEquals($expected, $result);
  3165. }
  3166. /**
  3167. * test Set nest with no specified parent data.
  3168. *
  3169. * The result should be the same as the input.
  3170. * For an easier comparison, unset all the empty children arrays from the result
  3171. *
  3172. * @return void
  3173. */
  3174. public function testMissingParent() {
  3175. $input = array(
  3176. array(
  3177. 'id' => 1,
  3178. ),
  3179. array(
  3180. 'id' => 2,
  3181. ),
  3182. array(
  3183. 'id' => 3,
  3184. ),
  3185. array(
  3186. 'id' => 4,
  3187. ),
  3188. array(
  3189. 'id' => 5,
  3190. ),
  3191. array(
  3192. 'id' => 6,
  3193. ),
  3194. array(
  3195. 'id' => 7,
  3196. ),
  3197. array(
  3198. 'id' => 8,
  3199. ),
  3200. array(
  3201. 'id' => 9,
  3202. ),
  3203. array(
  3204. 'id' => 10,
  3205. )
  3206. );
  3207. $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
  3208. foreach ($result as &$row) {
  3209. if (empty($row['children'])) {
  3210. unset($row['children']);
  3211. }
  3212. }
  3213. $this->assertEquals($input, $result);
  3214. }
  3215. }