Search code examples
phppostgresqllaravel-5codeception

Codeception making weird rollback after api test with Laravel 5.2 & Postgresql


I have an error using codeception with Laravel. Its not making an insertion even though I tested the route in postman and it works, I also debugged the controller to see if it was caught by an exception but it doesnt, and I know its not making the insertion because of the $I->seeInDatabase() method, but the autoincrement of the id in the table does increment. Also it does persist the insertions if I use postman to do the test (with the incremented id if I run the test with codeception prior to postman). This is my test:

    public function RegistrarCorrectamente(ApiTester $I) //200
    {
            $I->wantTo('Registrarse correctamente');
            $I->sendPOST($this->url, [
                'email' => '[email protected]',
                'first_name' => 'Prueba4',
                'last_name' => 'Probador',
                'community_id' => 2,
            ]);
            $I->seeInDatabase('app.users_x_communities', array('email' => '[email protected]'));
            $I->seeInDatabase('app.users', array('first_name' => 'Prueba4'));

            /*$this->checkResponseCode($I, HttpCode::OK);*/
            $I->seeResponseIsJson();
            $I->seeResponseContainsJson(array(
                'estado' => true,
            ));
    }

The seeInDatabase method doesnt see the insertion.

Any clue how can I fix this? Also, this are my yml files and .env:

api.suite.yml:

    class_name: ApiTester
    modules:
        enabled:
            - \Helper\Api
            - Asserts
            - Laravel5
            - Db
            - REST:
                url: http://localhost:8000/api/
                depends: Laravel5
        config:
            Laravel5:
                environment_file: .env.testing

codeception.yml:

    actor: Tester
    paths:
        tests: tests
        log: tests/_output
        data: tests/_data
        support: tests/_support
        envs: tests/_envs
    settings:
        bootstrap: _bootstrap.php
        colors: false
        memory_limit: 1024M
    extensions:
        enabled:
            - Codeception\Extension\RunFailed
    modules:
        config:
            Db:
                dsn: 'pgsql:host=localhost;port=5432;dbname=movers'
                user: 'postgres'
                password: '1234'

.env.testing:

    APP_ENV=testing
    APP_KEY=base64:WRVmtVKixE3/MQ5bcDA3rrEaYqCavwaoleuRkcZtc4w=
    APP_DEBUG=true
    APP_LOG_LEVEL=debug
    APP_URL=http://localhost

    DB_CONNECTION=pgsql
    DB_HOST=127.0.0.1
    DB_PORT=5432
    DB_DATABASE=movers
    DB_USERNAME=postgres
    DB_PASSWORD=1234

    API_VERSION=v1
    API_NAME="Movers API"
    API_PREFIX=api
    API_STANDARDS_TREE=vnd
    API_SUBTYPE=movers
    API_DEBUG=true

    CACHE_DRIVER=file
    SESSION_DRIVER=file
    QUEUE_DRIVER=sync

    REDIS_HOST=127.0.0.1
    REDIS_PASSWORD=null
    REDIS_PORT=6379

To clarify, I already tried with cleanup (true and false) and with populate (true and false).

Thanks in advance!


Solution

  • Laravel5 module runs test inside transaction and then rolls it back:

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

    Db module can't see the record, because it uses a separate database connection.

    Use seeRecord method of Laravel module or disable cleanup.