@@ -28,6 +28,13 @@ protected function setUp(): void {
2828 $ this ->policyId = $ policy ->id ;
2929 }
3030
31+ protected function tearDown (): void {
32+ parent ::tearDown ();
33+
34+ // clear any mocking of CarbonImmutable after each test
35+ CarbonImmutable::setTestNow ();
36+ }
37+
3138 public function testCreatesAndSavesSuccessfully (): void {
3239 $ policyAcceptance = new PolicyAcceptance ([
3340 'user_id ' => $ this ->userId ,
@@ -46,21 +53,88 @@ public function testCreatesAndSavesSuccessfully(): void {
4653 $ this ->assertInstanceOf (CarbonImmutable::class, $ policyAcceptance ->accepted_at );
4754 }
4855
49- public function testCreateFailsIfAcceptedAtIsMissing () {
50- $ this ->expectException (RuntimeException::class);
51- PolicyAcceptance::create ([
56+ // TODO: Quickly testing all different ways of creating and saving a model.
57+ // TODO: Is there a better way to do this e.g. with a `@dataProvider`?
58+ // TODO: Do we need all the options now that we have verified that they all work? Feels like testing Laravel's `Model::creating()` event works.
59+ public function testAcceptedAtIsSetOnCreateIfMissing () {
60+ $ knownDate = CarbonImmutable::create (2026 , 06 , 01 );
61+ CarbonImmutable::setTestNow ($ knownDate );
62+
63+ $ policyAcceptance = PolicyAcceptance::create ([
64+ 'user_id ' => $ this ->userId ,
65+ 'policy_id ' => $ this ->policyId ,
66+ ]);
67+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
68+
69+ $ policyAcceptance ->delete ();
70+
71+ $ policyAcceptance = PolicyAcceptance::make ([
5272 'user_id ' => $ this ->userId ,
5373 'policy_id ' => $ this ->policyId ,
5474 ]);
75+ $ policyAcceptance ->save ();
76+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
77+
78+ $ policyAcceptance ->delete ();
79+
80+ $ policyAcceptance = new PolicyAcceptance ([
81+ 'user_id ' => $ this ->userId ,
82+ 'policy_id ' => $ this ->policyId ,
83+ ]);
84+ $ policyAcceptance ->save ();
85+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
86+
87+ $ policyAcceptance ->delete ();
88+
89+ $ policyAcceptance = new PolicyAcceptance ();
90+ $ policyAcceptance ->user_id = $ this ->userId ;
91+ $ policyAcceptance ->policy_id = $ this ->policyId ;
92+ $ policyAcceptance ->save ();
93+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
94+
95+ $ policyAcceptance ->delete ();
96+
97+ $ policyAcceptance = new PolicyAcceptance ();
98+ $ policyAcceptance ->fill ([
99+ 'user_id ' => $ this ->userId ,
100+ 'policy_id ' => $ this ->policyId ,
101+ ]);
102+ $ policyAcceptance ->save ();
103+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
55104 }
56105
57- public function testCreateFailsIfAcceptedAtIsNull () {
58- $ this ->expectException (RuntimeException::class);
59- PolicyAcceptance::create ([
106+ public function testAcceptedAtIsSetOnCreateIfNull () {
107+ $ knownDate = CarbonImmutable::create (2026 , 06 , 01 );
108+ CarbonImmutable::setTestNow ($ knownDate );
109+
110+ $ policyAcceptance = PolicyAcceptance::create ([
60111 'user_id ' => $ this ->userId ,
61112 'policy_id ' => $ this ->policyId ,
62113 'accepted_at ' => null ,
63114 ]);
115+
116+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
117+ }
118+
119+ public function testAcceptedAtIsNotModifiedOnUpdate () {
120+ $ knownDate = CarbonImmutable::create (2026 , 06 , 01 );
121+ CarbonImmutable::setTestNow ($ knownDate );
122+
123+ $ policyAcceptance = PolicyAcceptance::create ([
124+ 'user_id ' => $ this ->userId ,
125+ 'policy_id ' => $ this ->policyId ,
126+ ]);
127+
128+ $ nextDay = $ knownDate ->addDay ();
129+ CarbonImmutable::setTestNow ($ nextDay );
130+ $ user = User::factory ()->create ();
131+
132+ $ policyAcceptance ->user_id = $ user ->id ;
133+ $ policyAcceptance ->save ();
134+
135+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->accepted_at );
136+ $ this ->assertEquals ($ knownDate , $ policyAcceptance ->created_at );
137+ $ this ->assertEquals ($ nextDay , $ policyAcceptance ->updated_at );
64138 }
65139
66140 public function testUserAcceptingSamePolicyTwiceFails () {
0 commit comments