Skip to content

Commit

Permalink
Merge branch 'release/2.4' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdotvin committed Sep 8, 2022
2 parents 9e686ed + 27c7e60 commit 2b3f778
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public function subscription()
return $this->morphOne(config('soulbscription.models.subscription'), 'subscriber')->ofMany('started_at', 'MAX');
}

public function lastSubscription()
{
return app(config('soulbscription.models.subscription'))
->withExpired()
->whereMorphedTo('subscriber', $this)
->orderBy('started_at', 'DESC')
->first();
}

/**
* @throws OutOfBoundsException
* @throws OverflowException
Expand Down
1 change: 0 additions & 1 deletion tests/Models/Concerns/ExpiresAndHasGraceDaysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function testModelReturnsNotExpiredStatus()
$this->assertTrue($notExpiredModel->notExpired());
}


public function testModelReturnsIfItHasExpired()
{
$expiredModel = self::MODEL::factory()
Expand Down
13 changes: 13 additions & 0 deletions tests/Models/Concerns/HasSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,4 +837,17 @@ public function testItChecksIfTheUserDoesNotHaveSubscriptionToAPlan()
$this->assertFalse($hasSubscription);
$this->assertFalse($isSubscribed);
}

public function testItReturnsTheLastSubscriptionWhenRetrievingExpired()
{
$plan = Plan::factory()->createOne();

$subscriber = User::factory()->createOne();
$subscriber->subscribeTo($plan, now()->subDay(), now()->subDay());
$expectedSubscription = $subscriber->subscribeTo($plan, now()->subHour(), now()->subHour());

$returnedSubscription = $subscriber->lastSubscription();

$this->assertEquals($expectedSubscription->id, $returnedSubscription->id);
}
}

0 comments on commit 2b3f778

Please sign in to comment.