Skip to content

Commit bcde5fe

Browse files
committed
refactor: Use factories for set grace days
1 parent 05ffe80 commit bcde5fe

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Models/Subscription.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function renew(?Carbon $expirationDate = null): self
113113
$graceDaysEndedAt = null;
114114

115115
if ($this->plan->grace_days) {
116-
$graceDaysEndedAt = $expirationDate->addDays($this->plan->grace_days);
116+
$graceDaysEndedAt = $expirationDate->copy()->addDays($this->plan->grace_days);
117117
}
118118

119119
$this->update([

tests/Models/SubscriptionTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,12 @@ public function testModelReturnsOnlyNotCanceledSubscriptionsWithTheScope()
347347
public function testModelUpdatesGraceDaysEndedAtWhenRenewing()
348348
{
349349
$subscriber = User::factory()->create();
350+
$plan = Plan::factory()->create([
351+
'grace_days' => $graceDays = $this->faker()->randomDigitNotNull(),
352+
]);
353+
350354
$subscription = Subscription::factory()
355+
->for($plan)
351356
->for($subscriber, 'subscriber')
352357
->create([
353358
'grace_days_ended_at' => now()->subDay(),
@@ -357,21 +362,24 @@ public function testModelUpdatesGraceDaysEndedAtWhenRenewing()
357362

358363
$this->assertDatabaseHas('subscriptions', [
359364
'id' => $subscription->id,
360-
'grace_days_ended_at' => $subscription->expired_at->addDays($subscription->plan->grace_days),
365+
'grace_days_ended_at' => $subscription->expired_at->addDays($graceDays),
361366
]);
362367
}
363368

364369
public function testModelLeavesGraceDaysEmptyWhenRenewingIfPlanDoesNotHaveIt()
365370
{
366371
$subscriber = User::factory()->create();
372+
$plan = Plan::factory()->create([
373+
'grace_days' => 0,
374+
]);
375+
367376
$subscription = Subscription::factory()
377+
->for($plan)
368378
->for($subscriber, 'subscriber')
369379
->create([
370380
'grace_days_ended_at' => null,
371381
]);
372382

373-
$subscription->plan->update(['grace_days' => 0]);
374-
375383
$subscription->renew();
376384

377385
$this->assertDatabaseHas('subscriptions', [

0 commit comments

Comments
 (0)