lundi 26 mars 2018

saving relations randomly not working in test scenario

I'm testing saving relations but randomly it does not work, aka no relation is set.

Any idea why it isn't working on random occasions? (maybe 5-10% of all tries) I'm using Laravel 5.5 on PHP 7.1

I'm not sure what other details I should give but stack forces me to write this text...

This is my first model:

public function up()
{
    Schema::create('modelas', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('foreign_key_other_system', false, true);
        $table->timestamps();
    });
}

$factory->define(ModelA::class, function (Faker $faker) {
    return [
        'foreign_key_other_system' => $faker->randomNumber()
    ];
});

class ModelA extends Model
{
     protected $fillable = ['foreign_key_other_system'];

     public function modelbs(): BelongsToMany
     {
        return $this->belongsToMany(ModelB::class);
     }
}

This is the second model:

public function up()
{
    Schema::create('modelbs', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('some_data',false,true);

        $table->unique('some_data');
        $table->timestamps();
    });
}

class ModelB extends Model
{
    public function modelas(): BelongsToMany
    {
        return $this->belongsToMany(ModelA::class);
    }
}

My test looks like this:

class Test extends TestCase
{
    use RefreshDatabase, WithFaker;

    public function setUp()
    {
        parent::setUp();
        $this->seed(\UnittestDatabaseSeeder::class); // <-- inserts 30 predefined ModelB in database
    }

    public function testRelation()
    {
        /** @var ModelA $modelA */
        $modelA = factory(ModelA::class)->create();

        /** @var ModelB $modelB */
        $modelB = ModelB::whereNotIn('some_data', [5, 9])->limit(1)->first();
        $modelA->modelbs()->save($modelB);

        // somewhere deep in code
        $modelA = $something->modelA; // same model as 7 lines up
        $modelBs = $modelA->modelbs;

        if(empty($modelBs) || $modelBs->count() < 1) {
            throw new \RuntimeException('no modelBs found'); // thrown 10% of time
        }

        // ...
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire