**I have the following Test class in Laravel 5.3, as you can see the tearDown() method is empty,I am using sqlite in memory for testing.
now the tests are being passed, but when i delete the empty tearDown() method, it throws a foreign key constraint failed exception,
I am using sqlite for the testing, phpunit.xml configuration is given below.
and I have tried to change the test database to mysql, that time everything works well(even without tearDown()). any idea why the absence of tearDown() method causes the error?
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Database\Eloquent\Model;
use App\Country;
class PhoneTest extends TestCase
{
use DatabaseMigrations;
public function setUp()
{
parent::setUp();
Schema::disableForeignKeyConstraints();
$this->seed(LocationsSeeder::class);
Schema::enableForeignKeyConstraints();
}
public function test_we_can_make_number_international()
{
$phoneService = resolve('Phone');
$international = $phoneService->makeInternational('0507639889', Country::find(1));
return $this->assertEquals($international, '+971507639889');
}
protected function tearDown()
{
//
}
}
Environment : Laravel 5.3, test db: sqlite - inmemory phpunit 4.8.36 php - 7 PHPUnit configuration
<php> <env name="APP_ENV" value="testing"/> <env name="DB_CONNECTION" value="sqlite"/> <env name="DB_DATABASE" value=":memory:"/> <env name="CACHE_DRIVER" value="array"/> <env name="SESSION_DRIVER" value="array"/> <env name="QUEUE_DRIVER" value="sync"/> </php>
EDIT: My migrations on a fresh db works properly, even up and rollback
Since you're using the DatabaseMigrations trait, Laravel tries to execute the migrate:rollback command:
$this->beforeApplicationDestroyed(function () {
$this->artisan('migrate:rollback');
});
And since your migrations are messy (that's why you're turning off FK constraints), it fails.
When you're placing tearDown() method, you're overriding the tearDown() method in the parent Illuminate\Foundation\Testing\TestCase class, so the beforeApplicationDestroyed callback is never executed.
The best way to handle this is to fix down() method in each migration, so each table could be deleted on migrate:rollback command execution. But you also could run migrate:reset from tearDown() method. Or use disableForeignKeyConstraints() to disable FK constraints as you do in the setUp() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With