Skip to content

Commit 8567413

Browse files
authored
Add tests for Model::preventAccessingMissingAttributes() (#44645)
1 parent c074646 commit 8567413

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,30 +2320,85 @@ public function testWithoutTouchingOnCallback()
23202320
$this->assertTrue($called);
23212321
}
23222322

2323-
public function testAccessingMissingAttributes()
2323+
public function testThrowsWhenAccessingMissingAttributes()
23242324
{
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+
23252346
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+
}
23272356

2357+
public function testDoesntThrowWhenAccessingMissingAttributesOnModelThatWasRecentlyCreated()
2358+
{
2359+
$originalMode = Model::preventsAccessingMissingAttributes();
2360+
Model::preventAccessingMissingAttributes();
2361+
2362+
try {
23282363
$model = new EloquentModelStub(['id' => 1]);
23292364
$model->exists = true;
2365+
$model->wasRecentlyCreated = true;
23302366

2331-
// Default behavior
23322367
$this->assertEquals(1, $model->id);
23332368
$this->assertNull($model->this_attribute_does_not_exist);
2369+
} finally {
2370+
Model::preventAccessingMissingAttributes($originalMode);
2371+
}
2372+
}
23342373

2335-
Model::preventAccessingMissingAttributes(true);
2374+
public function testDoesntThrowWhenAssigningMissingAttributes()
2375+
{
2376+
$originalMode = Model::preventsAccessingMissingAttributes();
2377+
Model::preventAccessingMissingAttributes();
23362378

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;
23402397

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));
23452400
} finally {
2346-
Model::preventAccessingMissingAttributes(false);
2401+
Model::preventAccessingMissingAttributes($originalMode);
23472402
}
23482403
}
23492404

0 commit comments

Comments
 (0)