Skip to content

A Codeception extension to automatically select data from DB based on certain conditions.

License

Notifications You must be signed in to change notification settings

501st-alpha1/codeception-data-selector

Repository files navigation

codeception-data-selector

A Codeception extension to automatically select data from DB based on certain conditions.

Travis CI Coverage Status

Usage

Basic

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.

Custom Operators

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

Delete Old Data

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";

Update Modified Data

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;

About

A Codeception extension to automatically select data from DB based on certain conditions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages