Skip to content

Commit

Permalink
Add failing tests for lazy loading prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
rogatty committed Mar 18, 2022
1 parent d05a2d0 commit ed33384
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/Integration/Database/EloquentStrictLoadingTest.php
Expand Up @@ -143,6 +143,21 @@ public function testStrictModeWithOverriddenHandlerOnLazyLoading()

$models[0]->modelTwos;
}

public function testStrictModeDoesntThrowAnExceptionOnManuallyMadeModel()
{
$model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::make();
$model2 = EloquentStrictLoadingTestModel2::make();
$model1->modelTwos->push($model2);

$this->assertInstanceOf(Collection::class, $model1->modelTwos);
}

public function testStrictModeDoesntThrowAnExceptionOnRecentlyCreatedModel()
{
$model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::create();
$this->assertInstanceOf(Collection::class, $model1->modelTwos);
}
}

class EloquentStrictLoadingTestModel1 extends Model
Expand Down Expand Up @@ -174,6 +189,19 @@ protected function handleLazyLoadingViolation($key)
}
}

class EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading extends Model
{
public $table = 'test_model1';
public $timestamps = false;
public $preventsLazyLoading = true;
protected $guarded = [];

public function modelTwos()
{
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
}
}

class EloquentStrictLoadingTestModel2 extends Model
{
public $table = 'test_model2';
Expand Down

0 comments on commit ed33384

Please sign in to comment.