Answer the question
In order to leave comments, you need to log in
How to clean up the database after / before the start of functional tests?
Hello!
I'm testing sambit forms. If everything is in order, a record should appear in the database. It is important for me to make sure that this is a new entry, and not an old one.
When preparing a base for tests, I like the option with fixtures, because they are removed after the tests and because they can be used in both functional and unit tests. I don't like the option of loading a clean dump before tests, as it conflicts with the idea of applying migrations, and I don't want to prepare it after every change in the database.
Actually the question is: is there something similar in codeception cleanDir from Filesystem Module , only that it would be cleanTable? It would be desirable to clean the table before the beginning of the test, but only it. The option of re-uploading a clean dump every time seems redundant to me.
Answer the question
In order to leave comments, you need to log in
I thought of doing this...
namespace Codeception\Module;
class DbHelper extends Yii2
{
/**
* Clean up a database table. This will execute 'DELETE FROM <table>' query
* @param string $table table name
* @return bool
*/
public function truncateTable($table)
{
$db = \Yii::$app->db;
if (! $db) {
throw new \LogicException(__CLASS__.' No connection to database.');
}
$query = 'DELETE FROM '.$table;
$this->debugSection('Query', $query);
$command = $db->createCommand($query);
return $command->execute();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question