Skip to content

Latest commit

 

History

History
223 lines (139 loc) · 5.57 KB

File metadata and controls

223 lines (139 loc) · 5.57 KB
layout title
doc
MongoDb - Codeception - Documentation

MongoDb

Installation

{% highlight yaml %} composer require --dev codeception/module-mongodb

{% endhighlight %}

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 {% highlight yaml %} mongodump {% endhighlight %} tool or it's {% highlight yaml %} .tar.gz {% endhighlight %} archive (not available for Windows systems), generated by {% highlight yaml %} tar -czf <archive_file_name>.tar.gz <path_to dump directory> {% endhighlight %}. Just put it in {% highlight yaml %} tests/_data {% endhighlight %} 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

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

  • param string $collection
  • param array $criteria
  • return void

Checks if collection doesn't contain an item.

{% highlight php %}

dontSeeInCollection('users', ['name' => 'miles']); {% endhighlight %} #### grabCollectionCount * `param string` $collection * `param array` $criteria * `return int` Grabs the documents count from a collection {% highlight php %} grabCollectionCount('users'); // or $count = $I->grabCollectionCount('users', ['isAdmin' => true]); {% endhighlight %} #### grabFromCollection * `param string` $collection * `param array` $criteria * `return object|array|null` Grabs a data from collection {% highlight php %} grabFromCollection('users', ['name' => 'miles']); {% endhighlight %} #### haveInCollection * `param string` $collection * `param array` $data * `return string` Inserts data into collection {% highlight php %} haveInCollection('users', ['name' => 'John', 'email' => 'john@coltrane.com']); $user_id = $I->haveInCollection('users', ['email' => 'john@coltrane.com']); {% endhighlight %} #### seeElementIsArray * `param string` $collection * `param array` $criteria * `param string` $elementToCheck * `return void` Asserts that an element in a collection exists and is an Array {% highlight php %} seeElementIsArray('users', ['name' => 'John Doe'], 'data.skills'); {% endhighlight %} #### seeElementIsObject * `param string` $collection * `param array` $criteria * `param string` $elementToCheck * `return void` Asserts that an element in a collection exists and is an Object {% highlight php %} seeElementIsObject('users', ['name' => 'John Doe'], 'data'); {% endhighlight %} #### seeInCollection * `param string` $collection * `param array` $criteria * `return void` Checks if collection contains an item. {% highlight php %} seeInCollection('users', ['name' => 'miles']); {% endhighlight %} #### seeNumElementsInCollection * `param string` $collection * `param int` $expected * `param array` $criteria * `return void` Count number of records in a collection {% highlight php %} seeNumElementsInCollection('users', 2); $I->seeNumElementsInCollection('users', 1, ['name' => 'miles']); {% endhighlight %} #### useDatabase * `param string` $dbName * `return void` Specify the database to use {% highlight php %} useDatabase('db_1'); {% endhighlight %}

 

Module reference is taken from the source code. Help us to improve documentation. Edit module reference