MongoDb
Installation
If you use Codeception installed using composer, install this module with the following command:
composer require --dev codeception/module-mongodb
Alternatively, you can enable MongoDb
module in suite configuration file and run
codecept init upgrade4
This module was bundled with Codeception 2 and 3, but since version 4 it is necessary to install it separately.
Some modules are bundled with PHAR files.
Warning. Using PHAR file and composer in the same project can cause unexpected errors.
Description
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 You can also use directory, generated by
mongodump
tool or it’s
.tar.gz
archive (not available for Windows systems), generated by
tar -czf <archive_file_name>.tar.gz <path_to dump directory>
. 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\Lib\Driver namespace. Check out the driver if you get problems loading dumps and cleaning databases.
HINT: This module can be used with Mongofill library which is Mongo client written in PHP without extension.
Status
- Maintainer: judgedim, davert
- Stability: beta
- Contact: [email protected]
Please review the code of non-stable modules and provide patches if you have issues.
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_type required - type of dump. One of ‘js’ (MongoDb::DUMP_TYPE_JS), ‘mongodump’ (MongoDb::DUMP_TYPE_MONGODUMP) or ‘mongodump-tar-gz’ (MongoDb::DUMP_TYPE_MONGODUMP_TAR_GZ). default: MongoDb::DUMP_TYPE_JS).
- 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. Boolean or ‘dirty’. If cleanup is set to ‘dirty’, the dump is only reloaded if any data has been written to the db during a test. This is checked using the dbHash command.
Actions
dontSeeInCollection
Checks if collection doesn’t contain an item.
<?php $I->dontSeeInCollection('users', array('name' => 'miles'));
-
param
$collection -
param array
$criteria
grabCollectionCount
Grabs the documents count from a collection
<?php $count = $I->grabCollectionCount('users'); // or $count = $I->grabCollectionCount('users', array('isAdmin' => true));
-
param
$collection -
param array
$criteria return integer
grabFromCollection
Grabs a data from collection
<?php $user = $I->grabFromCollection('users', array('name' => 'miles'));
-
param
$collection -
param array
$criteria return array
haveInCollection
Inserts data into collection
<?php $I->haveInCollection('users', array('name' => 'John', 'email' => '[email protected]')); $user_id = $I->haveInCollection('users', array('email' => '[email protected]'));
-
param
$collection -
param array
$data
seeElementIsArray
Asserts that an element in a collection exists and is an Array
<?php $I->seeElementIsArray('users', array('name' => 'John Doe') , 'data.skills');
-
param String
$collection -
param Array
$criteria -
param String
$elementToCheck
seeElementIsObject
Asserts that an element in a collection exists and is an Object
<?php $I->seeElementIsObject('users', array('name' => 'John Doe') , 'data');
-
param String
$collection -
param Array
$criteria -
param String
$elementToCheck
seeInCollection
Checks if collection contains an item.
<?php $I->seeInCollection('users', array('name' => 'miles'));
-
param
$collection -
param array
$criteria
seeNumElementsInCollection
Count number of records in a collection
<?php $I->seeNumElementsInCollection('users', 2); $I->seeNumElementsInCollection('users', 1, array('name' => 'miles'));
-
param
$collection -
param integer
$expected -
param array
$criteria
useDatabase
Specify the database to use
<?php $I->useDatabase('db_1');
-
param
$dbName
© 2011 Michael Bodnarchuk and contributors
Licensed under the MIT License.
https://codeception.com/docs/modules/MongoDb