| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081 |
- <?php
- namespace Alcaeus\MongoDbAdapter\Tests;
- use MongoDB\Driver\ReadPreference;
- /**
- * @author alcaeus <alcaeus@alcaeus.org>
- */
- class MongoCollectionTest extends TestCase
- {
- 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 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 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->setExpectedException('MongoDuplicateKeyException', 'E11000 duplicate key error index: mongo-php-adapter.test.$_id_');
- $id = new \MongoId();
- $documents = [['_id' => $id, 'foo' => 'bar'], ['_id' => $id, 'foo' => 'bleh']];
- $this->getCollection()->batchInsert($documents);
- }
- 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 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());
- }
- public function testCount()
- {
- $this->prepareData();
- $collection = $this->getCollection();
- $this->assertSame(3, $collection->count());
- $this->assertSame(2, $collection->count(['foo' => 'bar']));
- }
- 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 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 testAggregate()
- {
- $this->skipTestUnless(extension_loaded('mongo'));
- $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 testAggregateTimeoutException()
- {
- $this->skipTestUnless(extension_loaded('mongo'));
- $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 testCreateIndexesWithDifferentOptions()
- {
- $this->setExpectedException('MongoResultException');
- $this->getCollection()->createIndex(['foo' => 1]);
- $this->getCollection()->createIndex(['foo' => 1], ['unique' => true]);
- }
- public function testCreateIndexWithSameName()
- {
- $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 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 testGetIndexInfo()
- {
- $collection = $this->getCollection();
- $collection->createIndex(['foo' => 1]);
- $expected = [
- [
- 'v' => 1,
- 'key' => ['_id' => 1],
- 'name' => '_id_',
- 'ns' => 'mongo-php-adapter.test',
- ],
- [
- 'v' => 1,
- 'key' => ['foo' => 1],
- 'name' => 'foo_1',
- 'ns' => 'mongo-php-adapter.test',
- ],
- ];
- $this->assertSame(
- $expected,
- $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 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 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' => [],
- 'warning' => 'Some checks omitted for speed. use {full:true} option to do more thorough scan.',
- 'ok' => 1.0
- ],
- $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());
- }
- }
- class PrivatePropertiesStub
- {
- private $foo = 'bar';
- }
|