@@ -2320,30 +2320,85 @@ public function testWithoutTouchingOnCallback()
2320
2320
$ this ->assertTrue ($ called );
2321
2321
}
2322
2322
2323
- public function testAccessingMissingAttributes ()
2323
+ public function testThrowsWhenAccessingMissingAttributes ()
2324
2324
{
2325
+ $ originalMode = Model::preventsAccessingMissingAttributes ();
2326
+ Model::preventAccessingMissingAttributes ();
2327
+
2328
+ try {
2329
+ $ model = new EloquentModelStub (['id ' => 1 ]);
2330
+ $ model ->exists = true ;
2331
+
2332
+ $ this ->assertEquals (1 , $ model ->id );
2333
+ $ this ->expectException (MissingAttributeException::class);
2334
+
2335
+ $ model ->this_attribute_does_not_exist ;
2336
+ } finally {
2337
+ Model::preventAccessingMissingAttributes ($ originalMode );
2338
+ }
2339
+ }
2340
+
2341
+ public function testDoesntThrowWhenAccessingMissingAttributesOnModelThatIsNotSaved ()
2342
+ {
2343
+ $ originalMode = Model::preventsAccessingMissingAttributes ();
2344
+ Model::preventAccessingMissingAttributes ();
2345
+
2325
2346
try {
2326
- Model::preventAccessingMissingAttributes (false );
2347
+ $ model = new EloquentModelStub (['id ' => 1 ]);
2348
+ $ model ->exists = false ;
2349
+
2350
+ $ this ->assertEquals (1 , $ model ->id );
2351
+ $ this ->assertNull ($ model ->this_attribute_does_not_exist );
2352
+ } finally {
2353
+ Model::preventAccessingMissingAttributes ($ originalMode );
2354
+ }
2355
+ }
2327
2356
2357
+ public function testDoesntThrowWhenAccessingMissingAttributesOnModelThatWasRecentlyCreated ()
2358
+ {
2359
+ $ originalMode = Model::preventsAccessingMissingAttributes ();
2360
+ Model::preventAccessingMissingAttributes ();
2361
+
2362
+ try {
2328
2363
$ model = new EloquentModelStub (['id ' => 1 ]);
2329
2364
$ model ->exists = true ;
2365
+ $ model ->wasRecentlyCreated = true ;
2330
2366
2331
- // Default behavior
2332
2367
$ this ->assertEquals (1 , $ model ->id );
2333
2368
$ this ->assertNull ($ model ->this_attribute_does_not_exist );
2369
+ } finally {
2370
+ Model::preventAccessingMissingAttributes ($ originalMode );
2371
+ }
2372
+ }
2334
2373
2335
- Model::preventAccessingMissingAttributes (true );
2374
+ public function testDoesntThrowWhenAssigningMissingAttributes ()
2375
+ {
2376
+ $ originalMode = Model::preventsAccessingMissingAttributes ();
2377
+ Model::preventAccessingMissingAttributes ();
2336
2378
2337
- // "preventAccessingMissingAttributes" behavior
2338
- $ this ->expectException (MissingAttributeException::class);
2339
- $ model ->this_attribute_does_not_exist ;
2379
+ try {
2380
+ $ model = new EloquentModelStub (['id ' => 1 ]);
2381
+ $ model ->exists = true ;
2382
+
2383
+ $ model ->this_attribute_does_not_exist = 'now it does ' ;
2384
+ } finally {
2385
+ Model::preventAccessingMissingAttributes ($ originalMode );
2386
+ }
2387
+ }
2388
+
2389
+ public function testDoesntThrowWhenTestingMissingAttributes ()
2390
+ {
2391
+ $ originalMode = Model::preventsAccessingMissingAttributes ();
2392
+ Model::preventAccessingMissingAttributes ();
2393
+
2394
+ try {
2395
+ $ model = new EloquentModelStub (['id ' => 1 ]);
2396
+ $ model ->exists = true ;
2340
2397
2341
- // Ensure that unsaved models do not trigger the exception
2342
- $ newModel = new EloquentModelStub (['id ' => 2 ]);
2343
- $ this ->assertEquals (2 , $ newModel ->id );
2344
- $ this ->assertNull ($ newModel ->this_attribute_does_not_exist );
2398
+ $ this ->assertTrue (isset ($ model ->id ));
2399
+ $ this ->assertFalse (isset ($ model ->this_attribute_does_not_exist ));
2345
2400
} finally {
2346
- Model::preventAccessingMissingAttributes (false );
2401
+ Model::preventAccessingMissingAttributes ($ originalMode );
2347
2402
}
2348
2403
}
2349
2404
0 commit comments