mardi 1 décembre 2015

Database transactions don't work in codeception Laravel 5?

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)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1XG2qPf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire