| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872 |
- <?php
- namespace Alcaeus\MongoDbAdapter\Tests\Mongo;
- use MongoDB\BSON\Regex;
- use MongoDB\Driver\ReadPreference;
- use Alcaeus\MongoDbAdapter\Tests\TestCase;
- /**
- * @author alcaeus <alcaeus@alcaeus.org>
- */
- class MongoCollectionTest extends TestCase
- {
- public function testSerialize()
- {
- $this->assertInternalType('string', serialize($this->getCollection()));
- }
- public function testGetNestedCollections()
- {
- $collection = $this->getCollection()->foo->bar;
- $this->assertSame('mongo-php-adapter.test.foo.bar', (string) $collection);
- }
- public function testCreateRecord()
- {
- $collection = $this->getCollection();
- $expected = [
- 'ok' => 1.0,
- 'n' => 0,
- 'err' => null,
- 'errmsg' => null,
- ];
- $document = ['foo' => 'bar'];
- $this->assertSame($expected, $collection->insert($document));
- $this->assertInstanceOf('MongoId', $document['_id']);
- $id = (string) $document['_id'];
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
- $this->assertSame($id, (string) $object->_id);
- $this->assertObjectHasAttribute('foo', $object);
- $this->assertAttributeSame('bar', 'foo', $object);
- }
- public function testInsertInvalidData()
- {
- $this->setExpectedException('PHPUnit_Framework_Error_Warning', 'MongoCollection::insert(): expects parameter 1 to be an array or object, integer given');
- $document = 8;
- $this->getCollection()->insert($document);
- }
- public function testInsertEmptyArray()
- {
- $document = [];
- $this->getCollection()->insert($document);
- $this->assertSame(1, $this->getCollection()->count());
- }
- public function testInsertArrayWithNumericKeys()
- {
- $document = [1 => 'foo'];
- $this->getCollection()->insert($document);
- $this->assertSame(1, $this->getCollection()->count(['_id' => $document['_id']]));
- }
- public function testInsertEmptyObject()
- {
- $document = (object) [];
- $this->getCollection()->insert($document);
- $this->assertSame(1, $this->getCollection()->count());
- }
- public function testInsertObjectWithPrivateProperties()
- {
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
- $document = new PrivatePropertiesStub();
- $this->getCollection()->insert($document);
- }
- public function testInsertWithInvalidKey()
- {
- $document = ['*' => 'foo'];
- $this->getCollection()->insert($document);
- $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
- }
- public function testInsertWithEmptyKey()
- {
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
- $document = ['' => 'foo'];
- $this->getCollection()->insert($document);
- }
- public function testInsertWithNumericKey()
- {
- $document = ['foo'];
- $this->getCollection()->insert($document);
- $this->assertSame(1, $this->getCollection()->count(['foo']));
- }
- public function testInsertDuplicate()
- {
- $collection = $this->getCollection();
- $collection->createIndex(['foo' => 1], ['unique' => true]);
- $document = ['foo' => 'bar'];
- $collection->insert($document);
- unset($document['_id']);
- $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter\.test/');
- $collection->insert($document);
- }
- public function testUnacknowledgedWrite()
- {
- $document = ['foo' => 'bar'];
- $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
- }
- public function testUnacknowledgedWriteWithBooleanValue()
- {
- $document = ['foo' => 'bar'];
- $this->assertTrue($this->getCollection()->insert($document, ['w' => false]));
- }
- public function testAcknowledgedWriteConcernWithBool()
- {
- $document = ['foo' => 'bar'];
- $this->assertSame(
- [
- 'ok' => 1.0,
- 'n' => 0,
- 'err' => null,
- 'errmsg' => null,
- ],
- $this->getCollection()->insert($document, ['w' => true])
- );
- }
- public function testInsertWriteConcernException()
- {
- $this->setExpectedException(
- 'MongoWriteConcernException',
- "cannot use 'w' > 1 when a host is not replicated"
- );
- $document = ['foo' => 'bar'];
- $this->getCollection()->insert($document, ['w' => 2]);
- }
- public function testInsertMany()
- {
- $expected = [
- 'ok' => 1.0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- ];
- $documents = [
- ['foo' => 'bar'],
- ['bar' => 'foo']
- ];
- $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
- foreach ($documents as $document) {
- $this->assertInstanceOf('MongoId', $document['_id']);
- }
- }
- public function testInsertManyWithNonNumericKeys()
- {
- $expected = [
- 'ok' => 1.0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- ];
- $documents = [
- 'a' => ['foo' => 'bar'],
- 'b' => ['bar' => 'foo']
- ];
- $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(2, $newCollection->count());
- }
- public function testBatchInsertContinuesOnError()
- {
- $expected = [
- 'ok' => 1.0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- ];
- $documents = [
- 8,
- 'b' => ['bar' => 'foo']
- ];
- $this->assertArraySubset($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- }
- public function testBatchInsertException()
- {
- $this->setExpectedExceptionRegExp('MongoDuplicateKeyException', '/E11000 duplicate key error .* mongo-php-adapter.test.*_id_/');
- $id = new \MongoId();
- $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
- $this->getCollection()->batchInsert($documents);
- }
- public function testBatchInsertObjectWithPrivateProperties()
- {
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
- $documents = [new PrivatePropertiesStub()];
- $this->getCollection()->batchInsert($documents);
- }
- public function testBatchInsertWithInvalidKey()
- {
- $documents = [['*' => 'foo']];
- $this->getCollection()->batchInsert($documents);
- $this->assertSame(1, $this->getCollection()->count(['*' => 'foo']));
- }
- public function testBatchInsertWithEmptyKey()
- {
- $this->setExpectedException('MongoException', 'zero-length keys are not allowed, did you use $ with double quotes?');
- $documents = [['' => 'foo']];
- $this->getCollection()->batchInsert($documents);
- }
- public function testBatchInsertWithNumericKey()
- {
- $documents = [['foo']];
- $this->getCollection()->batchInsert($documents);
- $this->assertSame(1, $this->getCollection()->count(['foo']));
- }
- public function testBatchInsertEmptyBatchException()
- {
- $this->setExpectedException('MongoException', 'No write ops were included in the batch');
- $documents = [];
- $this->getCollection()->batchInsert($documents, ['w' => 2]);
- }
- public function testUpdateWriteConcern()
- {
- $this->setExpectedException('MongoWriteConcernException', "cannot use 'w' > 1 when a host is not replicated");
- $this->getCollection()->update([], ['$set' => ['foo' => 'bar']], ['w' => 2]);
- }
- public function testUpdateOne()
- {
- $document = ['foo' => 'bar'];
- $this->getCollection()->insert($document);
- // Unset ID to re-insert
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 1,
- 'n' => 1,
- 'err' => null,
- 'errmsg' => null,
- 'updatedExisting' => true,
- ];
- $result = $this->getCollection()->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
- $this->assertSame($expected, $result);
- $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
- }
- public function testUpdateReplaceOne()
- {
- $document = ['foo' => 'bar', 'bar' => 'foo'];
- $this->getCollection()->insert($document);
- // Unset ID to re-insert
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 1,
- 'n' => 1,
- 'err' => null,
- 'errmsg' => null,
- 'updatedExisting' => true,
- ];
- $result = $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo']);
- $this->assertSame($expected, $result);
- $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
- $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count(['bar' => 'foo']));
- }
- public function testUpdateReplaceMultiple()
- {
- $this->setExpectedExceptionRegExp('MongoWriteConcernException', '/multi update only works with \$ operators/', 9);
- $this->getCollection()->update(['foo' => 'bar'], ['foo' => 'foo'], ['multiple' => true]);
- }
- public function testUpdateDuplicate()
- {
- $collection = $this->getCollection();
- $collection->createIndex(['foo' => 1], ['unique' => 1]);
- $document = ['foo' => 'bar'];
- $collection->insert($document);
- $document = ['foo' => 'foo'];
- $collection->insert($document);
- $this->setExpectedException('MongoDuplicateKeyException');
- $collection->update(['foo' => 'bar'], ['$set' => ['foo' => 'foo']]);
- }
- public function testUpdateMany()
- {
- $document = ['change' => true, 'foo' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $document = ['change' => true, 'foo' => 'foo'];
- $this->getCollection()->insert($document);
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 2,
- 'n' => 3,
- 'err' => null,
- 'errmsg' => null,
- 'updatedExisting' => true,
- ];
- $result = $this->getCollection()->update(['change' => true], ['$set' => ['foo' => 'foo']], ['multiple' => true]);
- $this->assertSame($expected, $result);
- $this->assertSame(3, $this->getCheckDatabase()->selectCollection('test')->count(['foo' => 'foo']));
- }
- public function testUnacknowledgedUpdate()
- {
- $document = ['foo' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $this->assertTrue($this->getCollection()->update($document, ['$set' => ['foo' => 'foo']], ['w' => 0]));
- }
- public function testRemoveMultiple()
- {
- $document = ['change' => true, 'foo' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $document = ['change' => true, 'foo' => 'foo'];
- $this->getCollection()->insert($document);
- $expected = [
- 'ok' => 1.0,
- 'n' => 2,
- 'err' => null,
- 'errmsg' => null,
- ];
- $result = $this->getCollection()->remove(['foo' => 'bar']);
- $this->assertSame($expected, $result);
- $this->assertSame(1, $this->getCheckDatabase()->selectCollection('test')->count());
- }
- public function testRemoveSingle()
- {
- $document = ['change' => true, 'foo' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $expected = [
- 'ok' => 1.0,
- 'n' => 1,
- 'err' => null,
- 'errmsg' => null,
- ];
- $result = $this->getCollection()->remove(['foo' => 'bar'], ['justOne' => true]);
- $this->assertSame($expected, $result);
- $this->assertSame(2, $this->getCheckDatabase()->selectCollection('test')->count());
- }
- public function testRemoveUnacknowledged()
- {
- $document = ['change' => true, 'foo' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $this->assertTrue($this->getCollection()->remove(['foo' => 'bar'], ['w' => 0]));
- }
- public function testFindReturnsCursor()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertInstanceOf('MongoCursor', $collection->find());
- }
- /**
- * @dataProvider dataFindWithProjection
- */
- public function testFindWithProjection($projection)
- {
- $document = ['foo' => 'foo', 'bar' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $cursor = $this->getCollection()->find(['foo' => 'foo'], $projection);
- foreach ($cursor as $document) {
- $this->assertCount(2, $document);
- $this->assertArrayHasKey('_id', $document);
- $this->assertArraySubset(['bar' => 'bar'], $document);
- }
- }
- public static function dataFindWithProjection()
- {
- return [
- 'projection' => [['bar' => true]],
- 'intProjection' => [['bar' => 1]],
- 'legacyProjection' => [['bar']],
- ];
- }
- /**
- * @dataProvider dataFindWithProjectionAndNumericKeys
- */
- public function testFindWithProjectionAndNumericKeys($data, $projection, $expected)
- {
- $this->getCollection()->insert($data);
- $document = $this->getCollection()->findOne([], $projection);
- unset($document['_id']);
- $this->assertSame($expected, $document);
- }
- public static function dataFindWithProjectionAndNumericKeys()
- {
- return [
- 'sequentialIntegersStartingWithOne' => [
- ['0' => 'foo', '1' => 'bar', '2' => 'foobar'],
- [1 => true, 2 => true],
- ['1' => 'bar', '2' => 'foobar'],
- ],
- 'nonSequentialIntegers' => [
- ['0' => 'foo', '1' => 'bar', '2' => 'foobar', '3' => 'barfoo'],
- [1 => true, 3 => true],
- ['1' => 'bar', '3' => 'barfoo'],
- ]
- ];
- }
- public function testFindWithProjectionAndSequentialNumericKeys()
- {
- $this->setExpectedException(\MongoException::class, 'field names must be strings', 8);
- $this->getCollection()->findOne([], [true, false]);
- }
- /**
- * @dataProvider dataFindWithProjectionExcludeId
- */
- public function testFindWithProjectionExcludeId($projection)
- {
- $document = ['foo' => 'foo', 'bar' => 'bar'];
- $this->getCollection()->insert($document);
- unset($document['_id']);
- $this->getCollection()->insert($document);
- $cursor = $this->getCollection()->find(['foo' => 'foo'], $projection);
- foreach ($cursor as $document) {
- $this->assertCount(1, $document);
- $this->assertArrayNotHasKey('_id', $document);
- $this->assertArraySubset(['bar' => 'bar'], $document);
- }
- }
- public static function dataFindWithProjectionExcludeId()
- {
- return [
- 'projection' => [['_id' => false, 'bar' => true]],
- 'intProjection' => [['_id' => 0, 'bar' => 1]],
- ];
- }
- public function testCount()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(3, $collection->count());
- $this->assertSame(2, $collection->count(['foo' => 'bar']));
- }
- public function testCountWithLimit()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], ['limit' => 2]));
- $this->assertSame(1, $collection->count(['foo' => 'bar'], ['limit' => 1]));
- }
- public function testCountWithLimitLegacy()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], 2));
- $this->assertSame(1, $collection->count(['foo' => 'bar'], 1));
- }
- public function testCountWithSkip()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], ['skip' => 1]));
- $this->assertSame(1, $collection->count(['foo' => 'bar'], ['skip' => 1]));
- }
- public function testCountWithSkipLegacy()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], null, 1));
- $this->assertSame(1, $collection->count(['foo' => 'bar'], null, 1));
- }
- public function testCountWithLimitAndSkip()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], ['skip' => 1, 'limit' => 2]));
- $this->assertSame(1, $collection->count([], ['skip' => 1, 'limit' => 1]));
- }
- public function testCountWithLimitAndSkipLegacy()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(2, $collection->count([], 2, 1));
- $this->assertSame(1, $collection->count([], 1, 1));
- }
- public function testCountTimeout()
- {
- $this->failMaxTimeMS();
- $this->setExpectedException('MongoExecutionTimeoutException');
- $this->getCollection()->count([], ['maxTimeMS' => 1]);
- }
- public function testFindOne()
- {
- $this->prepareData();
- $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
- $this->assertSame(['foo' => 'foo'], $document);
- }
- public function testFindOneWithProjection()
- {
- $document = ['foo' => 'foo', 'bar' => 'bar'];
- $this->getCollection()->insert($document);
- $document = $this->getCollection()->findOne(['foo' => 'foo'], ['bar' => true]);
- $this->assertCount(2, $document);
- $this->assertArraySubset(['bar' => 'bar'], $document);
- }
- public function testFindOneWithLegacyProjection()
- {
- $document = ['foo' => 'foo', 'bar' => 'bar'];
- $this->getCollection()->insert($document);
- $document = $this->getCollection()->findOne(['foo' => 'foo'], ['bar']);
- $this->assertCount(2, $document);
- $this->assertArraySubset(['bar' => 'bar'], $document);
- }
- public function testFindOneNotFound()
- {
- $document = $this->getCollection()->findOne(['foo' => 'foo'], ['_id' => false]);
- $this->assertNull($document);
- }
- public function testFindOneConnectionIssue()
- {
- $this->setExpectedException('MongoConnectionException');
- $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
- $collection = $client->selectCollection('mongo-php-adapter', 'test');
- $collection->findOne();
- }
- public function testDistinct()
- {
- $this->prepareData();
- $values = $this->getCollection()->distinct('foo');
- $this->assertInternalType('array', $values);
- sort($values);
- $this->assertEquals(['bar', 'foo'], $values);
- }
- public function testDistinctWithQuery()
- {
- $this->prepareData();
- $values = $this->getCollection()->distinct('foo', ['foo' => 'bar']);
- $this->assertInternalType('array', $values);
- $this->assertEquals(['bar'], $values);
- }
- public function testDistinctWithIdQuery()
- {
- $document1 = ['foo' => 'bar'];
- $document2 = ['foo' => 'bar'];
- $document3 = ['foo' => 'foo'];
- $collection = $this->getCollection();
- $collection->insert($document1);
- $collection->insert($document2);
- $collection->insert($document3);
- $this->assertSame(
- ['bar'],
- $collection->distinct('foo', ['_id' => [
- '$in' => [$document1['_id'], $document2['_id']]
- ]])
- );
- $this->assertEquals(
- ['bar', 'foo'],
- $collection->distinct('foo', ['_id' => [
- '$in' => [$document1['_id'], $document3['_id']]
- ]])
- );
- }
- public function testAggregate()
- {
- $collection = $this->getCollection();
- $this->prepareData();
- $pipeline = [
- [
- '$group' => [
- '_id' => '$foo',
- 'count' => [ '$sum' => 1 ],
- ],
- ],
- [
- '$sort' => ['_id' => 1]
- ]
- ];
- $result = $collection->aggregate($pipeline);
- $this->assertInternalType('array', $result);
- $this->assertArrayHasKey('result', $result);
- $this->assertEquals([
- ['_id' => 'bar', 'count' => 2],
- ['_id' => 'foo', 'count' => 1],
- ], $result['result']);
- }
- public function testAggregateWithMultiplePilelineOperatorsAsArguments()
- {
- $collection = $this->getCollection();
- $this->prepareData();
- try {
- $result = $collection->aggregate(
- [
- '$group' => [
- '_id' => '$foo',
- 'count' => [ '$sum' => 1 ],
- ],
- ],
- [
- '$sort' => ['_id' => 1]
- ]
- );
- } catch (\MongoResultException $ex) {
- $msg = 'MongoCollection::aggregate ( array $op [, array $op [, array $... ]] ) should accept variable amount of pipeline operators as argument'
- . "\n"
- . $ex;
- $this->fail($msg);
- }
- $this->assertInternalType('array', $result);
- $this->assertArrayHasKey('result', $result);
- $this->assertEquals([
- ['_id' => 'bar', 'count' => 2],
- ['_id' => 'foo', 'count' => 1],
- ], $result['result']);
- }
- public function testAggregateInvalidPipeline()
- {
- $collection = $this->getCollection();
- $pipeline = [
- [
- '$invalid' => []
- ],
- ];
- $this->setExpectedException('MongoResultException', 'Unrecognized pipeline stage name');
- $collection->aggregate($pipeline);
- }
- public function testAggregateTimeoutException()
- {
- $collection = $this->getCollection();
- $this->failMaxTimeMS();
- $this->setExpectedException('MongoExecutionTimeoutException');
- $pipeline = [
- [
- '$group' => [
- '_id' => '$foo',
- 'count' => [ '$sum' => 1 ],
- ],
- ],
- [
- '$sort' => ['_id' => 1]
- ]
- ];
- $collection->aggregate($pipeline, ['maxTimeMS' => 1]);
- }
- public function testAggregateCursor()
- {
- $collection = $this->getCollection();
- $this->prepareData();
- $pipeline = [
- [
- '$group' => [
- '_id' => '$foo',
- 'count' => [ '$sum' => 1 ],
- ],
- ],
- [
- '$sort' => ['_id' => 1]
- ]
- ];
- $cursor = $collection->aggregateCursor($pipeline);
- $this->assertInstanceOf('MongoCommandCursor', $cursor);
- $this->assertEquals([
- ['_id' => 'bar', 'count' => 2],
- ['_id' => 'foo', 'count' => 1],
- ], iterator_to_array($cursor));
- }
- public function testReadPreference()
- {
- $collection = $this->getCollection();
- $this->assertSame(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
- $this->assertFalse($collection->getSlaveOkay());
- $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
- $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
- $this->assertTrue($collection->getSlaveOkay());
- $this->assertTrue($collection->setSlaveOkay(true));
- $this->assertSame(['type' => \MongoClient::RP_SECONDARY_PREFERRED, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
- $this->assertTrue($collection->setSlaveOkay(false));
- $this->assertArraySubset(['type' => \MongoClient::RP_PRIMARY], $collection->getReadPreference());
- }
- public function testReadPreferenceIsSetInDriver()
- {
- $this->skipTestIf(extension_loaded('mongo'));
- $collection = $this->getCollection();
- $this->assertTrue($collection->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]));
- // Only way to check whether options are passed down is through debugInfo
- $readPreference = $collection->getCollection()->__debugInfo()['readPreference'];
- $this->assertSame(ReadPreference::RP_SECONDARY, $readPreference->getMode());
- $this->assertSame([['a' => 'b']], $readPreference->getTagSets());
- }
- public function testReadPreferenceIsInherited()
- {
- $database = $this->getDatabase();
- $database->setReadPreference(\MongoClient::RP_SECONDARY, [['a' => 'b']]);
- $collection = $database->selectCollection('test');
- $this->assertSame(['type' => \MongoClient::RP_SECONDARY, 'tagsets' => [['a' => 'b']]], $collection->getReadPreference());
- }
- public function testWriteConcern()
- {
- $collection = $this->getCollection();
- $this->assertTrue($collection->setWriteConcern('majority', 100));
- $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
- }
- public function testWriteConcernIsSetInDriver()
- {
- $this->skipTestIf(extension_loaded('mongo'));
- $collection = $this->getCollection();
- $this->assertTrue($collection->setWriteConcern(2, 100));
- // Only way to check whether options are passed down is through debugInfo
- $writeConcern = $collection->getCollection()->__debugInfo()['writeConcern'];
- $this->assertSame(2, $writeConcern->getW());
- $this->assertSame(100, $writeConcern->getWtimeout());
- }
- public function testWriteConcernIsInherited()
- {
- $database = $this->getDatabase();
- $database->setWriteConcern('majority', 100);
- $collection = $database->selectCollection('test');
- $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
- }
- public function testSaveInsert()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $objectId = new \MongoId($id);
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 0,
- 'n' => 1,
- 'err' => null,
- 'errmsg' => null,
- 'upserted' => $objectId,
- 'updatedExisting' => false,
- ];
- $document = ['_id' => $objectId, 'foo' => 'bar'];
- $this->assertEquals($expected, $collection->save($document));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
- $this->assertSame($id, (string) $object->_id);
- $this->assertObjectHasAttribute('foo', $object);
- $this->assertAttributeSame('bar', 'foo', $object);
- }
- public function testRemoveOne()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $collection->remove(['_id' => new \MongoId($id)]);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(0, $newCollection->count());
- }
- public function testSaveUpdate()
- {
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 1,
- 'n' => 1,
- 'err' => null,
- 'errmsg' => null,
- 'updatedExisting' => true,
- ];
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $saveDocument = ['_id' => new \MongoId($id), 'foo' => 'foo'];
- $collection->insert($insertDocument);
- $this->assertSame($expected, $collection->save($saveDocument));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeInstanceOf('MongoDB\BSON\ObjectID', '_id', $object);
- $this->assertSame($id, (string) $object->_id);
- $this->assertObjectHasAttribute('foo', $object);
- $this->assertAttributeSame('foo', 'foo', $object);
- }
- public function testSavingShouldReplaceTheWholeDocument() {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $insertDocument = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $saveDocument = ['_id' => new \MongoId($id)];
- $collection->insert($insertDocument);
- $collection->save($saveDocument);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertObjectNotHasAttribute('foo', $object);
- }
- public function testSaveDuplicate()
- {
- $collection = $this->getCollection();
- $collection->createIndex(['foo' => 1], ['unique' => true]);
- $document = ['foo' => 'bar'];
- $collection->save($document);
- $this->setExpectedException('MongoDuplicateKeyException');
- unset($document['_id']);
- $collection->save($document);
- }
- public function testSaveEmptyKeys()
- {
- $document = [];
- $this->getCollection()->save($document);
- $this->assertSame(1, $this->getCollection()->count());
- }
- public function testSaveEmptyObject()
- {
- $document = (object) [];
- $this->getCollection()->save($document);
- $this->assertSame(1, $this->getCollection()->count());
- }
- public function testGetDBRef()
- {
- $collection = $this->getCollection();
- $insertDocument = ['_id' => 1, 'foo' => 'bar'];
- $collection->insert($insertDocument);
- $document = $collection->getDBRef([
- '$ref' => 'test',
- '$id' => 1,
- ]);
- $this->assertEquals($insertDocument, $document);
- }
- public function testCreateDBRef()
- {
- $collection = $this->getCollection();
- $reference = $collection->createDBRef(['_id' => 'foo']);
- $this->assertSame(
- [
- '$ref' => 'test',
- '$id' => 'foo',
- ],
- $reference
- );
- }
- public function testCreateIndex()
- {
- $expected = [
- 'createdCollectionAutomatically' => true,
- 'numIndexesBefore' => 1,
- 'numIndexesAfter' => 2,
- 'ok' => 1.0,
- ];
- $collection = $this->getCollection();
- $this->assertSame($expected, $collection->createIndex(['foo' => 1]));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $iterator = $newCollection->listIndexes();
- $indexes = iterator_to_array($iterator);
- $this->assertCount(2, $indexes);
- $index = $indexes[1];
- $this->assertSame(['foo' => 1], $index->getKey());
- $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
- }
- public function testCreateIndexInvalid()
- {
- $this->setExpectedException('MongoException', 'index specification has no elements');
- $this->getCollection()->createIndex([]);
- }
- public function testCreateIndexTwice()
- {
- $this->getCollection()->createIndex(['foo' => 1]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'note' => 'all indexes already exist',
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
- }
- public function testCreateIndexWithDeprecatedOptions()
- {
- $this->getCollection()->createIndex(['foo' => 1], ['w' => 1]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'note' => 'all indexes already exist',
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1]));
- }
- public function testCreateIndexTwiceWithSameName()
- {
- $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'note' => 'all indexes already exist',
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']));
- }
- public function testCreateIndexTwiceWithDifferentName()
- {
- $this->getCollection()->createIndex(['foo' => 1], ['name' => 'test_index']);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'note' => 'all indexes already exist',
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], ['name' => 'index_test']));
- }
- public function testCreateIndexTwiceWithDifferentOrder()
- {
- $this->getCollection()->createIndex(['foo' => 1, 'bar' => 1]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 3,
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['bar' => 1, 'foo' => 1]));
- }
- public function testCreateIndexesWithDifferentOptions()
- {
- $this->setExpectedException('MongoResultException');
- $this->getCollection()->createIndex(['foo' => 1]);
- $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
- }
- /**
- * @dataProvider createIndexIgnoredOptions
- */
- public function testCreateIndexesWithIgnoredOptions($option)
- {
- $this->getCollection()->createIndex(['foo' => 1]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'note' => 'all indexes already exist',
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->createIndex(['foo' => 1], [$option => true]));
- }
- public static function createIndexIgnoredOptions()
- {
- return [
- 'background' => ['background'],
- 'dropDups' => ['dropDups'],
- ];
- }
- public function testCreateIndexWithSameNameAndDifferentOptions()
- {
- $this->setExpectedException('MongoResultException');
- $this->getCollection()->createIndex(['foo' => 1], ['name' => 'foo']);
- $this->getCollection()->createIndex(['bar' => 1], ['name' => 'foo']);
- }
- public function testEnsureIndex()
- {
- $expected = [
- 'createdCollectionAutomatically' => true,
- 'numIndexesBefore' => 1,
- 'numIndexesAfter' => 2,
- 'ok' => 1.0
- ];
- $collection = $this->getCollection();
- $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $indexes = iterator_to_array($newCollection->listIndexes());
- $this->assertCount(2, $indexes);
- $index = $indexes[1];
- $this->assertSame(['bar' => 1], $index->getKey());
- $this->assertTrue($index->isUnique());
- $this->assertSame('mongo-php-adapter.test', $index->getNamespace());
- }
- public function testEnsureIndexAlreadyExists()
- {
- $collection = $this->getCollection();
- $collection->ensureIndex(['bar' => 1], ['unique' => true]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 2,
- 'numIndexesAfter' => 2,
- 'ok' => 1.0,
- 'note' => 'all indexes already exist',
- ];
- $this->assertEquals($expected, $collection->ensureIndex(['bar' => 1], ['unique' => true]));
- }
- public function testEnsureIndexAlreadyExistsWithDifferentOptions()
- {
- $collection = $this->getCollection();
- $collection->ensureIndex(['bar' => 1], ['unique' => true]);
- $this->setExpectedException('MongoResultException', 'Index with name: bar_1 already exists with different options');
- $collection->ensureIndex(['bar' => 1]);
- }
- public function testDeleteIndexUsingIndexName()
- {
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $newCollection->createIndex(['bar' => 1], ['name' => 'bar']);
- $expected = [
- 'nIndexesWas' => 2,
- 'errmsg' => 'index not found with name [bar_1]',
- 'ok' => 0.0,
- 'code' => 27,
- ];
- $this->assertEquals($expected, $this->getCollection()->deleteIndex('bar'));
- $this->assertCount(2, iterator_to_array($newCollection->listIndexes()));
- }
- public function testDeleteIndexUsingField()
- {
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $newCollection->createIndex(['bar' => 1]);
- $expected = [
- 'nIndexesWas' => 2,
- 'ok' => 1.0,
- ];
- $this->assertSame($expected, $this->getCollection()->deleteIndex('bar'));
- $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
- }
- public function testDeleteIndexUsingKeys()
- {
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $newCollection->createIndex(['bar' => 1]);
- $expected = [
- 'nIndexesWas' => 2,
- 'ok' => 1.0,
- ];
- $this->assertSame($expected, $this->getcollection()->deleteIndex(['bar' => 1]));
- $this->assertCount(1, iterator_to_array($newCollection->listIndexes()));
- }
- public function testDeleteIndexes()
- {
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $newCollection->createIndex(['bar' => 1]);
- $expected = [
- 'nIndexesWas' => 2,
- 'msg' => 'non-_id indexes dropped for collection',
- 'ok' => 1.0,
- ];
- $this->assertSame($expected, $this->getcollection()->deleteIndexes());
- $this->assertCount(1, iterator_to_array($newCollection->listIndexes())); // ID index is present by default
- }
- public function testDeleteIndexesForNonExistingCollection()
- {
- $expected = [
- 'ok' => 0.0,
- 'errmsg' => 'ns not found',
- 'code' => 26,
- ];
- $this->assertSame($expected, $this->getcollection('nonExisting')->deleteIndexes());
- }
- public static function dataGetIndexInfo()
- {
- return [
- 'plainIndex' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- ],
- 'fields' => ['foo' => 1],
- 'options' => [],
- ],
- 'uniqueIndex' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- 'unique' => true,
- ],
- 'fields' => ['foo' => 1],
- 'options' => ['unique' => true],
- ],
- 'sparseIndex' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- 'sparse' => true,
- ],
- 'fields' => ['foo' => 1],
- 'options' => ['sparse' => true],
- ],
- 'ttlIndex' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- 'expireAfterSeconds' => 86400,
- ],
- 'fields' => ['foo' => 1],
- 'options' => ['expireAfterSeconds' => 86400],
- ],
- 'textIndex' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => [
- '_fts' => 'text',
- '_ftsx' => 1,
- ],
- 'name' => 'foo_text',
- 'ns' => 'mongo-php-adapter.test',
- 'weights' => [
- 'foo' => 1,
- ],
- 'default_language' => 'english',
- 'language_override' => 'language',
- 'textIndexVersion' => 3,
- ],
- 'fields' => ['foo' => 'text'],
- 'options' => [],
- ],
- 'partialFilterExpression' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- 'partialFilterExpression' => [
- 'bar' => ['$gt' => 1],
- ],
- ],
- 'fields' => ['foo' => 1],
- 'options' => [
- 'partialFilterExpression' => ['bar' => ['$gt' => 1]],
- ],
- ],
- 'geoSpatial' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => '2dsphere'],
- 'name' => 'foo_2dsphere',
- 'ns' => 'mongo-php-adapter.test',
- '2dsphereIndexVersion' => 3,
- ],
- 'fields' => ['foo' => '2dsphere'],
- 'options' => [],
- ],
- 'geoHaystack' => [
- 'expectedIndex' => [
- 'v' => 1,
- 'key' => ['foo' => 'geoHaystack', 'bar' => 1],
- 'name' => 'foo_geoHaystack_bar_1',
- 'ns' => 'mongo-php-adapter.test',
- 'bucketSize' => 10,
- ],
- 'fields' => ['foo' => 'geoHaystack', 'bar' => 1],
- 'options' => ['bucketSize' => 10],
- ],
- ];
- }
- /**
- * @dataProvider dataGetIndexInfo
- */
- public function testGetIndexInfo($expectedIndex, $fields, $options)
- {
- $idIndex = [
- 'v' => 1,
- 'key' => ['_id' => 1],
- 'name' => '_id_',
- 'ns' => 'mongo-php-adapter.test',
- ];
- $expectedIndexInfo = [$idIndex, $expectedIndex];
- $collection = $this->getCollection();
- $collection->createIndex($fields, $options);
- $this->assertEquals(
- $expectedIndexInfo,
- $collection->getIndexInfo()
- );
- }
- public function testFindAndModifyUpdate()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- ['$set' => ['foo' => 'foo']]
- );
- $this->assertSame('bar', $document['foo']);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeSame('foo', 'foo', $object);
- }
- public function testFindAndModifyUpdateWithUpdateOptions()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- [],
- [],
- [
- 'update' => ['bar' => 'foo']
- ]
- );
- $this->assertSame('bar', $document['foo']);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeSame('foo', 'bar', $object);
- $this->assertObjectNotHasAttribute('foo', $object);
- }
- public function testFindAndModifyWithUpdateParamAndOption()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $data = ['foo' => 'foo', 'bar' => 'bar'];
- $this->getCollection()->findAndModify(
- ['_id' => new \MongoId($id)],
- [$data],
- [],
- [
- 'update' => ['$set' => ['foo' => 'foobar']],
- 'upsert' => true,
- ]
- );
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeSame('foobar', 'foo', $object);
- $this->assertObjectNotHasAttribute('bar', $object);
- }
- public function testFindAndModifyUpdateReplace()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- ['_id' => new \MongoId($id), 'foo' => 'boo']
- );
- $this->assertSame('bar', $document['foo']);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- $object = $newCollection->findOne();
- $this->assertNotNull($object);
- $this->assertAttributeSame('boo', 'foo', $object);
- $this->assertObjectNotHasAttribute('bar', $object);
- }
- public function testFindAndModifyUpdateReturnNew()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- ['$set' => ['foo' => 'foo']],
- null,
- ['new' => true]
- );
- $this->assertSame('foo', $document['foo']);
- }
- public function testFindAndModifyWithFields()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = [
- '_id' => new \MongoId($id),
- 'foo' => 'bar',
- 'bar' => 'foo',
- ];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- ['$set' => ['foo' => 'foo']],
- ['foo' => true]
- );
- $this->assertArrayNotHasKey('bar', $document);
- $this->assertArrayHasKey('foo', $document);
- }
- public function testGroup()
- {
- $collection = $this->getCollection();
- $document1 = ['a' => 2];
- $collection->insert($document1);
- $document2 = ['b' => 5];
- $collection->insert($document2);
- $document3 = ['a' => 1];
- $collection->insert($document3);
- $keys = [];
- $initial = ["count" => 0];
- $reduce = "function (obj, prev) { prev.count++; }";
- $condition = ['condition' => ["a" => [ '$gt' => 1]]];
- $result = $collection->group($keys, $initial, $reduce, $condition);
- $this->assertArraySubset(
- [
- 'retval' => [['count' => 1.0]],
- 'count' => 1.0,
- 'keys' => 1,
- 'ok' => 1.0,
- ],
- $result
- );
- }
- public function testMapReduce()
- {
- $data = array(
- array(
- 'username' => 'jones',
- 'likes' => 20.0,
- 'text' => 'Hello world!'
- ),
- array(
- 'username' => 'bob',
- 'likes' => 100.0,
- 'text' => 'Hello world!'
- ),
- array(
- 'username' => 'bob',
- 'likes' => 100.0,
- 'text' => 'Hello world!'
- ),
- );
- $collection = $this->getCollection();
- $collection->batchInsert($data);
- $map = 'function() {
- emit(this.username, { count: 1, likes: this.likes });
- }';
- $reduce = 'function(key, values) {
- var result = {count: 0, likes: 0};
- values.forEach(function(value) {
- result.count += value.count;
- result.likes += value.likes;
- });
- return result;
- }';
- $finalize = 'function (key, value) { value.test = "test"; return value; }';
- $command = [
- 'mapreduce' => $this->getCollection()->getName(),
- 'map' => new \MongoCode($map),
- 'reduce' => new \MongoCode($reduce),
- 'query' => (object) [],
- 'out' => ['inline' => true],
- 'finalize' => new \MongoCode($finalize),
- ];
- $result = $this->getDatabase()->command($command);
- $expected = [
- [
- '_id' => 'bob',
- 'value' => [
- 'count' => 2.0,
- 'likes' => 200.0,
- 'test' => 'test',
- ],
- ],
- [
- '_id' => 'jones',
- 'value' => [
- 'count' => 1.0,
- 'likes' => 20.0,
- 'test' => 'test',
- ],
- ],
- ];
- $this->assertSame(1.0, $result['ok']);
- $this->assertSame($expected, $result['results']);
- }
- public function testFindAndModifyResultException()
- {
- $this->markTestSkipped('Test fails on travis-ci - skipped while investigating this');
- $collection = $this->getCollection();
- $this->setExpectedException('MongoResultException');
- $collection->findAndModify(
- array("inprogress" => false, "name" => "Next promo"),
- array('$unsupportedOperator' => array("tasks" => -1)),
- array("tasks" => true),
- array("new" => true)
- );
- }
- public function testFindAndModifyExceptionTimeout()
- {
- $this->failMaxTimeMS();
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $this->setExpectedException('MongoExecutionTimeoutException');
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- null,
- null,
- ['maxTimeMS' => 1, 'remove' => true]
- );
- }
- public function testFindAndModifyRemove()
- {
- $id = '54203e08d51d4a1f868b456e';
- $collection = $this->getCollection();
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $collection->insert($document);
- $document = $collection->findAndModify(
- ['_id' => new \MongoId($id)],
- null,
- null,
- ['remove' => true]
- );
- $this->assertEquals('bar', $document['foo']);
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(0, $newCollection->count());
- }
- public function testValidate()
- {
- $collection = $this->getCollection();
- $document = ['foo' => 'bar'];
- $collection->insert($document);
- $result = $collection->validate();
- $this->assertArraySubset(
- [
- 'ns' => 'mongo-php-adapter.test',
- 'nrecords' => 1,
- 'nIndexes' => 1,
- 'keysPerIndex' => ['mongo-php-adapter.test.$_id_' => 1],
- 'valid' => true,
- 'errors' => [],
- ],
- $result
- );
- }
- public function testDrop()
- {
- $document = ['foo' => 'bar'];
- $this->getCollection()->insert($document);
- $expected = [
- 'ns' => (string) $this->getCollection(),
- 'nIndexesWas' => 1,
- 'ok' => 1.0
- ];
- $this->assertSame($expected, $this->getCollection()->drop());
- }
- public function testEmptyCollectionName()
- {
- $this->setExpectedException('Exception', 'Collection name cannot be empty');
- new \MongoCollection($this->getDatabase(), '');
- }
- public function testSelectCollectionWithNullBytes()
- {
- $this->setExpectedException('Exception', 'Collection name cannot contain null bytes');
- new \MongoCollection($this->getDatabase(), 'foo' . chr(0));
- }
- public function testSubCollectionWithNullBytes()
- {
- $collection = $this->getCollection();
- $this->assertInstanceOf('MongoCollection', $collection->{'foo' . chr(0)});
- $this->assertSame('test', $collection->getName());
- }
- public function testSelectCollectionWithDatabaseObject()
- {
- $client = $this->getClient();
- $database = $this->getDatabase($client);
- $collection = $client->selectCollection($database, 'test');
- $this->assertSame('mongo-php-adapter.test', (string) $collection);
- }
- public function testHasNextLoop()
- {
- $collection = $this->getCollection();
- for ($i = 0; $i < 5; $i++) {
- $document = ['i' => $i];
- $collection->insert($document);
- }
- $cursor = $collection->find()->sort(['i' => 1]);
- $data = [];
- $i = 0;
- while ($cursor->hasNext()) {
- $this->assertSame($i < 5, $cursor->hasNext());
- $row = $cursor->getNext();
- $this->assertSame($i, $row['i']);
- $data[] = $row;
- $i++;
- }
- $this->assertCount(5, $data);
- }
- public function testProjectionWithBSONTypes()
- {
- $collection = $this->getCollection();
- $id = new \MongoId();
- $referencedId = new \MongoId();
- $data = [
- '_id' => $id,
- 'loveItems' => [
- [
- 'sellable' => [
- '$ref' => 'sellables',
- '$id' => $referencedId,
- ]
- ],
- [
- 'sellable' => [
- '$ref' => 'sellables',
- '$id' => new \MongoId(),
- ]
- ]
- ]
- ];
- $collection->insert($data);
- $item = $collection->findOne(
- ['_id' => $id],
- ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
- );
- $this->assertArrayHasKey('loveItems', $item);
- $this->assertCount(1, $item['loveItems']);
- $cursor = $collection->find(
- ['_id' => $id],
- ['loveItems' => ['$elemMatch' => ['sellable.$id' => $referencedId]]]
- );
- $items = iterator_to_array($cursor, false);
- $this->assertCount(1, $items);
- $this->assertCount(1, $items[0]['loveItems']);
- }
- public static function dataFindWithRegex()
- {
- return [
- 'MongoRegex' => [new \MongoRegex('/^foo.*/i')],
- 'BSONRegex' => [new Regex('^foo.*', 'i')],
- ];
- }
- /**
- * @dataProvider dataFindWithRegex
- */
- public function testFindWithRegex($regex)
- {
- $this->skipTestIf(extension_loaded('mongo'));
- $document = ['name' => 'FOO 123'];
- $this->getCollection()->insert($document);
- $cursor = $this->getCollection()->find(['name' => $regex]);
- $this->assertSame(1, $cursor->count());
- }
- }
- class PrivatePropertiesStub
- {
- private $foo = 'bar';
- }
|