lundi 1 avril 2019

seeding works fine in laravel artisan command line but not in phpunit test file

I run database seeding using php artisan db:seed everything works fine; but if I put $this->seed('DatabaseSeeder'); in the TestCase.php set up class after refresh database, I ran into issues: foreign key constraint....

This for my mac on valet, php 7

this is my TestCase.php file:

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, RefreshDatabase;

    public function setUp()
    {
        parent::setUp();
        $this->seed('DatabaseSeeder');
    }
}

this is my DatabaseSeeder file:

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();
        Model::reguard();
        $this->call(AccountTableSeeder::class);
        $this->call(UserTableSeeder::class);
        $this->call(SampleTableSeeder::class);
        $this->call(AdminUsersTableSeeder::class);
        $this->call(RolesTableSeeder::class);
        $this->call(RoleUserTableSeeder::class);
        $this->call(ReportConfigTableSeeder::class);
        $this->call(ReportSnpInfoTableSeeder::class);
        $this->call(ReportSnpGenotypeTableSeeder::class);
        $this->call(QaCategoryTableSeeder::class);
        $this->call(QaQuestionaireTableSeeder::class);
        $this->call(QaQuestionaireFilterTableSeeder::class);
        $this->call(QaQuestionTableSeeder::class);
        $this->call(QaAnswerTableSeeder::class);
        $this->call(PromoCodesTableSeeder::class);
        $this->call(ProductCategoryTableSeeder::class);
        $this->call(ProductTableSeeder::class);
        $this->call(LocationsTableSeeder::class);
        $this->call(AgentListTableSeeder::class);
        $this->call(AdminUserProfilesTableSeeder::class);
    }
}

It works perfectly fine using command line; but errors as below if I run phpunit:

Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`mycodon`.`qa_question`, CONSTRAINT `qa_question_questionaire_id_foreign` FOREIGN KEY (`questionaire_id`) REFERENCES `qa_questionaire` (`questionaire_id`))

If I disable the foreign key constraint of every migration file, it works ofcourse.

I have no idea why this happen...

I expect as long as it works fine in command line, this show also work in file.

Aucun commentaire:

Enregistrer un commentaire