SetTest.php 111 KB

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