EloquentStrictLoadingTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\LazyLoadingViolationException;
  6. use Illuminate\Database\Schema\Blueprint;
  7. use Illuminate\Support\Facades\Schema;
  8. class EloquentStrictLoadingTest extends DatabaseTestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. Model::preventLazyLoading();
  14. }
  15. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  16. {
  17. Schema::create('test_model1', function (Blueprint $table) {
  18. $table->increments('id');
  19. $table->integer('number')->default(1);
  20. });
  21. Schema::create('test_model2', function (Blueprint $table) {
  22. $table->increments('id');
  23. $table->foreignId('model_1_id');
  24. });
  25. Schema::create('test_model3', function (Blueprint $table) {
  26. $table->increments('id');
  27. $table->foreignId('model_2_id');
  28. });
  29. }
  30. public function testStrictModeThrowsAnExceptionOnLazyLoading()
  31. {
  32. $this->expectException(LazyLoadingViolationException::class);
  33. $this->expectExceptionMessage('Attempted to lazy load');
  34. EloquentStrictLoadingTestModel1::create();
  35. EloquentStrictLoadingTestModel1::create();
  36. $models = EloquentStrictLoadingTestModel1::get();
  37. $models[0]->modelTwos;
  38. }
  39. public function testStrictModeDoesntThrowAnExceptionOnLazyLoadingWithSingleModel()
  40. {
  41. EloquentStrictLoadingTestModel1::create();
  42. $models = EloquentStrictLoadingTestModel1::get();
  43. $this->assertInstanceOf(Collection::class, $models);
  44. }
  45. public function testStrictModeDoesntThrowAnExceptionOnAttributes()
  46. {
  47. EloquentStrictLoadingTestModel1::create();
  48. $models = EloquentStrictLoadingTestModel1::get(['id']);
  49. $this->assertNull($models[0]->number);
  50. }
  51. public function testStrictModeDoesntThrowAnExceptionOnEagerLoading()
  52. {
  53. $this->app['config']->set('database.connections.testing.zxc', false);
  54. EloquentStrictLoadingTestModel1::create();
  55. EloquentStrictLoadingTestModel1::create();
  56. $models = EloquentStrictLoadingTestModel1::with('modelTwos')->get();
  57. $this->assertInstanceOf(Collection::class, $models[0]->modelTwos);
  58. }
  59. public function testStrictModeDoesntThrowAnExceptionOnLazyEagerLoading()
  60. {
  61. EloquentStrictLoadingTestModel1::create();
  62. EloquentStrictLoadingTestModel1::create();
  63. $models = EloquentStrictLoadingTestModel1::get();
  64. $models->load('modelTwos');
  65. $this->assertInstanceOf(Collection::class, $models[0]->modelTwos);
  66. }
  67. public function testStrictModeDoesntThrowAnExceptionOnSingleModelLoading()
  68. {
  69. $model = EloquentStrictLoadingTestModel1::create();
  70. $model = EloquentStrictLoadingTestModel1::find($model->id);
  71. $this->assertInstanceOf(Collection::class, $model->modelTwos);
  72. }
  73. public function testStrictModeThrowsAnExceptionOnLazyLoadingInRelations()
  74. {
  75. $this->expectException(LazyLoadingViolationException::class);
  76. $this->expectExceptionMessage('Attempted to lazy load');
  77. $model1 = EloquentStrictLoadingTestModel1::create();
  78. EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
  79. EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
  80. $models = EloquentStrictLoadingTestModel1::with('modelTwos')->get();
  81. $models[0]->modelTwos[0]->modelThrees;
  82. }
  83. public function testStrictModeWithCustomCallbackOnLazyLoading()
  84. {
  85. $this->expectsEvents(ViolatedLazyLoadingEvent::class);
  86. Model::handleLazyLoadingViolationUsing(function ($model, $key) {
  87. event(new ViolatedLazyLoadingEvent($model, $key));
  88. });
  89. EloquentStrictLoadingTestModel1::create();
  90. EloquentStrictLoadingTestModel1::create();
  91. $models = EloquentStrictLoadingTestModel1::get();
  92. $models[0]->modelTwos;
  93. Model::handleLazyLoadingViolationUsing(null);
  94. }
  95. public function testStrictModeWithOverriddenHandlerOnLazyLoading()
  96. {
  97. $this->expectException(\RuntimeException::class);
  98. $this->expectExceptionMessage('Violated');
  99. EloquentStrictLoadingTestModel1WithCustomHandler::create();
  100. EloquentStrictLoadingTestModel1WithCustomHandler::create();
  101. $models = EloquentStrictLoadingTestModel1WithCustomHandler::get();
  102. $models[0]->modelTwos;
  103. }
  104. public function testStrictModeDoesntThrowAnExceptionOnManuallyMadeModel()
  105. {
  106. $model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::make();
  107. $model2 = EloquentStrictLoadingTestModel2::make();
  108. $model1->modelTwos->push($model2);
  109. $this->assertInstanceOf(Collection::class, $model1->modelTwos);
  110. }
  111. public function testStrictModeDoesntThrowAnExceptionOnRecentlyCreatedModel()
  112. {
  113. $model1 = EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading::create();
  114. $this->assertInstanceOf(Collection::class, $model1->modelTwos);
  115. }
  116. }
  117. class EloquentStrictLoadingTestModel1 extends Model
  118. {
  119. public $table = 'test_model1';
  120. public $timestamps = false;
  121. protected $guarded = [];
  122. public function modelTwos()
  123. {
  124. return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
  125. }
  126. }
  127. class EloquentStrictLoadingTestModel1WithCustomHandler extends Model
  128. {
  129. public $table = 'test_model1';
  130. public $timestamps = false;
  131. protected $guarded = [];
  132. public function modelTwos()
  133. {
  134. return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
  135. }
  136. protected function handleLazyLoadingViolation($key)
  137. {
  138. throw new \RuntimeException("Violated {$key}");
  139. }
  140. }
  141. class EloquentStrictLoadingTestModel1WithLocalPreventsLazyLoading extends Model
  142. {
  143. public $table = 'test_model1';
  144. public $timestamps = false;
  145. public $preventsLazyLoading = true;
  146. protected $guarded = [];
  147. public function modelTwos()
  148. {
  149. return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
  150. }
  151. }
  152. class EloquentStrictLoadingTestModel2 extends Model
  153. {
  154. public $table = 'test_model2';
  155. public $timestamps = false;
  156. protected $guarded = [];
  157. public function modelThrees()
  158. {
  159. return $this->hasMany(EloquentStrictLoadingTestModel3::class, 'model_2_id');
  160. }
  161. }
  162. class EloquentStrictLoadingTestModel3 extends Model
  163. {
  164. public $table = 'test_model3';
  165. public $timestamps = false;
  166. protected $guarded = [];
  167. }
  168. class ViolatedLazyLoadingEvent
  169. {
  170. public $model;
  171. public $key;
  172. public function __construct($model, $key)
  173. {
  174. $this->model = $model;
  175. $this->key = $key;
  176. }
  177. }