| layout | doc |
|---|---|
| title | 09-Data - Codeception - Documentation |
Tests should not affect each other. That's a rule of thumb. When tests interact with database, they may change data inside it, which would eventually lead to data inconsistency. A test may try to insert a record that has already been inserted, or retrieve a deleted record. To avoid test failures, the database should be brought to its initial state before each test. Codeception has different methods and approaches to get your data cleaned.
This chapter summarizes all of the notices on cleaning ups from the previous chapters and suggests the best strategies of how to choose data storage backends.
When we decide to clean up a database, we should make this cleaning as fast as possible. Tests should always run fast. Rebuilding the database from scratch is not the best way, but might be the only one. In any case, you should use a special test database for testing. Do not ever run tests on development or production database!
Codeception has a Db module, which takes on most of the tasks of database interaction. By default it will try to repopulate the database from a dump and clean it up after each test. This module expects a database dump in SQL format. It's already prepared for configuration in codeception.yml:
{% highlight yaml %}
modules: config: Db: dsn: 'PDO DSN HERE' user: 'root' password: dump: tests/_data/your-dump-name.sql
{% endhighlight %}
After you enable this module in your test suite, it will automatically populate the database from a dump and repopulate it on each test run. These settings can be changed through the populate and cleanup options, which may be set to false.
In acceptance tests your tests are interacting with the application through a web server.This means that the test and the application will work with the same database. You should provide in the Db module the same credentials that your application uses, and then you can access the database for assertions (seeInDatabase actions) and to perform automatic cleanups.
Db module provides actions to create and verify data inside a database.
If you want to create special database record for one test, you can use haveInDatabase method of Db module.
{% highlight php %}
haveInDatabase('posts', [ 'title' => 'Top 10 Testing Frameworks', 'body' => '1. Codeception' ]); $I->amOnPage('/posts'); $I->see('Top 10 Testing Frameworks'); {% endhighlight %} `haveInDatabase` inserts a row with provided values into database. All added records will be deleted in the end of a test. If you want to check that a table record was created use [`seeInDatabase`](http://codeception.com/docs/modules/Db#haveInDatabase) method: {% highlight php %} amOnPage('/posts/1'); $I->fillField('comment', 'This is nice!'); $I->click('Submit'); $I->seeInDatabase('comments', ['body' => 'This is nice!']); {% endhighlight %} Follow the module [reference](http://codeception.com/docs/modules/Db) for other methods you can use for database testing. There are also modules for [MongoDb](http://codeception.com/docs/modules/MongoDb), [Redis](http://codeception.com/docs/modules/Redis), [Memcache](http://codeception.com/docs/modules/Memcache) which act in similar manner. ### Sequence If database cleanup takes to long for you you can follow different strategy: create new data for each test. This way the only problem you may face is duplication of data records. [Sequence](http://codeception.com/docs/modules/Sequence) was created to solve this. It provides `sq()` function which generates unique suffixes for creating data in tests. ## ORM modules Probably your application is using ORM to work with database. In this case Codeception allows you to use methods of ORM to work with database, instead of accessing database directly. This way you can work with models and entities of a domain, and not on tables and rows. By using ORM in functional and integration you can also improve performance of your tests. Instead of cleaning up database after each test ORM module will wrap all the database actions into transactions and rollback it in the end. This way no actual data will be written to the database. This cleanup strategy is enabled by default, you can disable it by setting `cleanup: false` in config of any ORM module. ### ActiveRecord Popular frameworks like Laravel, Yii, and Phalcon include ActiveRecord data layer by default. Because of this tight integration you just need to enable framework module, and use its config for database access. Corresponding framework modules provide similar methods for ORM access: * `haveRecord` * `seeRecord` * `dontSeeRecord` * `grabRecord` They allow to create and check data by model name and field names in model. Here is the example in Laravel {% highlight php %} haveRecord('posts', ['body' => 'My first blogpost', 'user_id' => 1]); $I->amOnPage('/posts/'.$id); $I->see('My first blogpost', 'article'); // check record exists $I->seeRecord('posts', ['id' => $id]); $I->click('Delete'); // record was deleted $I->dontSeeRecord('posts', ['id' => $id]); {% endhighlight %}