2

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?

1
  • You could use blackhole engine for these tests, if you don't need any data persisted. Commented Nov 13, 2015 at 8:58

1 Answer 1

0

What does your tests/api.suite.yml file look like?

It works for me, mine looks like this:

class_name: ApiTester
modules:
    enabled:
        - Helper\Api
        - Asserts
        - Laravel5:
            environment_file: .env
        - REST:
            depends: Laravel5

Adding this might help - but I didn't need to

- Laravel5:
    cleanup: true
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.