示例#1
0
 /**
  * Count number of records in a collection
  *
  * ``` php
  * <?php
  * $I->seeNumElementsInCollection('users', 2);
  * $I->seeNumElementsInCollection('users', 1, array('name' => 'miles'));
  * ```
  *
  * @param $collection
  * @param integer $expected
  * @param array $criteria
  */
 public function seeNumElementsInCollection($collection, $expected, $criteria = [])
 {
     $collection = $this->driver->getDbh()->selectCollection($collection);
     $res = $collection->count($criteria);
     \PHPUnit_Framework_Assert::assertSame($expected, $res);
 }
 protected function setupDriver($dbName, $config)
 {
     if (!empty($config['dump']) && (isset($config['cleanup']) or isset($config['populate']))) {
         if (!file_exists(Configuration::projectDir() . $config['dump'])) {
             throw new \Codeception\Exception\ModuleConfig(__CLASS__, "\n                    File with dump doesn't exist.\n\n                    Please, check path for dump file: " . $config['dump']);
         }
         $dumpFile = Configuration::projectDir() . $config['dump'];
         $content = file_get_contents($dumpFile);
         $content = trim(preg_replace('%/\\*(?:(?!\\*/).)*\\*/%s', "", $content));
         if (sizeof(explode("\n", $content))) {
             $this->dumpFiles[$dbName] = $dumpFile;
         }
     }
     try {
         $this->drivers[$dbName] = MongoDbDriver::create($config['dsn'], $config['user'], $config['password']);
     } catch (\MongoConnectionException $e) {
         throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage() . ' while creating Mongo connection');
     }
     $populate = !isset($config['populate']) || isset($config['populate']) && $config['populate'] == false;
     // starting with loading dump
     if ($populate) {
         $this->cleanup($dbName);
         $this->loadDump($dbName);
         $this->populated[$dbName] = true;
     }
 }
示例#3
0
 /**
  * Grabs a data from collection
  *
  * ``` php
  * <?php
  * $cursor = $I->grabFromCollection('users', array('name' => 'miles'));
  * ```
  *
  * @param $collection
  * @param array $criteria
  * @return \MongoCursor
  */
 public function grabFromCollection($collection, $criteria = array())
 {
     $collection = $this->driver->getDbh()->selectCollection($collection);
     return $collection->findOne($criteria);
 }