1

I am trying to work with database transactions in Laravel 5 using Codeception to test my API. The Laravel 5 module documentation says clearly states in the config section:

cleanup: boolean, default true - all db queries will be run in transaction, which will be rolled back at the end of test.

My codeception.yml file looks like so:

actor: Tester
paths:
    tests: tests
    log: tests/_output
    data: tests/_data
    support: tests/_support
    envs: tests/_envs
settings:
    bootstrap: _bootstrap.php
    colors: true
    memory_limit: 1024M
extensions:
    enabled:
        - Codeception\Extension\RunFailed
modules:
    config:
        Db:
            dsn: 'mysql:host=localhost;dbname=carparts'
            user: 'root'
            password: 'password'
            dump: tests/_data/dump.sql

My api.suite.yml file looks like so:

class_name: ApiTester
modules:
    enabled:
        - Laravel5
        - Db
        - PhpBrowser:
            url: 'http://localhost:8000/api'
            curl:
                CURLOPT_RETURNTRANSFER: true
        - REST:
            url: http://localhost:8000/api/
            depends: Laravel5
    config:
        Laravel5:
            environment_file: .env.testing
        Db:
            cleanup: false
            populate: false

Here I have made the Db module cleanup and populate set to false so that the dump is not used and instead the database transactions from the Laravel5 module are reversing the Db state.

Now here is a simple test:

<?php 

changeToTenantDb('carparts');
$I = new ApiTester($scenario);
$I->wantTo('create a new comment');
$I->useToken();
$I->sendPost('comments', [
    "document_id" => "1",
    "comment_type_id" => "1",
    "user_id" => "1",
    "body" => "Testing"
]);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['status' => 'success']);
$I->seeInDatabase('comment', [
    "document_id" => "1",
    "comment_type_id" => "1",
    "user_id" => "1",
    "body" => "Testing"
]);

It basically persist a comment in the Db and performs basic assertions on the API response and finally checks to see if the record actually did persist in the Db. When I run this, it runs fine and comes back green. But I check the Db and the comment still persists. I run it again, I see another new comment. According to my understand, after the test has finished running, the Db transactions should roll back to the original state. What am I doing wrong?

Moreover if I run the tests like so:

sudo codecept run api CreateCommentCept

It runs fine. But when I run them together:

sudo codecept run api

All tests fail. I am totally confused why this is happening. When I change my REST dependency to PhpBrowser instead of Laravel5 module, this does not happen.

PS: I am using Codeception (2.1.3)

2 Answers 2

1

Here is something that might be of help Laravel5 Module: disable cleanup #2525

here janhenkgerritsen commented

The $config variable of the Laravel 5 module is public, so you can change the cleanup entry in this array yourself by adding a helper. That will work for you now. And I will add the proposed methods to the module when I have some time.

and

After some thinking I decided not to add methods to the module to disable/enable cleanup. The use case is pretty rare in my opinion, and if you do run into it, you can easily change the behavior with a helper.

I recommend you to do in the way janhenkgerritsen mentioned in his comment.I don't recommend editing vendor files but what i did for the time being is

to find vendor/codeception/codeception/src/Codeception/Module/Laravel5.php and edited following code. changed the cleanup to false

$this->config = array_merge(
            [
                'cleanup' => false,
                'environment_file' => '.env',
                'bootstrap' => 'bootstrap' . DIRECTORY_SEPARATOR . 'app.php',
                'root' => '',
                'packages' => 'workbench',
                'disable_middleware' => false,
                'disable_events' => false,
            ],
            (array)$config
        );
Sign up to request clarification or add additional context in comments.

Comments

0

Resolved by adding cleanup: false, here is my integration.suite.yml example:

# Unit Test Suite Configuration
#
actor: UnitTester
modules:
    enabled:
        - Laravel5:
            environment_file: .env.testing
            cleanup: false
        - Asserts
        - Mockery
        - \Helper\Unit
        - \Helper\JsonLoader

and run ./vendor/bin/codecept build after that!

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.