getDatabase() public method

public getDatabase ( ) : Database
return Database
Ejemplo n.º 1
0
 public function testOnAfterConstruct()
 {
     $collectionMock = $this->getMock('\\Sokil\\Mongo\\Collection', array('getDocumentClassName'), array($this->collection->getDatabase(), 'phpmongo_test_collection'));
     $collectionMock->expects($this->once())->method('getDocumentClassName')->will($this->returnValue('\\Sokil\\Mongo\\DocumentWithAfterConstructEvent'));
     $document = $collectionMock->createDocument();
     $this->assertEquals(true, $document->status);
 }
Ejemplo n.º 2
0
 public function testEnsureFulltextIndex()
 {
     try {
         $this->collection->ensureFulltextIndex(array('fieldname1', 'fieldname2'), array('fieldname1' => 1, 'fieldname2' => 2), 'spanish');
     } catch (\MongoWriteConcernException $e) {
         $this->assertEquals('127.0.0.1:27017: text search not enabled', $e->getMessage());
         return;
     }
     $indexes = $this->collection->getIndexes();
     $index = $indexes[1];
     // 'textIndexVersion' differ in different versions of mongodb
     $this->assertArrayHasKey('textIndexVersion', $index);
     $dbVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($dbVersion, '2.6', '<')) {
         $this->assertEquals(1, $index['textIndexVersion']);
     } else {
         if (version_compare($dbVersion, '3.2', '<')) {
             $this->assertEquals(2, $index['textIndexVersion']);
         } else {
             $this->assertEquals(3, $index['textIndexVersion']);
         }
     }
     unset($index['textIndexVersion']);
     // chech other params
     $this->assertEquals($index, array('v' => 1, 'key' => array('_fts' => 'text', '_ftsx' => 1), 'name' => 'fieldname1_text_fieldname2_text', 'ns' => 'test.phpmongo_test_collection', 'default_language' => 'spanish', 'weights' => array('fieldname1' => 1, 'fieldname2' => 2), 'language_override' => 'language'));
 }
Ejemplo n.º 3
0
 public function testExpressionNearPointArrayDistabce()
 {
     // this feature allowed only in MongoDB 2.6
     if (version_compare($this->collection->getDatabase()->getClient()->getDbVersion(), '2.6', '<')) {
         return;
     }
     $this->collection->ensure2dSphereIndex('location');
     $document1Id = $this->collection->createDocument()->setPoint('location', 24.012228, 49.831485)->save()->getId();
     $document2Id = $this->collection->createDocument()->setPoint('location', 34.551416, 49.588264)->save()->getId();
     // point before min-max range
     $document = $this->collection->find()->nearPoint('location', 34.551, 49.588, array(null, 5))->findOne();
     $this->assertEmpty($document);
     // point before min-max range
     $document = $this->collection->find()->nearPoint('location', 34.551, 49.588, array(1, 5))->findOne();
     $this->assertEmpty($document);
     // point in min-max range
     $document = $this->collection->find()->nearPoint('location', 34.551, 49.588, array(5, 50))->findOne();
     $this->assertNotEmpty($document);
     $this->assertEquals($document2Id, $document->getId());
     // point after min-max range
     $document = $this->collection->find()->nearPoint('location', 34.551, 49.588, array(50, 500))->findOne();
     $this->assertEmpty($document);
     // point autside min range
     $document = $this->collection->find()->nearPoint('location', 34.551, 49.588, array(50, null))->findOne();
     $this->assertNotEmpty($document);
     $this->assertEquals($document1Id, $document->getId());
 }
