Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
0.00%
0 / 8
CRAP
0.00%
0 / 77
MongoDb
0.00%
0 / 1
0.00%
0 / 8
462
0.00%
0 / 77
 _initialize()
0.00%
0 / 1
72
0.00%
0 / 27
 _before(\Codeception\TestCase $test)
0.00%
0 / 1
12
0.00%
0 / 7
 _after(\Codeception\TestCase $test)
0.00%
0 / 1
2
0.00%
0 / 4
 cleanup()
0.00%
0 / 1
12
0.00%
0 / 12
 loadDump()
0.00%
0 / 1
12
0.00%
0 / 11
 seeInCollection($collection, $criteria = array()
0.00%
0 / 1
2
0.00%
0 / 6
 dontSeeInCollection($collection, $criteria = array()
0.00%
0 / 1
2
0.00%
0 / 6
 grabFromCollection($collection, $criteria = array()
0.00%
0 / 1
2
0.00%
0 / 4
<?php
namespace Codeception\Module;
/**
* Works with MongoDb database.
*
* The most important function of this module is cleaning database before each test.
* To have your database properly cleaned you should configure it to access the database.
*
* In order to have your database populated with data you need a valid js file with data (of the same style which can be fed up to mongo binary)
* File can be generated by RockMongo export command
* Just put it in ``` tests/_data ``` dir (by default) and specify path to it in config.
* Next time after database is cleared all your data will be restored from dump.
* The DB preparation should as following:
* - clean database
* - system collection system.users should contain the user which will be authenticated while script performs DB operations
*
* Connection is done by MongoDb driver, which is stored in Codeception\Util\Driver namespace.
* Check out the driver if you get problems loading dumps and cleaning databases.
*
* ## Config
*
* * dsn *required* - MongoDb DSN with the db name specified at the end of the host after slash
* * user *required* - user to access database
* * password *required* - password
* * dump - path to database dump
* * populate: true - should the dump be loaded before test suite is started.
* * cleanup: true - should the dump be reloaded after each test
*
* ### Beta Version
*
* Report an issue if this module doesn't work for you.
*/
use \Codeception\Util\Driver\MongoDb as MongoDbDriver;
class MongoDb extends \Codeception\Module
{
/**
* @api
* @var
*/
public $dbh;
/**
* @var
*/
protected $dumpFile;
protected $isDumpFileEmpty = true;
protected $config = array(
'populate' => true,
'cleanup' => true,
'dump' => null);
protected $populated = false;
/**
* @var \Codeception\Util\Driver\MongoDb
*/
public $driver;
protected $requiredFields = array('dsn', 'user', 'password');
public function _initialize()
{
if ($this->config['dump'] && ($this->config['cleanup'] or ($this->config['populate']))) {
if (!file_exists(getcwd() . DIRECTORY_SEPARATOR . $this->config['dump'])) {
throw new \Codeception\Exception\ModuleConfig(__CLASS__, "
File with dump doesn't exist.\n
Please, check path for dump file: " . $this->config['dump']);
}
$this->dumpFile = getcwd() . DIRECTORY_SEPARATOR . $this->config['dump'];
$this->isDumpFileEmpty = false;
$content = file_get_contents($this->dumpFile);
$content = trim(preg_replace('%/\*(?:(?!\*/).)*\*/%s', "", $content));
if (!sizeof(explode("\n", $content))) {
$this->isDumpFileEmpty = true;
}
}
try {
$this->driver = MongoDbDriver::create($this->config['dsn'], $this->config['user'], $this->config['password']);
} catch (\MongoConnectionException $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage() . ' while creating Mongo connection');
}
// starting with loading dump
if ($this->config['populate']) {
$this->cleanup();
$this->loadDump();
$this->populated = true;
}
}
public function _before(\Codeception\TestCase $test)
{
if ($this->config['cleanup'] && !$this->populated) {
$this->cleanup();
$this->loadDump();
}
}
public function _after(\Codeception\TestCase $test)
{
$this->populated = false;
}
protected function cleanup()
{
$dbh = $this->driver->getDbh();
if (!$dbh) {
throw new \Codeception\Exception\ModuleConfig(__CLASS__, "No connection to database. Remove this module from config if you don't need database repopulation");
}
try {
$this->driver->cleanup();
} catch (\Exception $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage());
}
}
protected function loadDump()
{
if ($this->isDumpFileEmpty) {
return;
}
try {
$this->driver->load($this->dumpFile);
} catch (\Exception $e) {
throw new \Codeception\Exception\Module(__CLASS__, $e->getMessage());
}
}
/**
* Checks if collection contains an item.
*
* ``` php
* <?php
* $I->seeInCollection('users', array('name' => 'miles'));
* ```
*
* @param $collection
* @param array $criteria
*/
public function seeInCollection($collection, $criteria = array())
{
$collection = $this->driver->getDbh()->selectCollection($collection);
$res = $collection->count($criteria);
\PHPUnit_Framework_Assert::assertGreaterThan(0, $res);
}
/**
* Checks if collection doesn't contain an item.
*
* ``` php
* <?php
* $I->dontSeeInCollection('users', array('name' => 'miles'));
* ```
*
* @param $collection
* @param array $criteria
*/
public function dontSeeInCollection($collection, $criteria = array())
{
$collection = $this->driver->getDbh()->selectCollection($collection);
$res = $collection->count($criteria);
\PHPUnit_Framework_Assert::assertLessThan(1, $res);
}
/**
* 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);
}