I am testing my Laravel5 API with Codeception and I have noticed the rollback functionality that comes with Codeception can get a little slow at times because of the huge SQL dump I have provided it with. One solution is to use SQLite to make it faster but I wish to stick with MySQL.
I think that the database transactions feature in Laravel 5 can help in speeding things up. But I am struggling to realize how to use it with codeception and can it be used in the first place too.
<?php
$I = new ApiTester($scenario);
$I->wantTo('create a new comment');
$I->haveHttpHeader('Authorization', 'Bearer ' . file_get_contents('tests/api/token'));
$I->sendPost('comments', [
"document_id" => "2",
"comment_type_id" => "2",
"user_id" => "2",
"body" => "Testing"
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['status' => 'success']);
$I->seeInDatabase('comment', [
"document_id" => "2",
"comment_type_id" => "2",
"user_id" => "2",
"body" => "Testing"
]);
This is a codeception test I have written. Now I tried to add:
\DB::beginTransaction(); // At the start
\DB::rollback(); // At the end
But it did not work.
I even tried to wrap up the whole test in this:
\DB::transaction(function () {
// Whole Test here
});
But even this did not work out. I still see the new comments in the DB after running the test. So what am I doing wrong and how to go around it?
blackholeengine for these tests, if you don't need any data persisted.