Ejemplo n.º 4
0
 /**
  * Get document by reference
  *
  * @param string    $name   name of field where reference stored
  * @return null|Document
  */
 public function getReferencedDocumentList($name)
 {
     $referenceList = $this->get($name);
     if (null === $referenceList) {
         return null;
     }
     if (!isset($referenceList[0])) {
         throw new Exception('List of references not found');
     }
     // build list of referenced collections and ids
     $documentIdList = array();
     foreach ($referenceList as $reference) {
         if (empty($reference['$ref']) || empty($reference['$id'])) {
             throw new Exception(sprintf('Iinvalid reference in list for document %s in field %s', $this->getId(), $name));
         }
         $documentIdList[$reference['$ref']][] = $reference['$id'];
     }
     // get list
     $documentList = array();
     $database = $this->collection->getDatabase();
     foreach ($documentIdList as $collectionName => $documentIdList) {
         $documentList += $database->getCollection($collectionName)->find()->byIdList($documentIdList)->findAll();
     }
     return $documentList;
 }
Ejemplo n.º 5
0
 /**
  * @expectedException \Sokil\Mongo\Exception
  * @expectedExceptionMessage Wrong pipeline specified
  */
 public function testExplainAggregate_WrongArgument()
 {
     // define db version where aggregate explanation supported
     $clientMock = $this->getMock('\\Sokil\\Mongo\\Client', array('getDbVersion'));
     $clientMock->expects($this->once())->method('getDbVersion')->will($this->returnValue('2.6.0'));
     $clientMock->setMongoClient($this->collection->getDatabase()->getClient()->getMongoClient());
     $this->collection = $clientMock->getDatabase('test')->getCollection('phpmongo_test_collection')->explainAggregate('wrong_argument');
 }
 public function testEnsureUniqueIndex()
 {
     $this->collection->ensureUniqueIndex(array('uniqueAsc' => 1, 'uniqueDesc' => -1), true);
     $indexes = $this->collection->getIndexes();
     /*
        v.2.6.8
        array (
            0 => array (
                'v' => 1,
                'key' => array (
                    '_id' => 1,
                ),
                'name' => '_id_',
                'ns' => 'test.phpmongo_test_collection',
            ),
            1 => array (
                'v' => 1,
                'unique' => true,
                'key' => array (
                    'uniqueAsc' => 1,
                    'uniqueDesc' => -1,
                ),
                'name' => 'uniqueAsc_1_uniqueDesc_-1',
                'ns' => 'test.phpmongo_test_collection',
                'dropDups' => true,
            ),
        )
     
        v.3.0.0
        array(
            0 => array(
                'v' => 1,
                'key' => array (
                    '_id' => 1,
                ),
                'name' => '_id_',
                'ns' => 'test.phpmongo_test_collection',
            ),
            1 => array (
                'v' => 1,
                'unique' => true,
                'key' => array (
                    'uniqueAsc' => 1,
                    'uniqueDesc' => -1,
                ),
                'name' => 'uniqueAsc_1_uniqueDesc_-1',
                'ns' => 'test.phpmongo_test_collection',
            ),
        )
     */
     $this->assertEquals(array('uniqueAsc' => 1, 'uniqueDesc' => -1), $indexes[1]['key']);
     $currentVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($currentVersion, '3', '<')) {
         $this->assertArrayHasKey('dropDups', $indexes[1]);
         $this->assertEquals(1, $indexes[1]['dropDups']);
     }
 }
Ejemplo n.º 7
0
 public function testBehaviorInCursor()
 {
     $db = $this->collection->getDatabase();
     $db->map('col', array('behaviors' => array('get42' => new SomeBehavior())));
     $collection = $db->getCollection('col');
     $collection->insertMultiple(array(array('key' => 'value1'), array('key' => 'value2'), array('key' => 'value3'), array('key' => 'value4'), array('key' => 'value5')), false);
     foreach ($collection->find() as $document) {
         $this->assertEquals(42, $document->return42());
     }
 }
Ejemplo n.º 8
0
 /**
  * @expectedException \Sokil\Mongo\Exception
  * @expectedExceptionMessage Update error: some_strange_error: Some strange error
  */
 public function testSave_UpdateError()
 {
     $mongoCollectionMock = $this->getMock('\\MongoCollection', array('update'), array($this->collection->getDatabase()->getMongoDb(), 'phpmongo_test_collection'));
     $mongoCollectionMock->expects($this->once())->method('update')->will($this->returnValue(array('ok' => (double) 0, 'err' => 'some_strange_error', 'errmsg' => 'Some strange error')));
     $collection = new Collection($this->collection->getDatabase(), $mongoCollectionMock);
     // create document
     $document = $collection->createDocument(array('p' => 'v'))->save();
     // update document with error
     $document->set('p', 'v1')->save();
 }
