You can also activate foreign keys based on each test (file) when the tests actually depend on tables with foreign keys.
Here's a sign: (e.g. tests/ForeignKeys.php )
<?php namespace Tests; trait ForeignKeys { public function enableForeignKeys() { $db = app()->make('db'); $db->getSchemaBuilder()->enableForeignKeyConstraints(); } }
don't forget to run the method somewhere in your test installation chain. I added mine as an override to my TestCase: ( tests/TestCase.php )
<?php namespace Tests; abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase { use CreatesApplication; ... protected function setUpTraits() { $uses = parent::setUpTraits(); if (isset($uses[ForeignKeys::class])) { $this->enableForeignKeys(); } } ...
after that you can add it to your tests as follows:
<?php namespace Tests\Feature; use Tests\ForeignKeys; use Tests\TestCase; use Illuminate\Foundation\Testing\DatabaseMigrations; class ExampleFeatureTest extends TestCase { use DatabaseMigrations; use ForeignKeys; ...
Alex man
source share