SetTest.php 110 KB

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