forked from laravel/framework
/
EloquentStrictLoadingTest.php
234 lines (177 loc) · 6.73 KB
/
EloquentStrictLoadingTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Illuminate\Tests\Integration\Database;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\LazyLoadingViolationException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class EloquentStrictLoadingTest extends DatabaseTestCase
{
protected function setUp(): void
{
parent::setUp();
Model::preventLazyLoading();
}
protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
{
Schema::create('test_model1', function (Blueprint $table) {
$table->increments('id');
$table->integer('number')->default(1);
});
Schema::create('test_model2', function (Blueprint $table) {
$table->increments('id');
$table->foreignId('model_1_id');
});
Schema::create('test_model3', function (Blueprint $table) {
$table->increments('id');
$table->foreignId('model_2_id');
});
}
public function testStrictModeThrowsAnExceptionOnLazyLoading()
{
$this->expectException(LazyLoadingViolationException::class);
$this->expectExceptionMessage('Attempted to lazy load');
EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::get();
$models[0]->modelTwos;
}
public function testStrictModeDoesntThrowAnExceptionOnLazyLoadingWithSingleModel()
{
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::get();
$this->assertInstanceOf(Collection::class, $models);
}
public function testStrictModeDoesntThrowAnExceptionOnAttributes()
{
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::get(['id']);
$this->assertNull($models[0]->number);
}
public function testStrictModeDoesntThrowAnExceptionOnEagerLoading()
{
$this->app['config']->set('database.connections.testing.zxc', false);
EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::with('modelTwos')->get();
$this->assertInstanceOf(Collection::class, $models[0]->modelTwos);
}
public function testStrictModeDoesntThrowAnExceptionOnLazyEagerLoading()
{
EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::get();
$models->load('modelTwos');
$this->assertInstanceOf(Collection::class, $models[0]->modelTwos);
}
public function testStrictModeDoesntThrowAnExceptionOnSingleModelLoading()
{
$model = EloquentStrictLoadingTestModel1::create();
$model = EloquentStrictLoadingTestModel1::find($model->id);
$this->assertInstanceOf(Collection::class, $model->modelTwos);
}
public function testStrictModeThrowsAnExceptionOnLazyLoadingInRelations()
{
$this->expectException(LazyLoadingViolationException::class);
$this->expectExceptionMessage('Attempted to lazy load');
$model1 = EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
EloquentStrictLoadingTestModel2::create(['model_1_id' => $model1->id]);
$models = EloquentStrictLoadingTestModel1::with('modelTwos')->get();
$models[0]->modelTwos[0]->modelThrees;
}
public function testStrictModeWithCustomCallbackOnLazyLoading()
{
$this->expectsEvents(ViolatedLazyLoadingEvent::class);
Model::handleLazyLoadingViolationUsing(function ($model, $key) {
event(new ViolatedLazyLoadingEvent($model, $key));
});
EloquentStrictLoadingTestModel1::create();
EloquentStrictLoadingTestModel1::create();
$models = EloquentStrictLoadingTestModel1::get();
$models[0]->modelTwos;
Model::handleLazyLoadingViolationUsing(null);
}
public function testStrictModeWithOverriddenHandlerOnLazyLoading()
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Violated');
EloquentStrictLoadingTestModel1WithCustomHandler::create();
EloquentStrictLoadingTestModel1WithCustomHandler::create();
$models = EloquentStrictLoadingTestModel1WithCustomHandler::get();
$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
{
public $table = 'test_model1';
public $timestamps = false;
protected $guarded = [];
public function modelTwos()
{
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
}
}
class EloquentStrictLoadingTestModel1WithCustomHandler extends Model
{
public $table = 'test_model1';
public $timestamps = false;
protected $guarded = [];
public function modelTwos()
{
return $this->hasMany(EloquentStrictLoadingTestModel2::class, 'model_1_id');
}
protected function handleLazyLoadingViolation($key)
{
throw new \RuntimeException("Violated {$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';
public $timestamps = false;
protected $guarded = [];
public function modelThrees()
{
return $this->hasMany(EloquentStrictLoadingTestModel3::class, 'model_2_id');
}
}
class EloquentStrictLoadingTestModel3 extends Model
{
public $table = 'test_model3';
public $timestamps = false;
protected $guarded = [];
}
class ViolatedLazyLoadingEvent
{
public $model;
public $key;
public function __construct($model, $key)
{
$this->model = $model;
$this->key = $key;
}
}