These are chat archives for Behat/Behat

5th
May 2017
Ciaran McNulty
@ciaranmcnulty
May 05 2017 14:14
Is there something like 'commit this transaction' that you're failing to do?
SamyOteroGlez
@SamyOteroGlez
May 05 2017 14:18
@ciaranmcnulty
I tried using db transactions and without, same result.
SamyOteroGlez
@SamyOteroGlez
May 05 2017 14:23
I read a post, the guy said that the problem is that the environment is no the same. So when you insert data to the db your using a test environment and when you launch the browser you are using a development environment.
Made sense, but in my case, I'm using only one environment and only one db. I do not understand why if I am inserting elements to the db before visit the url, the elements are in the db if I check for them by querying the bd within the test class but the elements do not exist when I try to login, by elements I mean, a user added to the user table
Ciaran McNulty
@ciaranmcnulty
May 05 2017 14:24
can you log in yourself?
using those credentials?
SamyOteroGlez
@SamyOteroGlez
May 05 2017 14:25
only if manually add the user directly to the db or using the app, in that case the user will be there, but nothing happen if I do it in the test class
is like the test is using a diferent db
SamyOteroGlez
@SamyOteroGlez
May 05 2017 17:13

I think I solved the issue.

For some reason DB::beginTransaction() and DB::rollback() functions from Laravel does not work properly with Behat.

Here is my solution

/**
 * @javascript
 * @BeforeFeature
 */
public static function before(BeforeFeatureScope $scope)
{
    putenv('APP_ENV=local');

    $tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();

    DB::statement('SET FOREIGN_KEY_CHECKS = 0');

    foreach ($tableNames as $name) {
        //if you don't want to truncate migrations
        if ($name == 'migrations') {
            continue;
        }
        DB::table($name)->delete();
    }
    DB::statement('SET FOREIGN_KEY_CHECKS = 1');

    Artisan::call('migrate');

    try {
        Artisan::call('db:seed');
    } catch (Exception $e) {
        dump('seeder failed');
    }
}


 /**
 * @javascript
 * @AfterFeature
 */
public static function after(AfterFeatureScope $scope)
{
    putenv('APP_ENV=local');
    Artisan::call('migrate:rollback');

    $tableNames = Schema::getConnection()->getDoctrineSchemaManager()->listTableNames();
    DB::statement('SET FOREIGN_KEY_CHECKS = 0');

    foreach ($tableNames as $name) {
        //if you don't want to truncate migrations
        if ($name == 'migrations') {
            continue;
        }
        DB::table($name)->delete();
        //Schema::dropIfExists($name);
    }

    DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}