| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563 |
- <?php
- /**
- * SetTest file
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since CakePHP(tm) v 1.2.0.4206
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Utility;
- use Cake\Model\Model;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Set;
- use Cake\Utility\Xml;
- /**
- * SetTest class
- *
- */
- class SetTest extends TestCase {
- /**
- * testNumericKeyExtraction method
- *
- * @return void
- */
- public function testNumericKeyExtraction() {
- $data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
- $this->assertEquals(array(1, 'whatever'), Set::extract($data, '{n}'));
- $this->assertEquals(array('plugin' => null, 'controller' => '', 'action' => ''), Set::diff($data, Set::extract($data, '{n}')));
- }
- /**
- * testEnum method
- *
- * @return void
- */
- public function testEnum() {
- $result = Set::enum(1, 'one, two');
- $this->assertEquals('two', $result);
- $result = Set::enum(2, 'one, two');
- $this->assertNull($result);
- $set = array('one', 'two');
- $result = Set::enum(0, $set);
- $this->assertEquals('one', $result);
- $result = Set::enum(1, $set);
- $this->assertEquals('two', $result);
- $result = Set::enum(1, array('one', 'two'));
- $this->assertEquals('two', $result);
- $result = Set::enum(2, array('one', 'two'));
- $this->assertNull($result);
- $result = Set::enum('first', array('first' => 'one', 'second' => 'two'));
- $this->assertEquals('one', $result);
- $result = Set::enum('third', array('first' => 'one', 'second' => 'two'));
- $this->assertNull($result);
- $result = Set::enum('no', array('no' => 0, 'yes' => 1));
- $this->assertEquals(0, $result);
- $result = Set::enum('not sure', array('no' => 0, 'yes' => 1));
- $this->assertNull($result);
- $result = Set::enum(0);
- $this->assertEquals('no', $result);
- $result = Set::enum(1);
- $this->assertEquals('yes', $result);
- $result = Set::enum(2);
- $this->assertNull($result);
- }
- /**
- * testFilter method
- *
- * @see Hash test cases, as Set::filter() is just a proxy.
- * @return void
- */
- public function testFilter() {
- $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
- $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
- $this->assertSame($expected, $result);
- }
- /**
- * testNumericArrayCheck method
- *
- * @see Hash test cases, as Set::numeric() is just a proxy.
- * @return void
- */
- public function testNumericArrayCheck() {
- $data = array('one');
- $this->assertTrue(Set::numeric(array_keys($data)));
- }
- /**
- * testKeyCheck method
- *
- * @return void
- */
- public function testKeyCheck() {
- $data = array('Multi' => array('dimensonal' => array('array')));
- $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
- $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
- $data = array(
- array(
- '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'),
- 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(
- 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'),
- 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'),
- ),
- 'Tag' => array(
- array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
- array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
- )
- ),
- array(
- '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'),
- 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(),
- 'Tag' => array()
- )
- );
- $this->assertTrue(Set::check($data, '0.Article.user_id'));
- $this->assertTrue(Set::check($data, '0.Comment.0.id'));
- $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
- $this->assertTrue(Set::check($data, '0.Article.user_id'));
- $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
- }
- /**
- * testMerge method
- *
- * @return void
- */
- public function testMerge() {
- $r = Set::merge(array('foo'));
- $this->assertEquals(array('foo'), $r);
- $r = Set::merge('foo');
- $this->assertEquals(array('foo'), $r);
- $r = Set::merge('foo', 'bar');
- $this->assertEquals(array('foo', 'bar'), $r);
- $r = Set::merge(array('foo'), array(), array('bar'));
- $this->assertEquals(array('foo', 'bar'), $r);
- $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');
- $this->assertEquals(array('foo', 'user' => 'bob', 'no-bar', 'bar'), $r);
- $a = array('foo', 'foo2');
- $b = array('bar', 'bar2');
- $this->assertEquals(array('foo', 'foo2', 'bar', 'bar2'), Set::merge($a, $b));
- $a = array('foo' => 'bar', 'bar' => 'foo');
- $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
- $this->assertEquals(array('foo' => 'no-bar', 'bar' => 'no-foo'), Set::merge($a, $b));
- $a = array('users' => array('bob', 'jim'));
- $b = array('users' => array('lisa', 'tina'));
- $this->assertEquals(array('users' => array('bob', 'jim', 'lisa', 'tina')), Set::merge($a, $b));
- $a = array('users' => array('jim', 'bob'));
- $b = array('users' => 'none');
- $this->assertEquals(array('users' => 'none'), Set::merge($a, $b));
- $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
- $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
- $this->assertEquals(array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream'), Set::merge($a, $b));
- $c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate');
- $expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate');
- $this->assertEquals($expected, Set::merge($a, $b, $c));
- $this->assertEquals(Set::merge($a, $b, array(), $c), $expected);
- $r = Set::merge($a, $b, $c);
- $this->assertEquals($expected, $r);
- $a = array('Tree', 'CounterCache',
- 'Upload' => array('folder' => 'products',
- 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));
- $b = array('Cacheable' => array('enabled' => false),
- 'Limit',
- 'Bindable',
- 'Validator',
- 'Transactional');
- $expected = array('Tree', 'CounterCache',
- 'Upload' => array('folder' => 'products',
- 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
- 'Cacheable' => array('enabled' => false),
- 'Limit',
- 'Bindable',
- 'Validator',
- 'Transactional');
- $this->assertEquals($expected, Set::merge($a, $b));
- $expected = array('Tree' => null, 'CounterCache' => null,
- 'Upload' => array('folder' => 'products',
- 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
- 'Cacheable' => array('enabled' => false),
- 'Limit' => null,
- 'Bindable' => null,
- 'Validator' => null,
- 'Transactional' => null);
- $this->assertEquals($expected, Set::normalize(Set::merge($a, $b)));
- }
- /**
- * testSort method
- *
- * @return void
- */
- public function testSort() {
- $result = Set::sort(array(), '{n}.name', 'asc');
- $this->assertEquals(array(), $result);
- $a = array(
- 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
- 1 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay')))
- );
- $b = array(
- 0 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay'))),
- 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
- );
- $a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
- $this->assertEquals($a, $b);
- $b = array(
- 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
- 1 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay')))
- );
- $a = array(
- 0 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay'))),
- 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
- );
- $a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
- $this->assertEquals($a, $b);
- $a = array(
- 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
- 1 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay'))),
- 2 => array('Person' => array('name' => 'Adam'), 'Friend' => array(array('name' => 'Bob')))
- );
- $b = array(
- 0 => array('Person' => array('name' => 'Adam'), 'Friend' => array(array('name' => 'Bob'))),
- 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
- 2 => array('Person' => array('name' => 'Tracy'), 'Friend' => array(array('name' => 'Lindsay')))
- );
- $a = Set::sort($a, '{n}.Person.name', 'asc');
- $this->assertEquals($a, $b);
- $a = array(
- array(7, 6, 4),
- array(3, 4, 5),
- array(3, 2, 1),
- );
- $b = array(
- array(3, 2, 1),
- array(3, 4, 5),
- array(7, 6, 4),
- );
- $a = Set::sort($a, '{n}.{n}', 'asc');
- $this->assertEquals($a, $b);
- $a = array(
- array(7, 6, 4),
- array(3, 4, 5),
- array(3, 2, array(1, 1, 1)),
- );
- $b = array(
- array(3, 2, array(1, 1, 1)),
- array(3, 4, 5),
- array(7, 6, 4),
- );
- $a = Set::sort($a, '{n}', 'asc');
- $this->assertEquals($a, $b);
- $a = array(
- 0 => array('Person' => array('name' => 'Jeff')),
- 1 => array('Shirt' => array('color' => 'black'))
- );
- $b = array(
- 0 => array('Shirt' => array('color' => 'black')),
- 1 => array('Person' => array('name' => 'Jeff')),
- );
- $a = Set::sort($a, '{n}.Person.name', 'ASC');
- $this->assertEquals($a, $b);
- $names = array(
- array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
- array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
- array('employees' => array(array('name' => array()))),
- array('employees' => array(array('name' => array())))
- );
- $result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
- $expected = array(
- array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
- array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
- array('employees' => array(array('name' => array()))),
- array('employees' => array(array('name' => array())))
- );
- $this->assertEquals($expected, $result);
- $menus = array(
- 'blogs' => array('title' => 'Blogs', 'weight' => 3),
- 'comments' => array('title' => 'Comments', 'weight' => 2),
- 'users' => array('title' => 'Users', 'weight' => 1),
- );
- $expected = array(
- 'users' => array('title' => 'Users', 'weight' => 1),
- 'comments' => array('title' => 'Comments', 'weight' => 2),
- 'blogs' => array('title' => 'Blogs', 'weight' => 3),
- );
- $result = Set::sort($menus, '{[a-z]+}.weight', 'ASC');
- $this->assertEquals($expected, $result);
- }
- /**
- * test sorting with string keys.
- *
- * @return void
- */
- public function testSortString() {
- $toSort = array(
- 'four' => array('number' => 4, 'some' => 'foursome'),
- 'six' => array('number' => 6, 'some' => 'sixsome'),
- 'five' => array('number' => 5, 'some' => 'fivesome'),
- 'two' => array('number' => 2, 'some' => 'twosome'),
- 'three' => array('number' => 3, 'some' => 'threesome')
- );
- $sorted = Set::sort($toSort, '{s}.number', 'asc');
- $expected = array(
- 'two' => array('number' => 2, 'some' => 'twosome'),
- 'three' => array('number' => 3, 'some' => 'threesome'),
- 'four' => array('number' => 4, 'some' => 'foursome'),
- 'five' => array('number' => 5, 'some' => 'fivesome'),
- 'six' => array('number' => 6, 'some' => 'sixsome')
- );
- $this->assertEquals($expected, $sorted);
- }
- /**
- * test sorting with out of order keys.
- *
- * @return void
- */
- public function testSortWithOutOfOrderKeys() {
- $data = array(
- 9 => array('class' => 510, 'test2' => 2),
- 1 => array('class' => 500, 'test2' => 1),
- 2 => array('class' => 600, 'test2' => 2),
- 5 => array('class' => 625, 'test2' => 4),
- 0 => array('class' => 605, 'test2' => 3),
- );
- $expected = array(
- array('class' => 500, 'test2' => 1),
- array('class' => 510, 'test2' => 2),
- array('class' => 600, 'test2' => 2),
- array('class' => 605, 'test2' => 3),
- array('class' => 625, 'test2' => 4),
- );
- $result = Set::sort($data, '{n}.class', 'asc');
- $this->assertEquals($expected, $result);
- $result = Set::sort($data, '{n}.test2', 'asc');
- $this->assertEquals($expected, $result);
- }
- /**
- * testExtract method
- *
- * @return void
- */
- public function testExtract() {
- $a = array(
- array(
- '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'),
- 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(
- 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'),
- 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'),
- ),
- 'Tag' => array(
- array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
- array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
- ),
- 'Deep' => array(
- 'Nesting' => array(
- 'test' => array(
- 1 => 'foo',
- 2 => array(
- 'and' => array('more' => 'stuff')
- )
- )
- )
- )
- ),
- array(
- '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'),
- 'User' => array('id' => '2', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(),
- 'Tag' => array()
- ),
- array(
- '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'),
- 'User' => array('id' => '3', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(),
- 'Tag' => array()
- ),
- array(
- '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'),
- 'User' => array('id' => '4', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(),
- 'Tag' => array()
- ),
- array(
- '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'),
- 'User' => array('id' => '5', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
- 'Comment' => array(),
- 'Tag' => array()
- )
- );
- $b = array('Deep' => $a[0]['Deep']);
- $c = array(
- array('a' => array('I' => array('a' => 1))),
- array(
- 'a' => array(
- 2
- )
- ),
- array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))),
- );
- $expected = array(array('a' => $c[2]['a']));
- $r = Set::extract('/a/II[a=3]/..', $c);
- $this->assertEquals($expected, $r);
- $expected = array(1, 2, 3, 4, 5);
- $this->assertEquals($expected, Set::extract('/User/id', $a));
- $expected = array(1, 2, 3, 4, 5);
- $this->assertEquals($expected, Set::extract('/User/id', $a));
- $expected = array(
- array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
- );
- $r = Set::extract('/User/id', $a, array('flatten' => false));
- $this->assertEquals($expected, $r);
- $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
- $this->assertEquals($expected, Set::extract('/Deep/Nesting/test', $a));
- $this->assertEquals($expected, Set::extract('/Deep/Nesting/test', $b));
- $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
- $r = Set::extract('/Deep/Nesting/test/1/..', $a);
- $this->assertEquals($expected, $r);
- $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
- $r = Set::extract('/Deep/Nesting/test/2/and/../..', $a);
- $this->assertEquals($expected, $r);
- $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
- $r = Set::extract('/Deep/Nesting/test/2/../../../Nesting/test/2/..', $a);
- $this->assertEquals($expected, $r);
- $expected = array(2);
- $r = Set::extract('/User[2]/id', $a);
- $this->assertEquals($expected, $r);
- $expected = array(4, 5);
- $r = Set::extract('/User[id>3]/id', $a);
- $this->assertEquals($expected, $r);
- $expected = array(2, 3);
- $r = Set::extract('/User[id>1][id<=3]/id', $a);
- $this->assertEquals($expected, $r);
- $expected = array(array('I'), array('II'));
- $r = Set::extract('/a/@*', $c);
- $this->assertEquals($expected, $r);
- $single = array(
- 'User' => array(
- 'id' => 4,
- 'name' => 'Neo',
- )
- );
- $tricky = array(
- 0 => array(
- 'User' => array(
- 'id' => 1,
- 'name' => 'John',
- )
- ),
- 1 => array(
- 'User' => array(
- 'id' => 2,
- 'name' => 'Bob',
- )
- ),
- 2 => array(
- 'User' => array(
- 'id' => 3,
- 'name' => 'Tony',
- )
- ),
- 'User' => array(
- 'id' => 4,
- 'name' => 'Neo',
- )
- );
- $expected = array(1, 2, 3, 4);
- $r = Set::extract('/User/id', $tricky);
- $this->assertEquals($expected, $r);
- $expected = array(4);
- $r = Set::extract('/User/id', $single);
- $this->assertEquals($expected, $r);
- $expected = array(1, 3);
- $r = Set::extract('/User[name=/n/]/id', $tricky);
- $this->assertEquals($expected, $r);
- $expected = array(4);
- $r = Set::extract('/User[name=/N/]/id', $tricky);
- $this->assertEquals($expected, $r);
- $expected = array(1, 3, 4);
- $r = Set::extract('/User[name=/N/i]/id', $tricky);
- $this->assertEquals($expected, $r);
- $expected = array(array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name'));
- $r = Set::extract('/User/@*', $tricky);
- $this->assertEquals($expected, $r);
- $common = array(
- array(
- 'Article' => array(
- 'id' => 1,
- 'name' => 'Article 1',
- ),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'user_id' => 5,
- 'article_id' => 1,
- 'text' => 'Comment 1',
- ),
- array(
- 'id' => 2,
- 'user_id' => 23,
- 'article_id' => 1,
- 'text' => 'Comment 2',
- ),
- array(
- 'id' => 3,
- 'user_id' => 17,
- 'article_id' => 1,
- 'text' => 'Comment 3',
- ),
- ),
- ),
- array(
- 'Article' => array(
- 'id' => 2,
- 'name' => 'Article 2',
- ),
- 'Comment' => array(
- array(
- 'id' => 4,
- 'user_id' => 2,
- 'article_id' => 2,
- 'text' => 'Comment 4',
- 'addition' => '',
- ),
- array(
- 'id' => 5,
- 'user_id' => 23,
- 'article_id' => 2,
- 'text' => 'Comment 5',
- 'addition' => 'foo',
- ),
- ),
- ),
- array(
- 'Article' => array(
- 'id' => 3,
- 'name' => 'Article 3',
- ),
- 'Comment' => array(),
- )
- );
- $r = Set::extract('/Comment/id', $common);
- $expected = array(1, 2, 3, 4, 5);
- $this->assertEquals($expected, $r);
- $expected = array(1, 2, 4, 5);
- $r = Set::extract('/Comment[id!=3]/id', $common);
- $this->assertEquals($expected, $r);
- $r = Set::extract('/', $common);
- $this->assertEquals($r, $common);
- $expected = array(1, 2, 4, 5);
- $r = Set::extract($common, '/Comment[id!=3]/id');
- $this->assertEquals($expected, $r);
- $expected = array($common[0]['Comment'][2]);
- $r = Set::extract($common, '/Comment/2');
- $this->assertEquals($expected, $r);
- $expected = array($common[0]['Comment'][0]);
- $r = Set::extract($common, '/Comment[1]/.[id=1]');
- $this->assertEquals($expected, $r);
- $expected = array($common[1]['Comment'][1]);
- $r = Set::extract($common, '/1/Comment/.[2]');
- $this->assertEquals($expected, $r);
- $expected = array();
- $r = Set::extract('/User/id', array());
- $this->assertEquals($expected, $r);
- $expected = array(5);
- $r = Set::extract('/Comment/id[:last]', $common);
- $this->assertEquals($expected, $r);
- $expected = array(1);
- $r = Set::extract('/Comment/id[:first]', $common);
- $this->assertEquals($expected, $r);
- $expected = array(3);
- $r = Set::extract('/Article[:last]/id', $common);
- $this->assertEquals($expected, $r);
- $expected = array(array('Comment' => $common[1]['Comment'][0]));
- $r = Set::extract('/Comment[addition=]', $common);
- $this->assertEquals($expected, $r);
- $habtm = array(
- array(
- 'Post' => array(
- 'id' => 1,
- 'title' => 'great post',
- ),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'text' => 'foo',
- 'User' => array(
- 'id' => 1,
- 'name' => 'bob'
- ),
- ),
- array(
- 'id' => 2,
- 'text' => 'bar',
- 'User' => array(
- 'id' => 2,
- 'name' => 'tod'
- ),
- ),
- ),
- ),
- array(
- 'Post' => array(
- 'id' => 2,
- 'title' => 'fun post',
- ),
- 'Comment' => array(
- array(
- 'id' => 3,
- 'text' => '123',
- 'User' => array(
- 'id' => 3,
- 'name' => 'dan'
- ),
- ),
- array(
- 'id' => 4,
- 'text' => '987',
- 'User' => array(
- 'id' => 4,
- 'name' => 'jim'
- ),
- ),
- ),
- ),
- );
- $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[1]['Comment']['User']['name']);
- $this->assertEquals(2, count($r));
- $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
- $this->assertEquals(2, count($r));
- $tree = array(
- array(
- 'Category' => array('name' => 'Category 1'),
- 'children' => array(array('Category' => array('name' => 'Category 1.1')))
- ),
- array(
- 'Category' => array('name' => 'Category 2'),
- 'children' => array(
- array('Category' => array('name' => 'Category 2.1')),
- array('Category' => array('name' => 'Category 2.2'))
- )
- ),
- array(
- 'Category' => array('name' => 'Category 3'),
- 'children' => array(array('Category' => array('name' => 'Category 3.1')))
- )
- );
- $expected = array(array('Category' => $tree[1]['Category']));
- $r = Set::extract('/Category[name=Category 2]', $tree);
- $this->assertEquals($expected, $r);
- $expected = array(
- array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children'])
- );
- $r = Set::extract('/Category[name=Category 2]/..', $tree);
- $this->assertEquals($expected, $r);
- $expected = array(
- array('children' => $tree[1]['children'][0]),
- array('children' => $tree[1]['children'][1])
- );
- $r = Set::extract('/Category[name=Category 2]/../children', $tree);
- $this->assertEquals($expected, $r);
- $habtm = array(
- array(
- 'Post' => array(
- 'id' => 1,
- 'title' => 'great post',
- ),
- 'Comment' => array(
- array(
- 'id' => 1,
- 'text' => 'foo',
- 'User' => array(
- 'id' => 1,
- 'name' => 'bob'
- ),
- ),
- array(
- 'id' => 2,
- 'text' => 'bar',
- 'User' => array(
- 'id' => 2,
- 'name' => 'tod'
- ),
- ),
- ),
- ),
- array(
- 'Post' => array(
- 'id' => 2,
- 'title' => 'fun post',
- ),
- 'Comment' => array(
- array(
- 'id' => 3,
- 'text' => '123',
- 'User' => array(
- 'id' => 3,
- 'name' => 'dan'
- ),
- ),
- array(
- 'id' => 4,
- 'text' => '987',
- 'User' => array(
- 'id' => 4,
- 'name' => 'jim'
- ),
- ),
- ),
- ),
- );
- $r = Set::extract('/Comment/User[name=/\w+/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[2]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[3]['Comment']['User']['name']);
- $this->assertEquals(4, count($r));
- $r = Set::extract('/Comment/User[name=/[a-z]+/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[2]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[3]['Comment']['User']['name']);
- $this->assertEquals(4, count($r));
- $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('dan', $r[1]['Comment']['User']['name']);
- $this->assertEquals(2, count($r));
- $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
- $this->assertEquals('bob', $r[0]['Comment']['User']['name']);
- $this->assertEquals('tod', $r[1]['Comment']['User']['name']);
- $this->assertEquals(2, count($r));
- $mixedKeys = array(
- 'User' => array(
- 0 => array(
- 'id' => 4,
- 'name' => 'Neo'
- ),
- 1 => array(
- 'id' => 5,
- 'name' => 'Morpheus'
- ),
- 'stringKey' => array()
- )
- );
- $expected = array('Neo', 'Morpheus');
- $r = Set::extract('/User/name', $mixedKeys);
- $this->assertEquals($expected, $r);
- $f = array(
- array(
- 'file' => array(
- 'name' => 'zipfile.zip',
- 'type' => 'application/zip',
- 'tmp_name' => '/tmp/php178.tmp',
- 'error' => 0,
- 'size' => '564647'
- )
- ),
- array(
- 'file' => array(
- 'name' => 'zipfile2.zip',
- 'type' => 'application/x-zip-compressed',
- 'tmp_name' => '/tmp/php179.tmp',
- 'error' => 0,
- 'size' => '354784'
- )
- ),
- array(
- 'file' => array(
- 'name' => 'picture.jpg',
- 'type' => 'image/jpeg',
- 'tmp_name' => '/tmp/php180.tmp',
- 'error' => 0,
- 'size' => '21324'
- )
- )
- );
- $expected = array(array('name' => 'zipfile2.zip', 'type' => 'application/x-zip-compressed', 'tmp_name' => '/tmp/php179.tmp', 'error' => 0, 'size' => '354784'));
- $r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
- $this->assertEquals($expected, $r);
- $expected = array(array('name' => 'zipfile.zip', 'type' => 'application/zip', 'tmp_name' => '/tmp/php178.tmp', 'error' => 0, 'size' => '564647'));
- $r = Set::extract('/file/.[type=application/zip]', $f);
- $this->assertEquals($expected, $r);
- $f = array(
- array(
- 'file' => array(
- 'name' => 'zipfile.zip',
- 'type' => 'application/zip',
- 'tmp_name' => '/tmp/php178.tmp',
- 'error' => 0,
- 'size' => '564647'
- )
- ),
- array(
- 'file' => array(
- 'name' => 'zipfile2.zip',
- 'type' => 'application/x zip compressed',
- 'tmp_name' => '/tmp/php179.tmp',
- 'error' => 0,
- 'size' => '354784'
- )
- ),
- array(
- 'file' => array(
- 'name' => 'picture.jpg',
- 'type' => 'image/jpeg',
- 'tmp_name' => '/tmp/php180.tmp',
- 'error' => 0,
- 'size' => '21324'
- )
- )
- );
- $expected = array(array('name' => 'zipfile2.zip', 'type' => 'application/x zip compressed', 'tmp_name' => '/tmp/php179.tmp', 'error' => 0, 'size' => '354784'));
- $r = Set::extract('/file/.[type=application/x zip compressed]', $f);
- $this->assertEquals($expected, $r);
- $expected = array(
- array('name' => 'zipfile.zip', 'type' => 'application/zip', 'tmp_name' => '/tmp/php178.tmp', 'error' => 0, 'size' => '564647'),
- array('name' => 'zipfile2.zip', 'type' => 'application/x zip compressed', 'tmp_name' => '/tmp/php179.tmp', 'error' => 0, 'size' => '354784')
- );
- $r = Set::extract('/file/.[tmp_name=/tmp\/php17/]', $f);
- $this->assertEquals($expected, $r);
- $hasMany = array(
- 'Node' => array(
- 'id' => 1,
- 'name' => 'First',
- 'state' => 50
- ),
- 'ParentNode' => array(
- 0 => array(
- 'id' => 2,
- 'name' => 'Second',
- 'state' => 60,
- )
- )
- );
- $result = Set::extract('/ParentNode/name', $hasMany);
- $expected = array('Second');
- $this->assertEquals($expected, $result);
- $data = array(
- array(
- 'Category' => array(
- 'id' => 1,
- 'name' => 'First'
- ),
- 0 => array(
- 'value' => 50
- )
- ),
- array(
- 'Category' => array(
- 'id' => 2,
- 'name' => 'Second'
- ),
- 0 => array(
- 'value' => 60
- )
- )
- );
- $expected = array(
- array(
- 'Category' => array(
- 'id' => 1,
- 'name' => 'First'
- ),
- 0 => array(
- 'value' => 50
- )
- )
- );
- $result = Set::extract('/Category[id=1]/..', $data);
- $this->assertEquals($expected, $result);
- $data = array(
- array(
- 'ChildNode' => array('id' => 1),
- array('name' => 'Item 1')
- ),
- array(
- 'ChildNode' => array('id' => 2),
- array('name' => 'Item 2')
- ),
- );
- $expected = array(
- 'Item 1',
- 'Item 2'
- );
- $result = Set::extract('/0/name', $data);
- $this->assertEquals($expected, $result);
- $data = array(
- array('A1', 'B1'),
- array('A2', 'B2')
- );
- $expected = array('A1', 'A2');
- $result = Set::extract('/0', $data);
- $this->assertEquals($expected, $result);
- }
- /**
- * test parent selectors with extract
- *
- * @return void
- */
- public function testExtractParentSelector() {
- $tree = array(
- array(
- 'Category' => array(
- 'name' => 'Category 1'
- ),
- 'children' => array(
- array(
- 'Category' => array(
- 'name' => 'Category 1.1'
- )
- )
- )
- ),
- array(
- 'Category' => array(
- 'name' => 'Category 2'
- ),
- 'children' => array(
- array(
- 'Category' => array(
- 'name' => 'Category 2.1'
- )
- ),
- array(
- 'Category' => array(
- 'name' => 'Category 2.2'
- )
- ),
- )
- ),
- array(
- 'Category' => array(
- 'name' => 'Category 3'
- ),
- 'children' => array(
- array(
- 'Category' => array(
- 'name' => 'Category 3.1'
- )
- )
- )
- )
- );
- $expected = array(array('Category' => $tree[1]['Category']));
- $r = Set::extract('/Category[name=Category 2]', $tree);
- $this->assertEquals($expected, $r);
- $expected = array(array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children']));
- $r = Set::extract('/Category[name=Category 2]/..', $tree);
- $this->assertEquals($expected, $r);
- $expected = array(array('children' => $tree[1]['children'][0]), array('children' => $tree[1]['children'][1]));
- $r = Set::extract('/Category[name=Category 2]/../children', $tree);
- $this->assertEquals($expected, $r);
- $single = array(
- array(
- 'CallType' => array(
- 'name' => 'Internal Voice'
- ),
- 'x' => array(
- 'hour' => 7
- )
- )
- );
- $expected = array(7);
- $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
- $this->assertEquals($expected, $r);
- $multiple = array(
- array(
- 'CallType' => array(
- 'name' => 'Internal Voice'
- ),
- 'x' => array(
- 'hour' => 7
- )
- ),
- array(
- 'CallType' => array(
- 'name' => 'Internal Voice'
- ),
- 'x' => array(
- 'hour' => 2
- )
- ),
- array(
- 'CallType' => array(
- 'name' => 'Internal Voice'
- ),
- 'x' => array(
- 'hour' => 1
- )
- )
- );
- $expected = array(7, 2, 1);
- $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
- $this->assertEquals($expected, $r);
- $a = array(
- 'Model' => array(
- '0' => array(
- 'id' => 18,
- 'SubModelsModel' => array(
- 'id' => 1,
- 'submodel_id' => 66,
- 'model_id' => 18,
- 'type' => 1
- ),
- ),
- '1' => array(
- 'id' => 0,
- 'SubModelsModel' => array(
- 'id' => 2,
- 'submodel_id' => 66,
- 'model_id' => 0,
- 'type' => 1
- ),
- ),
- '2' => array(
- 'id' => 17,
- 'SubModelsModel' => array(
- 'id' => 3,
- 'submodel_id' => 66,
- 'model_id' => 17,
- 'type' => 2
- ),
- ),
- '3' => array(
- 'id' => 0,
- 'SubModelsModel' => array(
- 'id' => 4,
- 'submodel_id' => 66,
- 'model_id' => 0,
- 'type' => 2
- )
- )
- )
- );
- $expected = array(
- array(
- 'Model' => array(
- 'id' => 17,
- 'SubModelsModel' => array(
- 'id' => 3,
- 'submodel_id' => 66,
- 'model_id' => 17,
- 'type' => 2
- ),
- )
- ),
- array(
- 'Model' => array(
- 'id' => 0,
- 'SubModelsModel' => array(
- 'id' => 4,
- 'submodel_id' => 66,
- 'model_id' => 0,
- 'type' => 2
- )
- )
- )
- );
- $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
- $this->assertEquals($expected, $r);
- }
- /**
- * test that extract() still works when arrays don't contain a 0 index.
- *
- * @return void
- */
- public function testExtractWithNonZeroArrays() {
- $nonZero = array(
- 1 => array(
- 'User' => array(
- 'id' => 1,
- 'name' => 'John',
- )
- ),
- 2 => array(
- 'User' => array(
- 'id' => 2,
- 'name' => 'Bob',
- )
- ),
- 3 => array(
- 'User' => array(
- 'id' => 3,
- 'name' => 'Tony',
- )
- )
- );
- $expected = array(1, 2, 3);
- $r = Set::extract('/User/id', $nonZero);
- $this->assertEquals($expected, $r);
- $expected = array(
- array('User' => array('id' => 1, 'name' => 'John')),
- array('User' => array('id' => 2, 'name' => 'Bob')),
- array('User' => array('id' => 3, 'name' => 'Tony')),
- );
- $result = Set::extract('/User', $nonZero);
- $this->assertEquals($expected, $result);
- $nonSequential = array(
- 'User' => array(
- 0 => array('id' => 1),
- 2 => array('id' => 2),
- 6 => array('id' => 3),
- 9 => array('id' => 4),
- 3 => array('id' => 5),
- ),
- );
- $nonZero = array(
- 'User' => array(
- 2 => array('id' => 1),
- 4 => array('id' => 2),
- 6 => array('id' => 3),
- 9 => array('id' => 4),
- 3 => array('id' => 5),
- ),
- );
- $expected = array(1, 2, 3, 4, 5);
- $this->assertEquals($expected, Set::extract('/User/id', $nonSequential));
- $result = Set::extract('/User/id', $nonZero);
- $this->assertEquals($expected, $result, 'Failed non zero array key extract');
- $expected = array(1, 2, 3, 4, 5);
- $this->assertEquals($expected, Set::extract('/User/id', $nonSequential));
- $result = Set::extract('/User/id', $nonZero);
- $this->assertEquals($expected, $result, 'Failed non zero array key extract');
- $startingAtOne = array(
- 'Article' => array(
- 1 => array(
- 'id' => 1,
- 'approved' => 1,
- ),
- )
- );
- $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1)));
- $result = Set::extract('/Article[approved=1]', $startingAtOne);
- $this->assertEquals($expected, $result);
- $items = array(
- 240 => array(
- 'A' => array(
- 'field1' => 'a240',
- 'field2' => 'a240',
- ),
- 'B' => array(
- 'field1' => 'b240',
- 'field2' => 'b240'
- ),
- )
- );
- $expected = array(
- 0 => 'b240'
- );
- $result = Set::extract('/B/field1', $items);
- $this->assertSame($expected, $result);
- $this->assertSame($result, Set::extract('{n}.B.field1', $items));
- }
- /**
- * testExtractWithArrays method
- *
- * @return void
- */
- public function testExtractWithArrays() {
- $data = array(
- 'Level1' => array(
- 'Level2' => array('test1', 'test2'),
- 'Level2bis' => array('test3', 'test4')
- )
- );
- $this->assertEquals(array(array('Level2' => array('test1', 'test2'))), Set::extract('/Level1/Level2', $data));
- $this->assertEquals(array(array('Level2bis' => array('test3', 'test4'))), Set::extract('/Level1/Level2bis', $data));
- }
- /**
- * test extract() with elements that have non-array children.
- *
- * @return void
- */
- public function testExtractWithNonArrayElements() {
- $data = array(
- 'node' => array(
- array('foo'),
- 'bar'
- )
- );
- $result = Set::extract('/node', $data);
- $expected = array(
- array('node' => array('foo')),
- 'bar'
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'node' => array(
- 'foo' => array('bar'),
- 'bar' => array('foo')
- )
- );
- $result = Set::extract('/node', $data);
- $expected = array(
- array('foo' => array('bar')),
- array('bar' => array('foo')),
- );
- $this->assertEquals($expected, $result);
- $data = array(
- 'node' => array(
- 'foo' => array(
- 'bar'
- ),
- 'bar' => 'foo'
- )
- );
- $result = Set::extract('/node', $data);
- $expected = array(
- array('foo' => array('bar')),
- 'foo'
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Test that extract() + matching can hit null things.
- */
- public function testExtractMatchesNull() {
- $data = array(
- 'Country' => array(
- array('name' => 'Canada'),
- array('name' => 'Australia'),
- array('name' => null),
- )
- );
- $result = Set::extract('/Country[name=/Canada|^$/]', $data);
- $expected = array(
- array(
- 'Country' => array(
- 'name' => 'Canada',
- ),
- ),
- array(
- 'Country' => array(
- 'name' => null,
- ),
- ),
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testMatches method
- *
- * @return void
- */
- public function testMatches() {
- $a = array(
- array('Article' => array('id' => 1, 'title' => 'Article 1')),
- array('Article' => array('id' => 2, 'title' => 'Article 2')),
- array('Article' => array('id' => 3, 'title' => 'Article 3'))
- );
- $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
- $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
- $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
- $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('3'), null, 3));
- $this->assertTrue(Set::matches(array('5'), null, 5));
- $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
- $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
- $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
- $this->assertTrue(Set::matches('/Article[id=2]', $a));
- $this->assertFalse(Set::matches('/Article[id=4]', $a));
- $this->assertTrue(Set::matches(array(), $a));
- $r = array(
- 'Attachment' => array(
- 'keep' => array()
- ),
- 'Comment' => array(
- 'keep' => array(
- 'Attachment' => array(
- 'fields' => array(
- 0 => 'attachment',
- ),
- ),
- )
- ),
- 'User' => array(
- 'keep' => array()
- ),
- 'Article' => array(
- 'keep' => array(
- 'Comment' => array(
- 'fields' => array(
- 0 => 'comment',
- 1 => 'published',
- ),
- ),
- 'User' => array(
- 'fields' => array(
- 0 => 'user',
- ),
- ),
- )
- )
- );
- $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
- $this->assertEquals(array('comment', 'published'), Set::extract('/Article/keep/Comment/fields', $r));
- $this->assertEquals(array('user'), Set::extract('/Article/keep/User/fields', $r));
- }
- /**
- * testSetExtractReturnsEmptyArray method
- *
- * @return void
- */
- public function testSetExtractReturnsEmptyArray() {
- $this->assertEquals(Set::extract(array(), '/Post/id'), array());
- $this->assertEquals(Set::extract('/Post/id', array()), array());
- $this->assertEquals(Set::extract('/Post/id', array(
- array('Post' => array('name' => 'bob')),
- array('Post' => array('name' => 'jim'))
- )), array());
- $this->assertEquals(Set::extract(array(), 'Message.flash'), null);
- }
- /**
- * testClassicExtract method
- *
- * @return void
- */
- public function testClassicExtract() {
- $a = array(
- array('Article' => array('id' => 1, 'title' => 'Article 1')),
- array('Article' => array('id' => 2, 'title' => 'Article 2')),
- array('Article' => array('id' => 3, 'title' => 'Article 3'))
- );
- $result = Set::extract($a, '{n}.Article.id');
- $expected = array(1, 2, 3);
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{n}.Article.title');
- $expected = array('Article 1', 'Article 2', 'Article 3');
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '1.Article.title');
- $expected = 'Article 2';
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '3.Article.title');
- $expected = null;
- $this->assertEquals($expected, $result);
- $a = array(
- array(
- 'Article' => array('id' => 1, 'title' => 'Article 1',
- 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
- ),
- array(
- 'Article' => array('id' => 2, 'title' => 'Article 2',
- 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
- ),
- array(
- 'Article' => array('id' => 3, 'title' => 'Article 3',
- 'User' => array('id' => 2, 'username' => 'phpnut'))
- )
- );
- $result = Set::extract($a, '{n}.Article.User.username');
- $expected = array('mariano.iglesias', 'mariano.iglesias', 'phpnut');
- $this->assertEquals($expected, $result);
- $a = array(
- array(
- 'Article' => array(
- 'id' => 1, 'title' => 'Article 1',
- 'Comment' => array(
- array('id' => 10, 'title' => 'Comment 10'),
- array('id' => 11, 'title' => 'Comment 11'),
- array('id' => 12, 'title' => 'Comment 12')
- )
- )
- ),
- array(
- 'Article' => array(
- 'id' => 2, 'title' => 'Article 2',
- 'Comment' => array(
- array('id' => 13, 'title' => 'Comment 13'),
- array('id' => 14, 'title' => 'Comment 14')
- )
- )
- ),
- array('Article' => array('id' => 3, 'title' => 'Article 3'))
- );
- $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
- $expected = array(array(10, 11, 12), array(13, 14), null);
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
- $expected = array(
- array('Comment 10', 'Comment 11', 'Comment 12'),
- array('Comment 13', 'Comment 14'),
- null
- );
- $this->assertEquals($expected, $result);
- $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
- $result = Set::extract($a, '{n}.1day');
- $expected = array('20 sales', '2 sales');
- $this->assertEquals($expected, $result);
- $a = array(
- 'pages' => array('name' => 'page'),
- 'fruites' => array('name' => 'fruit'),
- 0 => array('name' => 'zero')
- );
- $result = Set::extract($a, '{s}.name');
- $expected = array('page', 'fruit');
- $this->assertEquals($expected, $result);
- $a = array(
- 0 => array('pages' => array('name' => 'page')),
- 1 => array('fruites' => array('name' => 'fruit')),
- 'test' => array(array('name' => 'jippi')),
- 'dot.test' => array(array('name' => 'jippi'))
- );
- $result = Set::extract($a, '{n}.{s}.name');
- $expected = array(0 => array('page'), 1 => array('fruit'));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{s}.{n}.name');
- $expected = array(array('jippi'), array('jippi'));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{\w+}.{\w+}.name');
- $expected = array(
- array('pages' => 'page'),
- array('fruites' => 'fruit'),
- 'test' => array('jippi'),
- 'dot.test' => array('jippi')
- );
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{\d+}.{\w+}.name');
- $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{n}.{\w+}.name');
- $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{s}.{\d+}.name');
- $expected = array(array('jippi'), array('jippi'));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{s}');
- $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{[a-z]}');
- $expected = array(
- 'test' => array(array('name' => 'jippi')),
- 'dot.test' => array(array('name' => 'jippi'))
- );
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, '{dot\.test}.{n}');
- $expected = array('dot.test' => array(array('name' => 'jippi')));
- $this->assertEquals($expected, $result);
- $a = new \stdClass();
- $a->articles = array(
- array('Article' => array('id' => 1, 'title' => 'Article 1')),
- array('Article' => array('id' => 2, 'title' => 'Article 2')),
- array('Article' => array('id' => 3, 'title' => 'Article 3'))
- );
- $result = Set::extract($a, 'articles.{n}.Article.id');
- $expected = array(1, 2, 3);
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, 'articles.{n}.Article.title');
- $expected = array('Article 1', 'Article 2', 'Article 3');
- $this->assertEquals($expected, $result);
- $a = new \ArrayObject();
- $a['articles'] = array(
- array('Article' => array('id' => 1, 'title' => 'Article 1')),
- array('Article' => array('id' => 2, 'title' => 'Article 2')),
- array('Article' => array('id' => 3, 'title' => 'Article 3'))
- );
- $result = Set::extract($a, 'articles.{n}.Article.id');
- $expected = array(1, 2, 3);
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, 'articles.{n}.Article.title');
- $expected = array('Article 1', 'Article 2', 'Article 3');
- $this->assertEquals($expected, $result);
- $result = Set::extract($a, 'articles.0.Article.title');
- $expected = 'Article 1';
- $this->assertEquals($expected, $result);
- }
- /**
- * test classicExtract with keys that exceed 32bit max int.
- *
- * @return void
- */
- public function testClassicExtractMaxInt() {
- $data = array(
- 'Data' => array(
- '13376924712' => 'abc'
- )
- );
- $this->assertEquals('abc', Set::classicExtract($data, 'Data.13376924712'));
- }
- /**
- * testInsert method
- *
- * @see Hash tests, as Set::insert() is just a proxy.
- * @return void
- */
- public function testInsert() {
- $a = array(
- 'pages' => array('name' => 'page')
- );
- $result = Set::insert($a, 'files', array('name' => 'files'));
- $expected = array(
- 'pages' => array('name' => 'page'),
- 'files' => array('name' => 'files')
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testRemove method
- *
- * @return void
- */
- public function testRemove() {
- $a = array(
- 'pages' => array('name' => 'page'),
- 'files' => array('name' => 'files')
- );
- $result = Set::remove($a, 'files');
- $expected = array(
- 'pages' => array('name' => 'page')
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * testCheck method
- *
- * @return void
- */
- public function testCheck() {
- $set = array(
- 'My Index 1' => array('First' => 'The first item')
- );
- $this->assertTrue(Set::check($set, 'My Index 1.First'));
- $this->assertTrue(Set::check($set, 'My Index 1'));
- $this->assertEquals(Set::check($set, array()), $set);
- $set = array(
- 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
- );
- $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
- $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
- $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
- $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
- }
- /**
- * testWritingWithFunkyKeys method
- *
- * @return void
- */
- public function testWritingWithFunkyKeys() {
- $set = Set::insert(array(), 'Session Test', "test");
- $this->assertEquals('test', Set::extract($set, 'Session Test'));
- $set = Set::remove($set, 'Session Test');
- $this->assertFalse(Set::check($set, 'Session Test'));
- $expected = array('Session Test' => array('Test Case' => 'test'));
- $this->assertEquals(Set::insert(array(), 'Session Test.Test Case', "test"), $expected);
- $this->assertTrue(Set::check($expected, 'Session Test.Test Case'));
- }
- /**
- * testDiff method
- *
- * @return void
- */
- public function testDiff() {
- $a = array(
- 0 => array('name' => 'main'),
- 1 => array('name' => 'about')
- );
- $b = array(
- 0 => array('name' => 'main'),
- 1 => array('name' => 'about'),
- 2 => array('name' => 'contact')
- );
- $result = Set::diff($a, $b);
- $expected = array(
- 2 => array('name' => 'contact')
- );
- $this->assertEquals($expected, $result);
- $result = Set::diff($a, array());
- $expected = $a;
- $this->assertEquals($expected, $result);
- $result = Set::diff(array(), $b);
- $expected = $b;
- $this->assertEquals($expected, $result);
- $b = array(
- 0 => array('name' => 'me'),
- 1 => array('name' => 'about')
- );
- $result = Set::diff($a, $b);
- $expected = array(
- 0 => array('name' => 'main')
- );
- $this->assertEquals($expected, $result);
- $a = array();
- $b = array('name' => 'bob', 'address' => 'home');
- $result = Set::diff($a, $b);
- $this->assertEquals($b, $result);
- $a = array('name' => 'bob', 'address' => 'home');
- $b = array();
- $result = Set::diff($a, $b);
- $this->assertEquals($a, $result);
- $a = array('key' => true, 'another' => false, 'name' => 'me');
- $b = array('key' => 1, 'another' => 0);
- $expected = array('name' => 'me');
- $result = Set::diff($a, $b);
- $this->assertEquals($expected, $result);
- $a = array('key' => 'value', 'another' => null, 'name' => 'me');
- $b = array('key' => 'differentValue', 'another' => null);
- $expected = array('key' => 'value', 'name' => 'me');
- $result = Set::diff($a, $b);
- $this->assertEquals($expected, $result);
- $a = array('key' => 'value', 'another' => null, 'name' => 'me');
- $b = array('key' => 'differentValue', 'another' => 'value');
- $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
- $result = Set::diff($a, $b);
- $this->assertEquals($expected, $result);
- $a = array('key' => 'value', 'another' => null, 'name' => 'me');
- $b = array('key' => 'differentValue', 'another' => 'value');
- $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
- $result = Set::diff($b, $a);
- $this->assertEquals($expected, $result);
- $a = array('key' => 'value', 'another' => null, 'name' => 'me');
- $b = array(0 => 'differentValue', 1 => 'value');
- $expected = $a + $b;
- $result = Set::diff($a, $b);
- $this->assertEquals($expected, $result);
- }
- /**
- * testContains method
- *
- * @return void
- */
- public function testContains() {
- $a = array(
- 0 => array('name' => 'main'),
- 1 => array('name' => 'about')
- );
- $b = array(
- 0 => array('name' => 'main'),
- 1 => array('name' => 'about'),
- 2 => array('name' => 'contact'),
- 'a' => 'b'
- );
- $this->assertTrue(Set::contains($a, $a));
- $this->assertFalse(Set::contains($a, $b));
- $this->assertTrue(Set::contains($b, $a));
- }
- /**
- * testCombine method
- *
- * @return void
- */
- public function testCombine() {
- $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
- $this->assertTrue(empty($result));
- $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
- $this->assertTrue(empty($result));
- $a = array(
- array('User' => array('id' => 2, 'group_id' => 1,
- 'Data' => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'))),
- array('User' => array('id' => 14, 'group_id' => 2,
- 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
- array('User' => array('id' => 25, 'group_id' => 1,
- 'Data' => array('user' => 'gwoo', 'name' => 'The Gwoo'))));
- $result = Set::combine($a, '{n}.User.id');
- $expected = array(2 => null, 14 => null, 25 => null);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
- $expected = array(2 => null, 14 => null, 25 => null);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
- $expected = array(
- 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
- 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
- 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
- $expected = array(
- 2 => 'Mariano Iglesias',
- 14 => 'Larry E. Masters',
- 25 => 'The Gwoo');
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
- $expected = array(
- 1 => array(
- 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
- 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
- 2 => array(
- 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
- $expected = array(
- 1 => array(
- 2 => 'Mariano Iglesias',
- 25 => 'The Gwoo'),
- 2 => array(
- 14 => 'Larry E. Masters'));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id');
- $expected = array(2 => null, 14 => null, 25 => null);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
- $expected = array(
- 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
- 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
- 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
- $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
- $expected = array(
- 1 => array(
- 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
- 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
- 2 => array(
- 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
- $expected = array(
- 1 => array(
- 2 => 'Mariano Iglesias',
- 25 => 'The Gwoo'),
- 2 => array(
- 14 => 'Larry E. Masters'));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
- $expected = array(
- 1 => array(
- 2 => 'mariano.iglesias: Mariano Iglesias',
- 25 => 'gwoo: The Gwoo'),
- 2 => array(14 => 'phpnut: Larry E. Masters'));
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
- $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
- $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
- $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
- $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
- $this->assertEquals($expected, $result);
- $b = new \stdClass();
- $b->users = array(
- array('User' => array('id' => 2, 'group_id' => 1,
- 'Data' => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'))),
- array('User' => array('id' => 14, 'group_id' => 2,
- 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
- array('User' => array('id' => 25, 'group_id' => 1,
- 'Data' => array('user' => 'gwoo', 'name' => 'The Gwoo'))));
- $result = Set::combine($b, 'users.{n}.User.id');
- $expected = array(2 => null, 14 => null, 25 => null);
- $this->assertEquals($expected, $result);
- $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
- $expected = array(2 => null, 14 => null, 25 => null);
- $this->assertEquals($expected, $result);
- $result = Set::combine($a, 'fail', 'fail');
- $this->assertSame(array(), $result);
- }
- /**
- * testMapReverse method
- *
- * @return void
- */
- public function testMapReverse() {
- $result = Set::reverse(null);
- $this->assertEquals(null, $result);
- $result = Set::reverse(false);
- $this->assertEquals(false, $result);
- $expected = array(
- 'Array1' => array(
- 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
- 'Array2' => array(
- 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
- 'Array3' => array(
- 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
- $map = Set::map($expected, true);
- $this->assertEquals($expected['Array1']['Array1Data1'], $map->Array1->Array1Data1);
- $this->assertEquals($expected['Array2'][0]['Array2Data1'], $map->Array2[0]->Array2Data1);
- $result = Set::reverse($map);
- $this->assertEquals($expected, $result);
- $expected = array(
- 'Post' => array('id' => 1, 'title' => 'First Post'),
- 'Comment' => array(
- array('id' => 1, 'title' => 'First Comment'),
- array('id' => 2, 'title' => 'Second Comment')
- ),
- 'Tag' => array(
- array('id' => 1, 'title' => 'First Tag'),
- array('id' => 2, 'title' => 'Second Tag')
- ),
- );
- $map = Set::map($expected);
- $this->assertEquals($expected['Post']['title'], $map->title);
- foreach ($map->Comment as $comment) {
- $ids[] = $comment->id;
- }
- $this->assertEquals(array(1, 2), $ids);
- $expected = array(
- 'Array1' => array(
- 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3', 'Array1Data4' => 'Array1Data4 value 4',
- 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
- 'string' => 1,
- 'another' => 'string',
- 'some' => 'thing else',
- 'Array2' => array(
- 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
- 'Array3' => array(
- 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
- $map = Set::map($expected, true);
- $result = Set::reverse($map);
- $this->assertEquals($expected, $result);
- $expected = array(
- 'Array1' => array(
- 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3', 'Array1Data4' => 'Array1Data4 value 4',
- 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
- 'string' => 1,
- 'another' => 'string',
- 'some' => 'thing else',
- 'Array2' => array(
- 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
- 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
- 'string2' => 1,
- 'another2' => 'string',
- 'some2' => 'thing else',
- 'Array3' => array(
- 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
- 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
- 'string3' => 1,
- 'another3' => 'string',
- 'some3' => 'thing else');
- $map = Set::map($expected, true);
- $result = Set::reverse($map);
- $this->assertEquals($expected, $result);
- $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
- $map = Set::map($expected);
- $result = Set::reverse($map);
- $this->assertEquals($expected, $result);
- $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->psword = 'whatever';
- $class->User->Icon = new \stdClass;
- $class->User->Icon->id = 851;
- $result = Set::reverse($class);
- $this->assertEquals($expected, $result);
- $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->psword = 'whatever';
- $class->User->Icon = new \stdClass;
- $class->User->Icon->id = 851;
- $class->User->Profile = new \stdClass;
- $class->User->Profile->name = 'Some Name';
- $class->User->Profile->address = 'Some Address';
- $result = Set::reverse($class);
- $this->assertEquals($expected, $result);
- $expected = array('User' => array('psword' => 'whatever',
- 'Icon' => array('id' => 851),
- 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
- 'Comment' => array(
- 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'),
- 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'))));
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->psword = 'whatever';
- $class->User->Icon = new \stdClass;
- $class->User->Icon->id = 851;
- $class->User->Profile = new \stdClass;
- $class->User->Profile->name = 'Some Name';
- $class->User->Profile->address = 'Some Address';
- $class->User->Comment = new \stdClass;
- $class->User->Comment->{'0'} = new \stdClass;
- $class->User->Comment->{'0'}->id = 1;
- $class->User->Comment->{'0'}->article_id = 1;
- $class->User->Comment->{'0'}->user_id = 1;
- $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
- $class->User->Comment->{'0'}->published = 'Y';
- $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
- $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
- $class->User->Comment->{'1'} = new \stdClass;
- $class->User->Comment->{'1'}->id = 2;
- $class->User->Comment->{'1'}->article_id = 1;
- $class->User->Comment->{'1'}->user_id = 2;
- $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
- $class->User->Comment->{'1'}->published = 'Y';
- $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
- $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
- $result = Set::reverse($class);
- $this->assertEquals($expected, $result);
- $expected = array('User' => array('psword' => 'whatever',
- 'Icon' => array('id' => 851),
- 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
- 'Comment' => array(
- 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'),
- 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'))));
- // @codingStandardsIgnoreStart
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->psword = 'whatever';
- $class->User->Icon = new \stdClass;
- $class->User->Icon->id = 851;
- $class->User->Profile = new \stdClass;
- $class->User->Profile->name = 'Some Name';
- $class->User->Profile->address = 'Some Address';
- $class->User->Comment = array();
- $comment = new \stdClass;
- $comment->id = 1;
- $comment->article_id = 1;
- $comment->user_id = 1;
- $comment->comment = 'First Comment for First Article';
- $comment->published = 'Y';
- $comment->created = '2007-03-18 10:47:23';
- $comment->updated = '2007-03-18 10:49:31';
- $comment2 = new \stdClass;
- $comment2->id = 2;
- $comment2->article_id = 1;
- $comment2->user_id = 2;
- $comment2->comment = 'Second Comment for First Article';
- $comment2->published = 'Y';
- $comment2->created = '2007-03-18 10:47:23';
- $comment2->updated = '2007-03-18 10:49:31';
- // @codingStandardsIgnoreEnd
- $class->User->Comment = array($comment, $comment2);
- $result = Set::reverse($class);
- $this->assertEquals($expected, $result);
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->id = 100;
- $class->someString = 'this is some string';
- $class->Profile = new \stdClass;
- $class->Profile->name = 'Joe Mamma';
- $result = Set::reverse($class);
- $expected = array(
- 'User' => array('id' => '100'),
- 'someString' => 'this is some string',
- 'Profile' => array('name' => 'Joe Mamma')
- );
- $this->assertEquals($expected, $result);
- // @codingStandardsIgnoreStart
- $class = new \stdClass;
- $class->User = new \stdClass;
- $class->User->id = 100;
- $class->User->_name_ = 'User';
- $class->Profile = new \stdClass;
- $class->Profile->name = 'Joe Mamma';
- $class->Profile->_name_ = 'Profile';
- // @codingStandardsIgnoreEnd
- $result = Set::reverse($class);
- $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormatting method
- *
- * @return void
- */
- public function testFormatting() {
- $data = array(
- array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
- array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
- array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
- $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
- $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
- $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
- $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
- $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
- $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
- $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
- $expected = array('42, Nate', '0, Larry', '0, Garrett');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
- $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormattingNullValues method
- *
- * @return void
- */
- public function testFormattingNullValues() {
- $data = array(
- array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
- array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null)),
- array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null)));
- $result = Set::format($data, '%s', array('{n}.Person.something'));
- $expected = array('42', '', '');
- $this->assertEquals($expected, $result);
- $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something'));
- $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
- $this->assertEquals($expected, $result);
- }
- /**
- * testCountDim method
- *
- * @return void
- */
- public function testCountDim() {
- $data = array('one', '2', 'three');
- $result = Set::countDim($data);
- $this->assertEquals(1, $result);
- $data = array('1' => '1.1', '2', '3');
- $result = Set::countDim($data);
- $this->assertEquals(1, $result);
- $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
- $result = Set::countDim($data);
- $this->assertEquals(2, $result);
- $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
- $result = Set::countDim($data);
- $this->assertEquals(1, $result);
- $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
- $result = Set::countDim($data, true);
- $this->assertEquals(2, $result);
- $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
- $result = Set::countDim($data);
- $this->assertEquals(2, $result);
- $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
- $result = Set::countDim($data, true);
- $this->assertEquals(3, $result);
- $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')));
- $result = Set::countDim($data, true);
- $this->assertEquals(4, $result);
- $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')));
- $result = Set::countDim($data, true);
- $this->assertEquals(5, $result);
- $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')));
- $result = Set::countDim($data, true);
- $this->assertEquals(5, $result);
- $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')));
- $result = Set::countDim($set, false, 0);
- $this->assertEquals(2, $result);
- $result = Set::countDim($set, true);
- $this->assertEquals(5, $result);
- }
- /**
- * testMapNesting method
- *
- * @return void
- */
- public function testMapNesting() {
- $expected = array(
- array(
- "IndexedPage" => array(
- "id" => 1,
- "url" => 'http://blah.com/',
- 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
- 'headers' => array(
- 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
- 'Server' => "Apache",
- 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
- 'Cache-Control' => "private",
- 'Pragma' => "no-cache",
- 'Content-Type' => "text/html; charset=UTF-8",
- 'X-Original-Transfer-Encoding' => "chunked",
- 'Content-Length' => "50210",
- ),
- 'meta' => array(
- 'keywords' => array('testing', 'tests'),
- 'description' => 'describe me',
- ),
- 'get_vars' => '',
- 'post_vars' => array(),
- 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
- 'redirect' => '',
- 'created' => "1195055503",
- 'updated' => "1195055503",
- )
- ),
- array(
- "IndexedPage" => array(
- "id" => 2,
- "url" => 'http://blah.com/',
- 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
- 'headers' => array(
- 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
- 'Server' => "Apache",
- 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
- 'Cache-Control' => "private",
- 'Pragma' => "no-cache",
- 'Content-Type' => "text/html; charset=UTF-8",
- 'X-Original-Transfer-Encoding' => "chunked",
- 'Content-Length' => "50210",
- ),
- 'meta' => array(
- 'keywords' => array('testing', 'tests'),
- 'description' => 'describe me',
- ),
- 'get_vars' => '',
- 'post_vars' => array(),
- 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
- 'redirect' => '',
- 'created' => "1195055503",
- 'updated' => "1195055503",
- ),
- )
- );
- $mapped = Set::map($expected);
- $ids = array();
- foreach ($mapped as $object) {
- $ids[] = $object->id;
- }
- $this->assertEquals(array(1, 2), $ids);
- $this->assertEquals($expected[0]['IndexedPage']['headers'], get_object_vars($mapped[0]->headers));
- $result = Set::reverse($mapped);
- $this->assertEquals($expected, $result);
- $data = array(
- array(
- "IndexedPage" => array(
- "id" => 1,
- "url" => 'http://blah.com/',
- 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
- 'get_vars' => '',
- 'redirect' => '',
- 'created' => "1195055503",
- 'updated' => "1195055503",
- )
- ),
- array(
- "IndexedPage" => array(
- "id" => 2,
- "url" => 'http://blah.com/',
- 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
- 'get_vars' => '',
- 'redirect' => '',
- 'created' => "1195055503",
- 'updated' => "1195055503",
- ),
- )
- );
- $mapped = Set::map($data);
- // @codingStandardsIgnoreStart
- $expected = new \stdClass();
- $expected->_name_ = 'IndexedPage';
- $expected->id = 2;
- $expected->url = 'http://blah.com/';
- $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
- $expected->get_vars = '';
- $expected->redirect = '';
- $expected->created = '1195055503';
- $expected->updated = '1195055503';
- // @codingStandardsIgnoreEnd
- $this->assertEquals($expected, $mapped[1]);
- $ids = array();
- foreach ($mapped as $object) {
- $ids[] = $object->id;
- }
- $this->assertEquals(array(1, 2), $ids);
- $result = Set::map(null);
- $expected = null;
- $this->assertEquals($expected, $result);
- }
- /**
- * testNestedMappedData method
- *
- * @return void
- */
- public function testNestedMappedData() {
- $result = Set::map(array(
- array(
- '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'),
- 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
- ),
- array(
- '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'),
- 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
- )
- ));
- // @codingStandardsIgnoreStart
- $expected = new \stdClass;
- $expected->_name_ = 'Post';
- $expected->id = '1';
- $expected->author_id = '1';
- $expected->title = 'First Post';
- $expected->body = 'First Post Body';
- $expected->published = 'Y';
- $expected->created = "2007-03-18 10:39:23";
- $expected->updated = "2007-03-18 10:41:31";
- $expected->Author = new \stdClass;
- $expected->Author->id = '1';
- $expected->Author->user = 'mariano';
- $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
- $expected->Author->created = '2007-03-17 01:16:23';
- $expected->Author->updated = '2007-03-17 01:18:31';
- $expected->Author->test = 'working';
- $expected->Author->_name_ = 'Author';
- $expected2 = new \stdClass;
- $expected2->_name_ = 'Post';
- $expected2->id = '2';
- $expected2->author_id = '3';
- $expected2->title = 'Second Post';
- $expected2->body = 'Second Post Body';
- $expected2->published = 'Y';
- $expected2->created = "2007-03-18 10:41:23";
- $expected2->updated = "2007-03-18 10:43:31";
- $expected2->Author = new \stdClass;
- $expected2->Author->id = '3';
- $expected2->Author->user = 'larry';
- $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
- $expected2->Author->created = '2007-03-17 01:20:23';
- $expected2->Author->updated = '2007-03-17 01:22:31';
- $expected2->Author->test = 'working';
- $expected2->Author->_name_ = 'Author';
- // @codingStandardsIgnoreEnd
- $test = array();
- $test[0] = $expected;
- $test[1] = $expected2;
- $this->assertEquals($test, $result);
- $result = Set::map(
- array(
- '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'),
- 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
- )
- );
- // @codingStandardsIgnoreStart
- $expected = new \stdClass;
- $expected->_name_ = 'Post';
- $expected->id = '1';
- $expected->author_id = '1';
- $expected->title = 'First Post';
- $expected->body = 'First Post Body';
- $expected->published = 'Y';
- $expected->created = "2007-03-18 10:39:23";
- $expected->updated = "2007-03-18 10:41:31";
- $expected->Author = new \stdClass;
- $expected->Author->id = '1';
- $expected->Author->user = 'mariano';
- $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
- $expected->Author->created = "2007-03-17 01:16:23";
- $expected->Author->updated = "2007-03-17 01:18:31";
- $expected->Author->test = 'working';
- $expected->Author->_name_ = 'Author';
- // @codingStandardsIgnoreEnd
- $this->assertEquals($expected, $result);
- //Case where extra HABTM fields come back in a result
- $data = array(
- 'User' => array(
- 'id' => 1,
- 'email' => 'user@example.com',
- 'first_name' => 'John',
- 'last_name' => 'Smith',
- ),
- 'Piece' => array(
- array(
- 'id' => 1,
- 'title' => 'Moonlight Sonata',
- 'composer' => 'Ludwig van Beethoven',
- 'PiecesUser' => array(
- 'id' => 1,
- 'created' => '2008-01-01 00:00:00',
- 'modified' => '2008-01-01 00:00:00',
- 'piece_id' => 1,
- 'user_id' => 2,
- )
- ),
- array(
- 'id' => 2,
- 'title' => 'Moonlight Sonata 2',
- 'composer' => 'Ludwig van Beethoven',
- 'PiecesUser' => array(
- 'id' => 2,
- 'created' => '2008-01-01 00:00:00',
- 'modified' => '2008-01-01 00:00:00',
- 'piece_id' => 2,
- 'user_id' => 2,
- )
- )
- )
- );
- $result = Set::map($data);
- // @codingStandardsIgnoreStart
- $expected = new \stdClass();
- $expected->_name_ = 'User';
- $expected->id = 1;
- $expected->email = 'user@example.com';
- $expected->first_name = 'John';
- $expected->last_name = 'Smith';
- $piece = new \stdClass();
- $piece->id = 1;
- $piece->title = 'Moonlight Sonata';
- $piece->composer = 'Ludwig van Beethoven';
- $piece->PiecesUser = new \stdClass();
- $piece->PiecesUser->id = 1;
- $piece->PiecesUser->created = '2008-01-01 00:00:00';
- $piece->PiecesUser->modified = '2008-01-01 00:00:00';
- $piece->PiecesUser->piece_id = 1;
- $piece->PiecesUser->user_id = 2;
- $piece->PiecesUser->_name_ = 'PiecesUser';
- $piece->_name_ = 'Piece';
- $piece2 = new \stdClass();
- $piece2->id = 2;
- $piece2->title = 'Moonlight Sonata 2';
- $piece2->composer = 'Ludwig van Beethoven';
- $piece2->PiecesUser = new \stdClass();
- $piece2->PiecesUser->id = 2;
- $piece2->PiecesUser->created = '2008-01-01 00:00:00';
- $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
- $piece2->PiecesUser->piece_id = 2;
- $piece2->PiecesUser->user_id = 2;
- $piece2->PiecesUser->_name_ = 'PiecesUser';
- $piece2->_name_ = 'Piece';
- // @codingStandardsIgnoreEnd
- $expected->Piece = array($piece, $piece2);
- $this->assertEquals($expected, $result);
- //Same data, but should work if _name_ has been manually defined:
- $data = array(
- 'User' => array(
- 'id' => 1,
- 'email' => 'user@example.com',
- 'first_name' => 'John',
- 'last_name' => 'Smith',
- '_name_' => 'FooUser',
- ),
- 'Piece' => array(
- array(
- 'id' => 1,
- 'title' => 'Moonlight Sonata',
- 'composer' => 'Ludwig van Beethoven',
- '_name_' => 'FooPiece',
- 'PiecesUser' => array(
- 'id' => 1,
- 'created' => '2008-01-01 00:00:00',
- 'modified' => '2008-01-01 00:00:00',
- 'piece_id' => 1,
- 'user_id' => 2,
- '_name_' => 'FooPiecesUser',
- )
- ),
- array(
- 'id' => 2,
- 'title' => 'Moonlight Sonata 2',
- 'composer' => 'Ludwig van Beethoven',
- '_name_' => 'FooPiece',
- 'PiecesUser' => array(
- 'id' => 2,
- 'created' => '2008-01-01 00:00:00',
- 'modified' => '2008-01-01 00:00:00',
- 'piece_id' => 2,
- 'user_id' => 2,
- '_name_' => 'FooPiecesUser',
- )
- )
- )
- );
- $result = Set::map($data);
- // @codingStandardsIgnoreStart
- $expected = new \stdClass();
- $expected->_name_ = 'FooUser';
- $expected->id = 1;
- $expected->email = 'user@example.com';
- $expected->first_name = 'John';
- $expected->last_name = 'Smith';
- $piece = new \stdClass();
- $piece->id = 1;
- $piece->title = 'Moonlight Sonata';
- $piece->composer = 'Ludwig van Beethoven';
- $piece->_name_ = 'FooPiece';
- $piece->PiecesUser = new \stdClass();
- $piece->PiecesUser->id = 1;
- $piece->PiecesUser->created = '2008-01-01 00:00:00';
- $piece->PiecesUser->modified = '2008-01-01 00:00:00';
- $piece->PiecesUser->piece_id = 1;
- $piece->PiecesUser->user_id = 2;
- $piece->PiecesUser->_name_ = 'FooPiecesUser';
- $piece2 = new \stdClass();
- $piece2->id = 2;
- $piece2->title = 'Moonlight Sonata 2';
- $piece2->composer = 'Ludwig van Beethoven';
- $piece2->_name_ = 'FooPiece';
- $piece2->PiecesUser = new \stdClass();
- $piece2->PiecesUser->id = 2;
- $piece2->PiecesUser->created = '2008-01-01 00:00:00';
- $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
- $piece2->PiecesUser->piece_id = 2;
- $piece2->PiecesUser->user_id = 2;
- $piece2->PiecesUser->_name_ = 'FooPiecesUser';
- // @codingStandardsIgnoreEnd
- $expected->Piece = array($piece, $piece2);
- $this->assertEquals($expected, $result);
- }
- /**
- * testPushDiff method
- *
- * @return void
- */
- public function testPushDiff() {
- $array1 = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2'));
- $array2 = array('ModelTwo' => array('id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2'));
- $result = Set::pushDiff($array1, $array2);
- $this->assertEquals($array1 + $array2, $result);
- $array3 = array('ModelOne' => array('id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3'));
- $result = Set::pushDiff($array1, $array3);
- $expected = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3'));
- $this->assertEquals($expected, $result);
- $array1 = array(
- 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
- 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
- $array2 = array(
- 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
- 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2')));
- $result = Set::pushDiff($array1, $array2);
- $this->assertEquals($array1, $result);
- $array3 = array(0 => array('ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')));
- $result = Set::pushDiff($array1, $array3);
- $expected = array(
- 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'),
- 'ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')),
- 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
- $this->assertEquals($expected, $result);
- $result = Set::pushDiff($array1, null);
- $this->assertEquals($array1, $result);
- $result = Set::pushDiff($array1, $array2);
- $this->assertEquals($array1 + $array2, $result);
- }
- /**
- * testSetApply method
- * @return void
- *
- */
- public function testApply() {
- $data = array(
- array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)),
- array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)),
- array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3))
- );
- $result = Set::apply('/Movie/rating', $data, 'array_sum');
- $expected = 9;
- $this->assertEquals($expected, $result);
- $result = Set::apply('/Movie/rating', $data, 'array_product');
- $expected = 15;
- $this->assertEquals($expected, $result);
- $result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
- $expected = array('Movie 3', 'Movie 1', 'Movie 2');
- $this->assertEquals($expected, $result);
- $result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
- $expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
- $this->assertEquals($expected, $result);
- $result = Set::apply('/Movie/rating', $data, array(__CLASS__, 'method'), array('type' => 'reduce'));
- $expected = 9;
- $this->assertEquals($expected, $result);
- $result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
- $expected = null;
- $this->assertEquals($expected, $result);
- }
- /**
- * Helper method to test Set::apply()
- *
- * @return void
- */
- public static function method($val1, $val2) {
- $val1 += $val2;
- return $val1;
- }
- /**
- * testXmlSetReverse method
- *
- * @return void
- */
- public function testXmlSetReverse() {
- $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <rss version="2.0">
- <channel>
- <title>Cake PHP Google Group</title>
- <link>http://groups.google.com/group/cake-php</link>
- <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.</description>
- <language>en</language>
- <item>
- <title>constructng result array when using findall</title>
- <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
- <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(</description>
- <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
- <author>bmil...@gmail.com(bpscrugs)</author>
- <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
- </item>
- <item>
- <title>Re: share views between actions?</title>
- <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
- <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</description>
- <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
- <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
- <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
- </item>
- </channel>
- </rss>';
- $xml = Xml::build($string);
- $result = Set::reverse($xml);
- $expected = array('rss' => array(
- '@version' => '2.0',
- 'channel' => array(
- 'title' => 'Cake PHP Google Group',
- 'link' => 'http://groups.google.com/group/cake-php',
- 'description' => 'Search this group before posting anything. There are over 20,000 posts and it'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.',
- 'language' => 'en',
- 'item' => array(
- array(
- 'title' => 'constructng result array when using findall',
- 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
- '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->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] => Array(",
- 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
- 'author' => 'bmil...@gmail.com(bpscrugs)',
- 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
- ),
- array(
- 'title' => 'Re: share views between actions?',
- 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
- '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 "RTFM" 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',
- 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
- 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
- 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
- )
- )
- )
- ));
- $this->assertEquals($expected, $result);
- $string = '<data><post title="Title of this post" description="cool"/></data>';
- $xml = Xml::build($string);
- $result = Set::reverse($xml);
- $expected = array('data' => array('post' => array('@title' => 'Title of this post', '@description' => 'cool')));
- $this->assertEquals($expected, $result);
- $xml = Xml::build('<example><item><title>An example of a correctly reversed SimpleXMLElement</title><desc/></item></example>');
- $result = Set::reverse($xml);
- $expected = array('example' =>
- array(
- 'item' => array(
- 'title' => 'An example of a correctly reversed SimpleXMLElement',
- 'desc' => '',
- )
- )
- );
- $this->assertEquals($expected, $result);
- $xml = Xml::build('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
- $result = Set::reverse($xml);
- $expected =
- array('example' => array(
- 'item' => array(
- '@attr' => '123',
- 'titles' => array(
- 'title' => array('title1', 'title2')
- )
- )
- )
- );
- $this->assertEquals($expected, $result);
- $xml = Xml::build('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
- $result = Set::reverse($xml);
- $expected =
- array('example' => array(
- '@attr' => 'ex_attr',
- 'item' => array(
- '@attr' => '123',
- 'titles' => 'list',
- '@' => 'textforitems'
- )
- )
- );
- $this->assertEquals($expected, $result);
- $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <rss version="2.0" xmlns:dc="http://www.cakephp.org/">
- <channel>
- <title>Cake PHP Google Group</title>
- <link>http://groups.google.com/group/cake-php</link>
- <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.</description>
- <language>en</language>
- <item>
- <title>constructng result array when using findall</title>
- <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
- <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(</description>
- <dc:creator>cakephp</dc:creator>
- <category><![CDATA[cakephp]]></category>
- <category><![CDATA[model]]></category>
- <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
- <author>bmil...@gmail.com(bpscrugs)</author>
- <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
- </item>
- <item>
- <title>Re: share views between actions?</title>
- <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
- <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</description>
- <dc:creator>cakephp</dc:creator>
- <category><![CDATA[cakephp]]></category>
- <category><![CDATA[model]]></category>
- <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
- <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
- <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
- </item>
- </channel>
- </rss>';
- $xml = Xml::build($string);
- $result = Set::reverse($xml);
- $expected = array('rss' => array(
- '@version' => '2.0',
- 'channel' => array(
- 'title' => 'Cake PHP Google Group',
- 'link' => 'http://groups.google.com/group/cake-php',
- 'description' => 'Search this group before posting anything. There are over 20,000 posts and it'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.',
- 'language' => 'en',
- 'item' => array(
- array(
- 'title' => 'constructng result array when using findall',
- 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
- '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->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] => Array(",
- 'dc:creator' => 'cakephp',
- 'category' => array('cakephp', 'model'),
- 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
- 'author' => 'bmil...@gmail.com(bpscrugs)',
- 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
- ),
- array(
- 'title' => 'Re: share views between actions?',
- 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
- '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 "RTFM" 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',
- 'dc:creator' => 'cakephp',
- 'category' => array('cakephp', 'model'),
- 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
- 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
- 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
- )
- )
- )
- ));
- $this->assertEquals($expected, $result);
- $text = '<?xml version="1.0" encoding="UTF-8"?>
- <XRDS xmlns="xri://$xrds">
- <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
- <Type>xri://$xrds*simple</Type>
- <Expires>2008-04-13T07:34:58Z</Expires>
- <Service>
- <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
- <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
- <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
- <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
- <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
- </Service>
- </XRD>
- <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
- <Type>xri://$xrds*simple</Type>
- <Service priority="10">
- <Type>http://oauth.net/discovery/1.0</Type>
- <URI>#oauth</URI>
- </Service>
- </XRD>
- </XRDS>';
- $xml = Xml::build($text);
- $result = Set::reverse($xml);
- $expected = array('XRDS' => array(
- 'XRD' => array(
- array(
- '@xml:id' => 'oauth',
- '@version' => '2.0',
- 'Type' => 'xri://$xrds*simple',
- 'Expires' => '2008-04-13T07:34:58Z',
- 'Service' => array(
- 'Type' => array(
- 'http://oauth.net/core/1.0/endpoint/authorize',
- 'http://oauth.net/core/1.0/parameters/auth-header',
- 'http://oauth.net/core/1.0/parameters/uri-query'
- ),
- 'URI' => array(
- array(
- '@' => 'https://ma.gnolia.com/oauth/authorize',
- '@priority' => '10',
- ),
- array(
- '@' => 'http://ma.gnolia.com/oauth/authorize',
- '@priority' => '20'
- )
- )
- )
- ),
- array(
- '@version' => '2.0',
- 'Type' => 'xri://$xrds*simple',
- 'Service' => array(
- '@priority' => '10',
- 'Type' => 'http://oauth.net/discovery/1.0',
- 'URI' => '#oauth'
- )
- )
- )
- ));
- $this->assertEquals($expected, $result);
- }
- /**
- * testStrictKeyCheck method
- *
- * @return void
- */
- public function testStrictKeyCheck() {
- $set = array('a' => 'hi');
- $this->assertFalse(Set::check($set, 'a.b'));
- }
- /**
- * Tests Set::flatten
- *
- * @see Hash test cases, as Set::flatten() is just a proxy.
- * @return void
- */
- public function testFlatten() {
- $data = array('Larry', 'Curly', 'Moe');
- $result = Set::flatten($data);
- $this->assertEquals($data, $result);
- $data[9] = 'Shemp';
- $result = Set::flatten($data);
- $this->assertEquals($data, $result);
- $data = array(
- array(
- 'Post' => array('id' => '1', 'author_id' => null, 'title' => 'First Post'),
- 'Author' => array(),
- )
- );
- $result = Set::flatten($data);
- $expected = array(
- '0.Post.id' => '1',
- '0.Post.author_id' => null,
- '0.Post.title' => 'First Post',
- '0.Author' => array()
- );
- $this->assertEquals($expected, $result);
- }
- /**
- * Tests Set::expand
- *
- * @return void
- */
- public function testExpand() {
- $data = array('My', 'Array', 'To', 'Flatten');
- $flat = Set::flatten($data);
- $result = Set::expand($flat);
- $this->assertEquals($data, $result);
- }
- /**
- * test normalization
- *
- * @return void
- */
- public function testNormalizeStrings() {
- $result = Set::normalize('one,two,three');
- $expected = array('one' => null, 'two' => null, 'three' => null);
- $this->assertEquals($expected, $result);
- $result = Set::normalize('one two three', true, ' ');
- $expected = array('one' => null, 'two' => null, 'three' => null);
- $this->assertEquals($expected, $result);
- $result = Set::normalize('one , two , three ', true, ',', true);
- $expected = array('one' => null, 'two' => null, 'three' => null);
- $this->assertEquals($expected, $result);
- }
- /**
- * test normalizing arrays
- *
- * @return void
- */
- public function testNormalizeArrays() {
- $result = Set::normalize(array('one', 'two', 'three'));
- $expected = array('one' => null, 'two' => null, 'three' => null);
- $this->assertEquals($expected, $result);
- $result = Set::normalize(array('one', 'two', 'three'), false);
- $expected = array('one', 'two', 'three');
- $this->assertEquals($expected, $result);
- $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
- $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
- $this->assertEquals($expected, $result);
- $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
- $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
- $this->assertEquals($expected, $result);
- $result = Set::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
- $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
- $this->assertEquals($expected, $result);
- }
- /**
- * test Set nest with a normal model result set. For kicks rely on Set nest detecting the key names
- * automatically
- *
- * @return void
- */
- public function testNestModel() {
- $input = array(
- array(
- 'ModelName' => array(
- 'id' => 1,
- 'parent_id' => null
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 2,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 3,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 4,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 5,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 6,
- 'parent_id' => null
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 7,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 8,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 9,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 10,
- 'parent_id' => 6
- )
- )
- );
- $expected = array(
- array(
- 'ModelName' => array(
- 'id' => 1,
- 'parent_id' => null
- ),
- 'children' => array(
- array(
- 'ModelName' => array(
- 'id' => 2,
- 'parent_id' => 1
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 3,
- 'parent_id' => 1
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 4,
- 'parent_id' => 1
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 5,
- 'parent_id' => 1
- ),
- 'children' => array()
- ),
- )
- ),
- array(
- 'ModelName' => array(
- 'id' => 6,
- 'parent_id' => null
- ),
- 'children' => array(
- array(
- 'ModelName' => array(
- 'id' => 7,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 8,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 9,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 10,
- 'parent_id' => 6
- ),
- 'children' => array()
- )
- )
- )
- );
- $result = Set::nest($input);
- $this->assertEquals($expected, $result);
- }
- /**
- * test Set nest with a normal model result set, and a nominated root id
- *
- * @return void
- */
- public function testNestModelExplicitRoot() {
- $input = array(
- array(
- 'ModelName' => array(
- 'id' => 1,
- 'parent_id' => null
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 2,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 3,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 4,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 5,
- 'parent_id' => 1
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 6,
- 'parent_id' => null
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 7,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 8,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 9,
- 'parent_id' => 6
- ),
- ),
- array(
- 'ModelName' => array(
- 'id' => 10,
- 'parent_id' => 6
- )
- )
- );
- $expected = array(
- array(
- 'ModelName' => array(
- 'id' => 6,
- 'parent_id' => null
- ),
- 'children' => array(
- array(
- 'ModelName' => array(
- 'id' => 7,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 8,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 9,
- 'parent_id' => 6
- ),
- 'children' => array()
- ),
- array(
- 'ModelName' => array(
- 'id' => 10,
- 'parent_id' => 6
- ),
- 'children' => array()
- )
- )
- )
- );
- $result = Set::nest($input, array('root' => 6));
- $this->assertEquals($expected, $result);
- }
- /**
- * test Set nest with a 1d array - this method should be able to handle any type of array input
- *
- * @return void
- */
- public function testNest1Dimensional() {
- $input = array(
- array(
- 'id' => 1,
- 'parent_id' => null
- ),
- array(
- 'id' => 2,
- 'parent_id' => 1
- ),
- array(
- 'id' => 3,
- 'parent_id' => 1
- ),
- array(
- 'id' => 4,
- 'parent_id' => 1
- ),
- array(
- 'id' => 5,
- 'parent_id' => 1
- ),
- array(
- 'id' => 6,
- 'parent_id' => null
- ),
- array(
- 'id' => 7,
- 'parent_id' => 6
- ),
- array(
- 'id' => 8,
- 'parent_id' => 6
- ),
- array(
- 'id' => 9,
- 'parent_id' => 6
- ),
- array(
- 'id' => 10,
- 'parent_id' => 6
- )
- );
- $expected = array(
- array(
- 'id' => 1,
- 'parent_id' => null,
- 'children' => array(
- array(
- 'id' => 2,
- 'parent_id' => 1,
- 'children' => array()
- ),
- array(
- 'id' => 3,
- 'parent_id' => 1,
- 'children' => array()
- ),
- array(
- 'id' => 4,
- 'parent_id' => 1,
- 'children' => array()
- ),
- array(
- 'id' => 5,
- 'parent_id' => 1,
- 'children' => array()
- ),
- )
- ),
- array(
- 'id' => 6,
- 'parent_id' => null,
- 'children' => array(
- array(
- 'id' => 7,
- 'parent_id' => 6,
- 'children' => array()
- ),
- array(
- 'id' => 8,
- 'parent_id' => 6,
- 'children' => array()
- ),
- array(
- 'id' => 9,
- 'parent_id' => 6,
- 'children' => array()
- ),
- array(
- 'id' => 10,
- 'parent_id' => 6,
- 'children' => array()
- )
- )
- )
- );
- $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
- $this->assertEquals($expected, $result);
- }
- /**
- * test Set nest with no specified parent data.
- *
- * The result should be the same as the input.
- * For an easier comparison, unset all the empty children arrays from the result
- *
- * @return void
- */
- public function testMissingParent() {
- $input = array(
- array(
- 'id' => 1,
- ),
- array(
- 'id' => 2,
- ),
- array(
- 'id' => 3,
- ),
- array(
- 'id' => 4,
- ),
- array(
- 'id' => 5,
- ),
- array(
- 'id' => 6,
- ),
- array(
- 'id' => 7,
- ),
- array(
- 'id' => 8,
- ),
- array(
- 'id' => 9,
- ),
- array(
- 'id' => 10,
- )
- );
- $result = Set::nest($input, array('idPath' => '/id', 'parentPath' => '/parent_id'));
- foreach ($result as &$row) {
- if (empty($row['children'])) {
- unset($row['children']);
- }
- }
- $this->assertEquals($input, $result);
- }
- }
|