| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067 |
- <?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', 'ongoCollection::insert expects parameter 1 to be an array or object, integer given');
- $document = 8;
- $this->getCollection()->insert($document);
- }
- public function testInsertEmptyArray()
- {
- $this->setExpectedException('MongoException', 'document cannot be empty');
- $document = [];
- $this->getCollection()->insert($document);
- }
- public function testInsertArrayWithNumericKeys()
- {
- $this->setExpectedException('MongoException', 'document contain invalid key');
- $document = [1 => 'foo'];
- $this->getCollection()->insert($document);
- }
- public function testInsertEmptyObject()
- {
- $this->setExpectedException('MongoException', 'document cannot be empty');
- $document = (object) [];
- $this->getCollection()->insert($document);
- }
- public function testInsertObjectWithPrivateProperties()
- {
- $this->setExpectedException('MongoException', 'document contain invalid key');
- $document = $this->getCollection();
- $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->assertArraySubset(
- [
- 'ok' => 0.0,
- 'n' => 0,
- 'err' => 11000,
- ],
- $collection->insert($document)
- );
- }
- public function testUnacknowledgedWrite()
- {
- $document = ['foo' => 'bar'];
- $this->assertTrue($this->getCollection()->insert($document, ['w' => 0]));
- }
- public function testInsertWriteConcernException()
- {
- $this->setExpectedException('MongoConnectionException');
- $document = ['foo' => 'bar'];
- $this->getCollection()->insert($document, ['w' => 2]);
- }
- public function testInsertMany()
- {
- $expected = [
- 'connectionId' => 0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- 'errmsg' => null
- ];
- $documents = [
- ['foo' => 'bar'],
- ['bar' => 'foo']
- ];
- $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
- foreach ($documents as $document) {
- $this->assertInstanceOf('MongoId', $document['_id']);
- }
- }
- public function testInsertManyWithNonNumericKeys()
- {
- $expected = [
- 'connectionId' => 0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- 'errmsg' => null
- ];
- $documents = [
- 'a' => ['foo' => 'bar'],
- 'b' => ['bar' => 'foo']
- ];
- $this->assertSame($expected, $this->getCollection()->batchInsert($documents));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(2, $newCollection->count());
- }
- public function testBatchInsertContinuesOnError()
- {
- $expected = [
- 'connectionId' => 0,
- 'n' => 0,
- 'syncMillis' => 0,
- 'writtenTo' => null,
- 'err' => null,
- 'errmsg' => null
- ];
- $documents = [
- 8,
- 'b' => ['bar' => 'foo']
- ];
- $this->assertSame($expected, $this->getCollection()->batchInsert($documents, ['continueOnError' => true]));
- $newCollection = $this->getCheckDatabase()->selectCollection('test');
- $this->assertSame(1, $newCollection->count());
- }
- public function testBatchInsertException()
- {
- $this->setExpectedException('MongoConnectionException');
- $documents = [['foo' => 'bar']];
- $this->getCollection()->batchInsert($documents, ['w' => 2]);
- }
- 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('MongoConnectionException'); // does not match driver
- $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 testUpdateFail()
- {
- $collection = $this->getCollection();
- $collection->createIndex(['foo' => 1], ['unique' => 1]);
- $document = ['foo' => 'bar'];
- $collection->insert($document);
- $document = ['foo' => 'foo'];
- $collection->insert($document);
- $this->assertArraySubset(
- [
- 'ok' => 0.0,
- 'nModified' => 0,
- 'n' => 0,
- 'err' => 11000,
- 'updatedExisting' => true,
- ],
- $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()
- {
- $client = $this->getClient([], 'mongodb://localhost:28888?connectTimeoutMS=1');
- $collection = $client->selectCollection('mongo-php-adapter', 'test');
- $this->setExpectedException('MongoConnectionException');
- $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()
- {
- $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()
- {
- $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->assertSame(['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->assertSame(['w' => 1, 'wtimeout' => 0], $collection->getWriteConcern());
- $this->assertSame(1, $collection->w);
- $this->assertSame(0, $collection->wtimeout);
- $this->assertTrue($collection->setWriteConcern('majority', 100));
- $this->assertSame(['w' => 'majority', 'wtimeout' => 100], $collection->getWriteConcern());
- $collection->w = 2;
- $this->assertSame(['w' => 2, 'wtimeout' => 100], $collection->getWriteConcern());
- $collection->wtimeout = -1;
- $this->assertSame(['w' => 2, 'wtimeout' => 0], $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();
- $expected = [
- 'ok' => 1.0,
- 'nModified' => 0,
- 'n' => 0,
- 'err' => null,
- 'errmsg' => null,
- 'updatedExisting' => false,
- ];
- $document = ['_id' => new \MongoId($id), 'foo' => 'bar'];
- $this->assertSame($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('MongoCursorException');
- unset($document['_id']);
- $this->assertArraySubset(
- [
- 'ok' => 0.0,
- 'nModified' => 0,
- 'n' => 0,
- 'err' => 11000,
- 'updatedExisting' => true,
- ],
- $collection->save($document)
- );
- }
- public function testSaveEmptyKeys()
- {
- $this->setExpectedException('MongoException');
- $document = [];
- $this->getCollection()->save($document);
- }
- public function testSaveEmptyObject()
- {
- $this->setExpectedException('MongoException');
- $document = (object) [];
- $this->getCollection()->save($document);
- }
- public function testSaveWrite()
- {
- $this->setExpectedException('MongoConnectionException'); // should be MongoCursorException
- $document = ['foo' => 'bar'];
- $this->getCollection()->save($document, ['w' => 2, 'wtimeout' => 1000]);
- }
- 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', 'keys cannot be empty');
- $this->getCollection()->createIndex([]);
- }
- public function testCreateIndexTwice()
- {
- $this->getCollection()->createIndex(['foo' => 1]);
- $expected = [
- 'createdCollectionAutomatically' => false,
- 'numIndexesBefore' => 1,
- 'numIndexesAfter' => 1,
- '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()
- {
- $collection = $this->getCollection();
- $this->assertTrue($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,
- '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 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->setExpectedException('Exception', 'Collection name cannot contain null bytes');
- $collection->{'foo' . chr(0)};
- }
- }
|