Ejemplo n.º 9
0
 /**
  * Copy selected documents to another collection
  *
  * @param type $targetCollectionName
  * @param type $targetDatabaseName Target database name. If not specified - use current
  */
 public function copyToCollection($targetCollectionName, $targetDatabaseName = null)
 {
     // target database
     if (!$targetDatabaseName) {
         $database = $this->collection->getDatabase();
     } else {
         $database = $this->client->getDatabase($targetDatabaseName);
     }
     // target collection
     $targetMongoCollection = $database->getCollection($targetCollectionName)->getMongoCollection();
     // cursor
     $cursor = $this->getCursor();
     $batchLimit = 100;
     $inProgress = true;
     // copy data
     while ($inProgress) {
         // get next pack of documents
         $documentList = array();
         for ($i = 0; $i < $batchLimit; $i++) {
             if (!$cursor->valid()) {
                 $inProgress = false;
                 if ($documentList) {
                     // still need batch insert
                     break;
                 } else {
                     // no documents to insert - just exit
                     break 2;
                 }
             }
             $documentList[] = $cursor->current();
             $cursor->next();
         }
         // insert
         $result = $targetMongoCollection->batchInsert($documentList);
         // check result
         if (is_array($result)) {
             if ($result['ok'] != 1) {
                 throw new Exception('Batch insert error: ' . $result['err']);
             }
         } elseif (!$result) {
             throw new Exception('Batch insert error');
         }
     }
     return $this;
 }
Ejemplo n.º 10
0
 public function testEnsureFulltextIndex()
 {
     $this->collection->ensureFulltextIndex(array('fieldname1', 'fieldname2'), array('fieldname1' => 1, 'fieldname2' => 2), 'spanish');
     $indexes = $this->collection->getIndexes();
     $index = $indexes[1];
     // 'textIndexVersion' differ in different versions of mongodb
     $this->assertArrayHasKey('textIndexVersion', $index);
     $dbVersion = $this->collection->getDatabase()->getClient()->getDbVersion();
     if (version_compare($dbVersion, '2.6', '<')) {
         $this->assertEquals(1, $index['textIndexVersion']);
     } else {
         if (version_compare($dbVersion, '3', '<')) {
             $this->assertEquals(2, $index['textIndexVersion']);
         } else {
             $this->assertEquals(3, $index['textIndexVersion']);
         }
     }
     unset($index['textIndexVersion']);
     // chech other params
     $this->assertEquals($index, array('v' => 1, 'key' => array('_fts' => 'text', '_ftsx' => 1), 'name' => 'fieldname1_text_fieldname2_text', 'ns' => 'test.phpmongo_test_collection', 'default_language' => 'spanish', 'weights' => array('fieldname1' => 1, 'fieldname2' => 2), 'language_override' => 'language'));
 }
Ejemplo n.º 11
0
 public function testMoveToCollection()
 {
     $targetCollectionName = 'targetMoveCollection';
     $targetCollection = $this->collection->getDatabase()->getCollection($targetCollectionName)->delete();
     // fill collection with documents
     for ($i = 0; $i < 200; $i++) {
         $this->collection->createDocument(array('i' => $i))->save();
     }
     $this->collection->find()->whereMod('i', 2, 0)->moveToCollection($targetCollectionName);
     // check source collection
     $this->assertEquals(100, $this->collection->count());
     foreach ($this->collection->find() as $document) {
         $this->assertEquals(1, $document->i % 2);
     }
     // check target collection
     $this->assertEquals(100, $targetCollection->count());
     foreach ($targetCollection->find() as $document) {
         $this->assertEquals(0, $document->i % 2);
     }
     // clear
     $targetCollection->delete();
 }