A Codeception extension to automatically select data from DB based on certain conditions.
Update your codeception.yml
:
extensions:
enabled:
- \Alpha1_501st\CodeceptionDataSelector\DataSelector
config:
dsn: 'mysql:host=localhost;dbname=testdb'
user: 'root'
password: ''
data:
var1:
table: 'comments'
fields:
- content
joins:
users:
- comments.user_id
- users.id
conditions:
users.activated: '1'
This will produce the following SQL query:
SELECT content FROM comments
LEFT JOIN users ON comments.user_id = users.id
WHERE users.activated = 1 LIMIT 1;
In your test classes, do:
$data = \Alpha1_501st\CodeceptionDataSelector\DataFactory::make();
And then you can access the content
field from above via. $data->var1->content
.
To use a custom comparison operator, instead of =
, do e.g.:
conditions:
users.deleted_at:
- 'IS NOT'
- 'NULL'
This produces:
WHERE users.deleted_at IS NOT NULL
To delete old data before every test, do e.g.:
data:
...
deletes:
one:
table: 'users'
conditions:
first_name: "Test"
last_name: "User"
The name for the deletion (e.g. one
) is not used, but should be unique for in deletes
. This example will produce the following SQL:
DELETE FROM users WHERE first_name = "Test" AND last_name = "User";
To reset data that has been modified by a test, do e.g.:
data:
...
updates:
one:
table: 'posts'
sets:
title: '"My Test Post"'
conditions:
user_id: 1
The name for the update (e.g. one
) is not used, but should be unique in updates
. This example will produce the following SQL:
UPDATE users SET title = "My Test Post" WHERE user_id = 1;