@@ -68,6 +68,9 @@ protected function tearDown(): void
68
68
{
69
69
parent ::tearDown ();
70
70
71
+ Model::$ fillBelongsToRelations = false ;
72
+ EloquentModelStub::$ fillBelongsToRelations = false ;
73
+
71
74
m::close ();
72
75
Carbon::setTestNow (null );
73
76
@@ -3330,6 +3333,89 @@ public function testUseFactoryAttribute()
3330
3333
$ this ->assertEquals (EloquentModelWithUseFactoryAttribute::class, $ factory ->modelName ());
3331
3334
$ this ->assertEquals ('test name ' , $ instance ->name ); // Small smoke test to ensure the factory is working
3332
3335
}
3336
+
3337
+ public function testFillableWithBelongsToRelationDoesntWorkIfDisabledByDefault ()
3338
+ {
3339
+ $ model = new EloquentModelStub ;
3340
+ $ this ->addMockConnection ($ model );
3341
+
3342
+ $ model ->fillable (['name ' , 'age ' ]);
3343
+ $ relation = new EloquentModelSaveStub ();
3344
+ $ relation ->id = 10 ;
3345
+
3346
+ $ model ->fill (['name ' => 'foo ' , 'age ' => 'bar ' , 'belongsToStub ' => $ relation , 'morphToStub ' => $ relation ]);
3347
+
3348
+ $ this ->assertNull ($ model ->belongs_to_stub_id );
3349
+ $ this ->assertNull ($ model ->morph_to_stub_id );
3350
+
3351
+ $ model ->fillable (['name ' , 'age ' , 'belongsToStub ' , 'morphToStub ' ]);
3352
+ $ model ->fill (['name ' => 'foo ' , 'age ' => 'bar ' , 'belongsToStub ' => $ relation , 'morphToStub ' => $ relation ]);
3353
+
3354
+ $ this ->assertSame ('foo ' , $ model ->name );
3355
+ $ this ->assertSame ('bar ' , $ model ->age );
3356
+ $ this ->assertNull ($ model ->belongs_to_stub_id );
3357
+ $ this ->assertNull ($ model ->morph_to_stub_id );
3358
+ $ this ->assertNull ($ model ->morph_to_stub_type );
3359
+ $ this ->assertFalse ($ model ->relationLoaded ('belongsToStub ' ));
3360
+ $ this ->assertFalse ($ model ->relationLoaded ('morphToStub ' ));
3361
+ }
3362
+
3363
+ public function testFillableWithBelongsToRelation ()
3364
+ {
3365
+ EloquentModelStub::$ fillBelongsToRelations = true ;
3366
+
3367
+ $ model = new EloquentModelStub ;
3368
+ $ this ->addMockConnection ($ model );
3369
+
3370
+ $ model ->fillable (['name ' , 'age ' ]);
3371
+ $ relation = new EloquentModelSaveStub ();
3372
+ $ relation ->id = 10 ;
3373
+
3374
+ $ model ->fill (['name ' => 'foo ' , 'age ' => 'bar ' , 'belongsToStub ' => $ relation , 'morphToStub ' => $ relation ]);
3375
+
3376
+ $ this ->assertNull ($ model ->belongs_to_stub_id );
3377
+ $ this ->assertNull ($ model ->morph_to_stub_id );
3378
+
3379
+ $ model ->fillable (['name ' , 'age ' , 'belongsToStub ' , 'morphToStub ' ]);
3380
+ $ model ->fill (['name ' => 'foo ' , 'age ' => 'bar ' , 'belongsToStub ' => $ relation , 'morphToStub ' => $ relation ]);
3381
+
3382
+ $ this ->assertSame ('foo ' , $ model ->name );
3383
+ $ this ->assertSame ('bar ' , $ model ->age );
3384
+ $ this ->assertSame (10 , $ model ->belongs_to_stub_id );
3385
+ $ this ->assertSame (10 , $ model ->morph_to_stub_id );
3386
+ $ this ->assertSame (EloquentModelSaveStub::class, $ model ->morph_to_stub_type );
3387
+ $ this ->assertSAme ($ relation , $ model ->getRelation ('belongsToStub ' ));
3388
+ $ this ->assertSAme ($ relation , $ model ->getRelation ('morphToStub ' ));
3389
+ }
3390
+
3391
+ public function testFillableWithBelongsToRelationNotFillableIfDoesntExist ()
3392
+ {
3393
+ EloquentModelStub::$ fillBelongsToRelations = true ;
3394
+
3395
+ $ model = new EloquentModelStub ;
3396
+ $ this ->addMockConnection ($ model );
3397
+ $ model ->fillable (['name ' , 'foo ' ]);
3398
+
3399
+ $ relation = new EloquentModelSaveStub ();
3400
+
3401
+ $ model ->fill (['foo ' => $ relation ]);
3402
+
3403
+ $ this ->assertNull ($ model ->getRelation ('foo ' ));
3404
+ $ this ->assertSame ($ relation , $ model ->getAttribute ('foo ' ));
3405
+ }
3406
+
3407
+ public function testFillableWithBelongsToRelationNotModelIsSetAsAttribute ()
3408
+ {
3409
+ EloquentModelStub::$ fillBelongsToRelations = true ;
3410
+
3411
+ $ model = new EloquentModelStub ;
3412
+ $ model ->fillable (['name ' , 'age ' , 'belongsToStub ' ]);
3413
+
3414
+ $ model ->fill (['belongsToStub ' => 'foo ' ]);
3415
+
3416
+ $ this ->assertNull ($ model ->getRelation ('belongsToStub ' ));
3417
+ $ this ->assertSame ('foo ' , $ model ->getAttribute ('belongsToStub ' ));
3418
+ }
3333
3419
}
3334
3420
3335
3421
class EloquentTestObserverStub
0 commit comments