Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 80 additions & 93 deletions src/Codeception/Module/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This module also provides actions to perform checks in a database, e.g. [seeInDatabase()](https://codeception.com/docs/modules/Db#seeInDatabase)
*
* In order to have your database populated with data you need a raw SQL dump.
* Simply put the dump in the `tests/_data` directory (by default) and specify the path in the config.
* Simply put the dump in the `tests/Support/Data` directory (by default) and specify the path in the config.
* The next time after the database is cleared, all your data will be restored from the dump.
* Don't forget to include `CREATE TABLE` statements in the dump.
*
Expand All @@ -41,85 +41,72 @@
* * MS SQL
* * Oracle
*
* Connection is done by database Drivers, which are stored in the `Codeception\Lib\Driver` namespace.
* [Check out the drivers](https://github.com/Codeception/Codeception/tree/2.4/src/Codeception/Lib/Driver)
* if you run into problems loading dumps and cleaning databases.
* Connection is done by database drivers, which are stored in the `Codeception\Lib\Driver` namespace.
* Check out the drivers if you run into problems loading dumps and cleaning databases.
*
* ## Config
*
* * dsn *required* - PDO DSN
* * user *required* - username to access database
* * password *required* - password
* * dump - path to database dump
* * populate: false - whether the the dump should be loaded before the test suite is started
* * cleanup: false - whether the dump should be reloaded before each test
* * reconnect: false - whether the module should reconnect to the database before each test
* * waitlock: 0 - wait lock (in seconds) that the database session should use for DDL statements
* * ssl_key - path to the SSL key (MySQL specific, @see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-key)
* * ssl_cert - path to the SSL certificate (MySQL specific, @see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-cert)
* * ssl_ca - path to the SSL certificate authority (MySQL specific, @see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-ca)
* * ssl_verify_server_cert - disables certificate CN verification (MySQL specific, @see https://php.net/manual/de/ref.pdo-mysql.php)
* * ssl_cipher - list of one or more permissible ciphers to use for SSL encryption (MySQL specific, @see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-cipher)
* * databases - include more database configs and switch between them in tests.
* * initial_queries - list of queries to be executed right after connection to the database has been initiated, i.e. creating the database if it does not exist or preparing the database collation
* * skip_cleanup_if_failed - Do not perform the cleanup if the tests failed. If this is used, manual cleanup might be required when re-running
* ## Example
*
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/_data/dump.sql'
* populate: true
* cleanup: true
* reconnect: true
* waitlock: 10
* skip_cleanup_if_failed: true
* ssl_key: '/path/to/client-key.pem'
* ssl_cert: '/path/to/client-cert.pem'
* ssl_ca: '/path/to/ca-cert.pem'
* ssl_verify_server_cert: false
* ssl_cipher: 'AES256-SHA'
* initial_queries:
* - 'CREATE DATABASE IF NOT EXISTS temp_db;'
* - 'USE temp_db;'
* - 'SET NAMES utf8;'
* ## Example `Functional.suite.yml`
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/Support/Data/dump.sql'
* populate: true # whether the dump should be loaded before the test suite is started
* cleanup: true # whether the dump should be reloaded before each test
* reconnect: true # whether the module should reconnect to the database before each test
* waitlock: 10 # wait lock (in seconds) that the database session should use for DDL statements
* databases: # include more database configs and switch between them in tests.
* skip_cleanup_if_failed: true # Do not perform the cleanup if the tests failed. If this is used, manual cleanup might be required when re-running
* ssl_key: '/path/to/client-key.pem' # path to the SSL key (MySQL specific, see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-key)
* ssl_cert: '/path/to/client-cert.pem' # path to the SSL certificate (MySQL specific, see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-cert)
* ssl_ca: '/path/to/ca-cert.pem' # path to the SSL certificate authority (MySQL specific, see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-ssl-ca)
* ssl_verify_server_cert: false # disables certificate CN verification (MySQL specific, see https://php.net/manual/de/ref.pdo-mysql.php)
* ssl_cipher: 'AES256-SHA' # list of one or more permissible ciphers to use for SSL encryption (MySQL specific, see https://php.net/manual/de/ref.pdo-mysql.php#pdo.constants.mysql-attr-cipher)
* initial_queries: # list of queries to be executed right after connection to the database has been initiated, i.e. creating the database if it does not exist or preparing the database collation
* - 'CREATE DATABASE IF NOT EXISTS temp_db;'
* - 'USE temp_db;'
* - 'SET NAMES utf8;'
* ```
*
* ## Example with multi-dumps
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump:
* - 'tests/_data/dump.sql'
* - 'tests/_data/dump-2.sql'
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump:
* - 'tests/Support/Data/dump.sql'
* - 'tests/Support/Data/dump-2.sql'
* ```
*
* ## Example with multi-databases
*
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* databases:
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* databases:
* db2:
* dsn: 'mysql:host=localhost;dbname=testdb2'
* user: 'userdb2'
* password: ''
*
* ## Example with Sqlite
* dsn: 'mysql:host=localhost;dbname=testdb2'
* user: 'userdb2'
* password: ''
* ```
*
* modules:
* enabled:
* - Db:
* dsn: 'sqlite:relative/path/to/sqlite-database.db'
* user: ''
* password: ''
* ## Example with SQLite
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'sqlite:relative/path/to/sqlite-database.db'
* user: ''
* password: ''
* ```
*
* ## SQL data dump
*
Expand All @@ -134,30 +121,30 @@
*
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/_data/dump.sql'
* populate: true # run populator before all tests
* cleanup: true # run populator before each test
* populator: 'mysql -u $user -h $host $dbname < $dump'
* enabled:
* - Db:
* dsn: 'mysql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/Support/Data/dump.sql'
* populate: true # run populator before all tests
* cleanup: true # run populator before each test
* populator: 'mysql -u $user -h $host $dbname < $dump'
* ```
*
* For PostgreSQL (using pg_restore)
* For PostgreSQL (using `pg_restore`)
*
* ```
* ```yaml
* modules:
* enabled:
* - Db:
* dsn: 'pgsql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/_data/db_backup.dump'
* populate: true # run populator before all tests
* cleanup: true # run populator before each test
* populator: 'pg_restore -u $user -h $host -D $dbname < $dump'
* enabled:
* - Db:
* dsn: 'pgsql:host=localhost;dbname=testdb'
* user: 'root'
* password: ''
* dump: 'tests/Support/Data/db_backup.dump'
* populate: true # run populator before all tests
* cleanup: true # run populator before each test
* populator: 'pg_restore -u $user -h $host -D $dbname < $dump'
* ```
*
* Variable names are being taken from config and DSN which has a `keyword=value` format, so you should expect to have a variable named as the
Expand Down