File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -355,4 +355,14 @@ public function isEmpty(): bool
355
355
{
356
356
return collect ($ this ->jsonModelAttributeCache )->every ->isEmpty () && parent ::isEmpty ();
357
357
}
358
+
359
+ /**
360
+ * The Laravel method ->refresh tries to reuse the object reference while reloading data from disk.
361
+ * Reload the new cache this attribute introduces, too.
362
+ */
363
+ public function refresh (): static
364
+ {
365
+ $ this ->emptyJsonModelAttributeCache ();
366
+ return parent ::refresh ();
367
+ }
358
368
}
Original file line number Diff line number Diff line change 15
15
use Tests \BaseTestCase ;
16
16
use Tests \MockClasses \ConcreteJsonModel ;
17
17
use Tests \MockClasses \ConcreteNullableJsonModel ;
18
+ use Tests \MockClasses \EventedModelWithJsonAttributes ;
18
19
19
20
/**
20
21
* Class HasJsonModelAttributes
@@ -424,4 +425,23 @@ public function testAttributeCanBeTestedWithIsset()
424
425
self ::assertFalse ($ model ->jsonModel ->a ?? false );
425
426
self ::assertFalse ($ model ->jsonModel ->other ?? false );
426
427
}
428
+
429
+ public function testAttributeIsResetByRefresh ()
430
+ {
431
+ $ model = mock (EventedModelWithJsonAttributes::class)->makePartial ()->shouldAllowMockingProtectedMethods ();
432
+ // We're mocking the guts of Model::refresh to not actually require a database
433
+ $ model ->shouldReceive ('setKeysForSelectQuery ' )->once ()->andReturnSelf ();
434
+ $ model ->shouldReceive ('useWritePdo ' )->andReturnSelf ();
435
+ $ model ->shouldReceive ('firstOrFail ' )->andReturn ((object )['attributes ' => ['thing ' => ['a ' => "aneurysm " ]]]);
436
+
437
+ $ model ->thing ->a = "Apple " ;
438
+ self ::assertSame ("Apple " , $ model ->thing ->a );
439
+
440
+ // You have to tell the model it exists (on disk) or refresh doesn't do anything.
441
+ $ model ->exists = true ;
442
+ // The change wasn't saved, the model reverts to "disk"
443
+ $ model ->refresh ();
444
+ self ::assertEmpty (getProperty ($ model , 'jsonModelAttributeCache ' ));
445
+ self ::assertSame ('aneurysm ' , $ model ->thing ->a );
446
+ }
427
447
}
You can’t perform that action at this time.
0 commit comments