Skip to content

Commit d6d75b0

Browse files
committed
Set PolicyAcceptance::accepted_at to "now" if null when created
So that we don't have to specify `accepted_at` each time we create a `PolicyAcceptance` object. Bug: T430706
1 parent 7f60f37 commit d6d75b0

2 files changed

Lines changed: 86 additions & 6 deletions

File tree

app/PolicyAcceptance.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class PolicyAcceptance extends Model {
3838
'accepted_at',
3939
];
4040

41+
protected static function booted(): void {
42+
static::creating(function (PolicyAcceptance $model): void {
43+
$model->accepted_at ??= CarbonImmutable::now();
44+
});
45+
}
46+
4147
protected function casts(): array {
4248
return [
4349
// cast `accepted_at` to a `CarbonImmutable` instance rather than a string

tests/PolicyAcceptanceTest.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)