Testomatio - Test Management for Codeception
Access a database.
The most important function of this module is to clean a database before each test. This module also provides actions to perform checks in a database, e.g. 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.
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.
Supported and tested databases are:
Also available:
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.
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;â
modules:
enabled:
- Db:
dsn: 'mysql:host=localhost;dbname=testdb'
user: 'root'
password: ''
dump:
- 'tests/_data/dump.sql'
- 'tests/_data/dump-2.sql'
modules:
enabled:
- Db:
dsn: 'mysql:host=localhost;dbname=testdb'
user: 'root'
password: ''
databases:
db2:
dsn: 'mysql:host=localhost;dbname=testdb2'
user: 'userdb2'
password: ''
modules:
enabled:
- Db:
dsn: 'sqlite:relative/path/to/sqlite-database.db'
user: ''
password: ''
There are two ways of loading the dump into your database:
The recommended approach is to configure a populator
, an external command to load a dump. Command parameters like host, username, password, database
can be obtained from the config and inserted into placeholders:
For MySQL:
For PostgreSQL (using pg_restore)
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
keyword with the full value inside it.
PDO dsn elements for the supported drivers:
Db module by itself can load SQL dump without external tools by using current database connection. This approach is system-independent, however, it is slower than using a populator and may have parsing issues (see below).
Provide a path to SQL file in dump
config option:
To parse SQL Db file, it should follow this specification:
dump.sql
may contain multiline statements.seeInDatabase
, dontSeeInDatabase
, seeNumRecords
, grabFromDatabase
and grabNumRecords
methods
accept arrays as criteria. WHERE condition is generated using item key as a field name and
item value as a field value.
Example:
Will generate:
Since version 2.1.9 itâs possible to use LIKE in a condition, as shown here:
Will generate:
Null comparisons are also available, as shown here:
Will generate:
param string
$databaseKeythrows ModuleConfigException
return void
Make sure you are connected to the right database.
param string
$tableparam array
$criteriareturn void
Effect is opposite to ->seeInDatabase
Asserts that there is no record with the given column values in a database. Provide table name and column values.
Fails if such user was found.
Comparison expressions can be used as well:
Supported operators: <
, >
, >=
, <=
, !=
, like
.
param string
$tableparam string
$columnparam array
$criteriareturn array
Fetches all values from the column in database.
Provide table name, desired column and criteria.
param string
$tableparam array
$criteriathrows PDOException|Exception
return array<array<string,
mixed» Returns an array of all matched rowsFetches a set of entries from a database.
Provide table name and criteria.
Comparison expressions can be used as well:
Supported operators: <
, >
, >=
, <=
, !=
, like
.
param string
$tableparam array
$criteriathrows PDOException|Exception
return array
Returns a single entry valueFetches a whole entry from a database.
Make the test fail if the entry is not found. Provide table name, desired column and criteria.
Comparison expressions can be used as well:
Supported operators: <
, >
, >=
, <=
, !=
, like
.
param string
$tableparam string
$columnparam array
$criteriareturn mixed
Returns a single column value or falseFetches a single column value from a database.
Provide table name, desired column and criteria.
Comparison expressions can be used as well:
Supported operators: <
, >
, >=
, <=
, !=
, like
.
param string
$table Table nameparam array
$criteria Search criteria [Optional]return int
Returns the number of rows in a database
param string
$tableparam array
$datareturn int
Inserts an SQL record into a database. This record will be erased after the test, unless youâve configured âskip_cleanup_if_failedâ, and the test fails.
param
$databaseKeyparam ActionSequence|array|callable
$actionsthrows ModuleConfigException
return void
Can be used with a callback if you donât want to change the current database in your test.
List of actions can be pragmatically built using Codeception\Util\ActionSequence
:
Alternatively an array can be used:
Choose the syntax you like the most and use it,
Actions executed from array or ActionSequence will print debug output for actions, and adds an action name to exception on failure.
param string
$tableparam array
$criteriareturn void
Asserts that a row with the given column values exists.
Provide table name and column values.
Fails if no such user found.
Comparison expressions can be used as well:
Supported operators: <
, >
, >=
, <=
, !=
, like
.
param int
$expectedNumber Expected numberparam string
$table Table nameparam array
$criteria Search criteria [Optional]return void
Asserts that the given number of records were found in the database.
param string
$tableparam array
$dataparam array
$criteriareturn void
Update an SQL record into a database.