|
| 1 | +--- |
| 2 | +layout: doc |
| 3 | +title: Db Module - Codeception - Documentation |
| 4 | +--- |
| 5 | + |
| 6 | +# Db Module |
| 7 | +**For additional reference, please review the [source](https://github.com/Codeception/Codeception/tree/master/src/Codeception/Module/Db.php)** |
| 8 | + |
| 9 | + |
| 10 | +Works with SQL database. |
| 11 | + |
| 12 | +The most important function of this module is cleaning database before each test. |
| 13 | +That's why this module was added into global configuration file: codeception.yml. |
| 14 | +To have your database properly cleaned you should configure it to access the database. |
| 15 | +Also provides actions to perform checks in database. |
| 16 | + |
| 17 | +In order to have your database populated with data you need a raw SQL dump. |
| 18 | +Just put it in {% highlight yaml %} |
| 19 | + tests/_data |
| 20 | +{% endhighlight %} dir (by default) and specify path to it in config. |
| 21 | +Next time after database is cleared all your data will be restored from dump. |
| 22 | +Don't forget to include CREATE TABLE statements into it. |
| 23 | + |
| 24 | +Supported and tested databases are: |
| 25 | + |
| 26 | +* MySQL |
| 27 | +* SQLite (only file) |
| 28 | +* PostgreSQL |
| 29 | + |
| 30 | +Supported but not tested. |
| 31 | + |
| 32 | +* MSSQL |
| 33 | +* Oracle |
| 34 | + |
| 35 | +Connection is done by database Drivers, which are stored in Codeception\Util\Driver namespace. |
| 36 | +Check out drivers if you get problems loading dumps and cleaning databases. |
| 37 | + |
| 38 | +### Status |
| 39 | + |
| 40 | +* Maintainer: **davert** |
| 41 | +* stability: |
| 42 | + - Mysql: **stable** |
| 43 | + - SQLite: **stable** |
| 44 | + - Postgres: **beta** |
| 45 | + - MSSQL: **alpha** |
| 46 | + - Oracle: **alpha** |
| 47 | +* Contact: codecept@davert.mail.ua |
| 48 | + |
| 49 | +*Please review the code of non-stable modules and provide patches if you have issues.* |
| 50 | + |
| 51 | +### Config |
| 52 | + |
| 53 | +* dsn *required* - PDO DSN |
| 54 | +* user *required* - user to access database |
| 55 | +* password *required* - password |
| 56 | +* dump - path to database dump. |
| 57 | +* populate: true - should the dump be loaded before test suite is started. |
| 58 | +* cleanup: true - should the dump be reloaded after each test |
| 59 | + |
| 60 | +#### Example |
| 61 | + |
| 62 | + modules: |
| 63 | + enabled: [Db] |
| 64 | + config: |
| 65 | + Db: |
| 66 | + dsn: 'mysql:host=localhost;dbname=testdb' |
| 67 | + user: 'root' |
| 68 | + password: '' |
| 69 | + dump: 'tests/_data/dump.sql' |
| 70 | + populate: true |
| 71 | + cleanup: false |
| 72 | + |
| 73 | +### Public Properties |
| 74 | +* dbh - contains PDO connection. |
| 75 | +* driver - contains Connection Driver. See [list all available drivers](https://github.com/Codeception/Codeception/tree/master/src/Codeception/Util/Driver) |
| 76 | + |
| 77 | + |
| 78 | +### Actions |
| 79 | + |
| 80 | + |
| 81 | +#### dontSeeInDatabase |
| 82 | + |
| 83 | + |
| 84 | +Effect is opposite to ->seeInDatabase |
| 85 | + |
| 86 | +Checks if there is no record with such column values in database. |
| 87 | +Provide table name and column values. |
| 88 | + |
| 89 | +Example: |
| 90 | + |
| 91 | +{% highlight php %} |
| 92 | + |
| 93 | +<?php |
| 94 | +$I->dontSeeInDatabase('users', array('name' => 'Davert', 'email' => 'davert@mail.com')); |
| 95 | + |
| 96 | + |
| 97 | +{% endhighlight %} |
| 98 | +Will generate: |
| 99 | + |
| 100 | +{% highlight yaml %} |
| 101 | + sql |
| 102 | +SELECT COUNT(*) FROM `users` WHERE `name` = 'Davert' AND `email` = 'davert@mail.com' |
| 103 | + |
| 104 | +{% endhighlight %} |
| 105 | +Fails if such user was found. |
| 106 | + |
| 107 | + * param $table |
| 108 | + * param array $criteria |
| 109 | + |
| 110 | + |
| 111 | +#### getName |
| 112 | + |
| 113 | +__not documented__ |
| 114 | + |
| 115 | + |
| 116 | +#### grabFromDatabase |
| 117 | + |
| 118 | + |
| 119 | +Fetches a single column value from a database. |
| 120 | +Provide table name, desired column and criteria. |
| 121 | + |
| 122 | +Example: |
| 123 | + |
| 124 | +{% highlight php %} |
| 125 | + |
| 126 | +<?php |
| 127 | +$mail = $I->grabFromDatabase('users', 'email', array('name' => 'Davert')); |
| 128 | + |
| 129 | + |
| 130 | +{% endhighlight %} |
| 131 | + |
| 132 | + * version 1.1 |
| 133 | + * param $table |
| 134 | + * param $column |
| 135 | + * param array $criteria |
| 136 | + * return mixed |
| 137 | + |
| 138 | + |
| 139 | +#### haveInDatabase |
| 140 | + |
| 141 | + |
| 142 | +Inserts SQL record into database. This record will be erased after the test. |
| 143 | + |
| 144 | +{% highlight php %} |
| 145 | + |
| 146 | +<?php |
| 147 | +$I->haveInDatabase('users', array('name' => 'miles', 'email' => 'miles@davis.com')); |
| 148 | +?> |
| 149 | + |
| 150 | +{% endhighlight %} |
| 151 | + |
| 152 | + * param $table |
| 153 | + * param array $data |
| 154 | + * return integer $id |
| 155 | + |
| 156 | + |
| 157 | +#### seeInDatabase |
| 158 | + |
| 159 | + |
| 160 | +Checks if a row with given column values exists. |
| 161 | +Provide table name and column values. |
| 162 | + |
| 163 | +Example: |
| 164 | + |
| 165 | +{% highlight php %} |
| 166 | + |
| 167 | +<?php |
| 168 | +$I->seeInDatabase('users', array('name' => 'Davert', 'email' => 'davert@mail.com')); |
| 169 | + |
| 170 | + |
| 171 | +{% endhighlight %} |
| 172 | +Will generate: |
| 173 | + |
| 174 | +{% highlight yaml %} |
| 175 | + sql |
| 176 | +SELECT COUNT(*) FROM `users` WHERE `name` = 'Davert' AND `email` = 'davert@mail.com' |
| 177 | + |
| 178 | +{% endhighlight %} |
| 179 | +Fails if no such user found. |
| 180 | + |
| 181 | + * param $table |
| 182 | + * param array $criteria |
0 commit comments