|
| 1 | +# MongoDb |
| 2 | + |
| 3 | + |
| 4 | +Works with MongoDb database. |
| 5 | + |
| 6 | +The most important function of this module is cleaning database before each test. |
| 7 | +To have your database properly cleaned you should configure it to access the database. |
| 8 | + |
| 9 | +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) |
| 10 | +File can be generated by RockMongo export command |
| 11 | +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>```. |
| 12 | +Just put it in ``` tests/_data ``` dir (by default) and specify path to it in config. |
| 13 | +Next time after database is cleared all your data will be restored from dump. |
| 14 | +The DB preparation should as following: |
| 15 | +- clean database |
| 16 | +- system collection system.users should contain the user which will be authenticated while script performs DB operations |
| 17 | + |
| 18 | +Connection is done by MongoDb driver, which is stored in Codeception\Lib\Driver namespace. |
| 19 | +Check out the driver if you get problems loading dumps and cleaning databases. |
| 20 | + |
| 21 | +HINT: This module can be used with [Mongofill](https://github.com/mongofill/mongofill) library which is Mongo client written in PHP without extension. |
| 22 | + |
| 23 | +## Status |
| 24 | + |
| 25 | +* Maintainer: **judgedim**, **davert** |
| 26 | +* Stability: **beta** |
| 27 | +* Contact: davert@codeception.com |
| 28 | + |
| 29 | +*Please review the code of non-stable modules and provide patches if you have issues.* |
| 30 | + |
| 31 | +## Config |
| 32 | + |
| 33 | +* dsn *required* - MongoDb DSN with the db name specified at the end of the host after slash |
| 34 | +* user *required* - user to access database |
| 35 | +* password *required* - password |
| 36 | +* dump_type *required* - type of dump. |
| 37 | + One of 'js' (MongoDb::DUMP_TYPE_JS), 'mongodump' (MongoDb::DUMP_TYPE_MONGODUMP) or 'mongodump-tar-gz' (MongoDb::DUMP_TYPE_MONGODUMP_TAR_GZ). |
| 38 | + default: MongoDb::DUMP_TYPE_JS). |
| 39 | +* dump - path to database dump |
| 40 | +* populate: true - should the dump be loaded before test suite is started. |
| 41 | +* cleanup: true - should the dump be reloaded after each test |
| 42 | + |
| 43 | + |
| 44 | +## Actions |
| 45 | + |
| 46 | +### dontSeeInCollection |
| 47 | + |
| 48 | +Checks if collection doesn't contain an item. |
| 49 | + |
| 50 | +``` php |
| 51 | +<?php |
| 52 | +$I->dontSeeInCollection('users', array('name' => 'miles')); |
| 53 | +``` |
| 54 | + |
| 55 | + * `param` $collection |
| 56 | + * `param array` $criteria |
| 57 | + |
| 58 | + |
| 59 | +### grabCollectionCount |
| 60 | + |
| 61 | +Grabs the documents count from a collection |
| 62 | + |
| 63 | +``` php |
| 64 | +<?php |
| 65 | +$count = $I->grabCollectionCount('users'); |
| 66 | +// or |
| 67 | +$count = $I->grabCollectionCount('users', array('isAdmin' => true)); |
| 68 | +``` |
| 69 | + |
| 70 | + * `param` $collection |
| 71 | + * `param array` $criteria |
| 72 | + * `return` integer |
| 73 | + |
| 74 | + |
| 75 | +### grabFromCollection |
| 76 | + |
| 77 | +Grabs a data from collection |
| 78 | + |
| 79 | +``` php |
| 80 | +<?php |
| 81 | +$user = $I->grabFromCollection('users', array('name' => 'miles')); |
| 82 | +``` |
| 83 | + |
| 84 | + * `param` $collection |
| 85 | + * `param array` $criteria |
| 86 | + * `return` array |
| 87 | + |
| 88 | + |
| 89 | +### haveInCollection |
| 90 | + |
| 91 | +Inserts data into collection |
| 92 | + |
| 93 | +``` php |
| 94 | +<?php |
| 95 | +$I->haveInCollection('users', array('name' => 'John', 'email' => 'john@coltrane.com')); |
| 96 | +$user_id = $I->haveInCollection('users', array('email' => 'john@coltrane.com')); |
| 97 | +``` |
| 98 | + |
| 99 | + * `param` $collection |
| 100 | + * `param array` $data |
| 101 | + |
| 102 | + |
| 103 | +### seeElementIsArray |
| 104 | + |
| 105 | +Asserts that an element in a collection exists and is an Array |
| 106 | + |
| 107 | +``` php |
| 108 | +<?php |
| 109 | +$I->seeElementIsArray('users', array('name' => 'John Doe') , 'data.skills'); |
| 110 | +``` |
| 111 | + |
| 112 | + * `param String` $collection |
| 113 | + * `param Array` $criteria |
| 114 | + * `param String` $elementToCheck |
| 115 | + |
| 116 | + |
| 117 | +### seeElementIsObject |
| 118 | + |
| 119 | +Asserts that an element in a collection exists and is an Object |
| 120 | + |
| 121 | +``` php |
| 122 | +<?php |
| 123 | +$I->seeElementIsObject('users', array('name' => 'John Doe') , 'data'); |
| 124 | +``` |
| 125 | + |
| 126 | + * `param String` $collection |
| 127 | + * `param Array` $criteria |
| 128 | + * `param String` $elementToCheck |
| 129 | + |
| 130 | + |
| 131 | +### seeInCollection |
| 132 | + |
| 133 | +Checks if collection contains an item. |
| 134 | + |
| 135 | +``` php |
| 136 | +<?php |
| 137 | +$I->seeInCollection('users', array('name' => 'miles')); |
| 138 | +``` |
| 139 | + |
| 140 | + * `param` $collection |
| 141 | + * `param array` $criteria |
| 142 | + |
| 143 | + |
| 144 | +### seeNumElementsInCollection |
| 145 | + |
| 146 | +Count number of records in a collection |
| 147 | + |
| 148 | +``` php |
| 149 | +<?php |
| 150 | +$I->seeNumElementsInCollection('users', 2); |
| 151 | +$I->seeNumElementsInCollection('users', 1, array('name' => 'miles')); |
| 152 | +``` |
| 153 | + |
| 154 | + * `param` $collection |
| 155 | + * `param integer` $expected |
| 156 | + * `param array` $criteria |
| 157 | + |
| 158 | + |
| 159 | +### useDatabase |
| 160 | + |
| 161 | +Specify the database to use |
| 162 | + |
| 163 | +``` php |
| 164 | +<?php |
| 165 | +$I->useDatabase('db_1'); |
| 166 | +``` |
| 167 | + |
| 168 | + * `param` $dbName |
| 169 | + |
| 170 | +<p> </p><div class="alert alert-warning">Module reference is taken from the source code. <a href="https://github.com/Codeception/module-mongodb/tree/master/src/Codeception/Module/MongoDb.php">Help us to improve documentation. Edit module reference</a></div> |
0 commit comments