Skip to content

Instantly share code, notes, and snippets.

@nezarfadle
Last active January 20, 2019 18:45
Show Gist options
  • Save nezarfadle/8a04a3a16e3b22f1232e982909c099a2 to your computer and use it in GitHub Desktop.
Save nezarfadle/8a04a3a16e3b22f1232e982909c099a2 to your computer and use it in GitHub Desktop.

File: tests/TestCase.php

use Illuminate\Support\Facades\DB;

public function tearDown()
{

    $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = 'YOUR_DATABASE_NAME';";

    DB::statement("SET FOREIGN_KEY_CHECKS = 0;");
    $tables = DB::select($sql);

    array_walk($tables, function($table){
        if ($table->TABLE_NAME != 'migrations') {
            DB::table($table->TABLE_NAME)->truncate();
        }
    });

    DB::statement("SET FOREIGN_KEY_CHECKS = 1;");
    parent::tearDown();
}
@UnderDogg
Copy link

Why don't you try/catch to truncate function, so If there's an exception, you can catch it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment