Skip to content

Commit

Permalink
Merge branch 'release/2.2.2' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdotvin committed Jul 19, 2022
2 parents 1ac050a + 57ea335 commit 955b4d8
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.3.1
uses: dependabot/fetch-metadata@v1.3.3
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
6 changes: 5 additions & 1 deletion config/soulbscription.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'feature_tickets' => false,
'feature_tickets' => env('SOULBSCRIPTION_FEATURE_TICKETS', false),

'models' => [

Expand All @@ -14,6 +14,10 @@
'feature_plan' => \LucasDotVin\Soulbscription\Models\FeaturePlan::class,

'plan' => \LucasDotVin\Soulbscription\Models\Plan::class,

'subscriber' => [
'uses_uuid' => env('SOULBSCRIPTION_SUBSCRIBER_USES_UUID', false),
],

'subscription' => \LucasDotVin\Soulbscription\Models\Subscription::class,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up()
$table->id();
$table->foreignIdFor(\LucasDotVin\Soulbscription\Models\Plan::class);
$table->timestamp('canceled_at')->nullable();
$table->timestamp('expired_at');
$table->timestamp('expired_at')->nullable();
$table->timestamp('grace_days_ended_at')->nullable();
$table->date('started_at');
$table->timestamp('suppressed_at')->nullable();
Expand Down
22 changes: 22 additions & 0 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ public function subscribeTo(Plan $plan, $expiration = null, $startDate = null):
->start($startDate);
}

public function hasSubscriptionTo(Plan $plan): bool
{
return $this->subscription()
->where('plan_id', $plan->id)
->exists();
}

public function isSubscribedTo(Plan $plan): bool
{
return $this->hasSubscriptionTo($plan);
}

public function missingSubscriptionTo(Plan $plan): bool
{
return !$this->hasSubscriptionTo($plan);
}

public function isNotSubscribedTo(Plan $plan): bool
{
return !$this->isSubscribedTo($plan);
}

public function switchTo(Plan $plan, $expiration = null, $immediately = true): Subscription
{
if ($immediately) {
Expand Down
15 changes: 14 additions & 1 deletion src/Models/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function renew(?Carbon $expirationDate = null): self
'overdue' => $this->isOverdue,
]);

$expirationDate = $expirationDate ?: $this->plan->calculateNextRecurrenceEnd();
$expirationDate = $this->getRenewedExpiration($expirationDate);

$this->update([
'expired_at' => $expirationDate,
Expand Down Expand Up @@ -153,4 +153,17 @@ public function getIsOverdueAttribute(): bool

return $this->expired_at->isPast();
}

private function getRenewedExpiration(?Carbon $expirationDate = null)
{
if (!empty($expirationDate)) {
return $expirationDate;
}

if ($this->isOverdue) {
return $this->plan->calculateNextRecurrenceEnd();
}

return $this->plan->calculateNextRecurrenceEnd($this->expired_at);
}
}
28 changes: 28 additions & 0 deletions tests/Models/Concerns/HasSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,32 @@ public function testItRaisesAnExceptionWhenSettingConsumedQuotaForANotQuotaFeatu

$subscriber->setConsumedQuota($feature->name, $consumption);
}

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

$subscriber = User::factory()->createOne();
$subscriber->subscribeTo($plan);

$hasSubscription = $subscriber->hasSubscriptionTo($plan);
$isSubscribed = $subscriber->isSubscribedTo($plan);

$this->assertTrue($hasSubscription);
$this->assertTrue($isSubscribed);
}

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

$subscriber = User::factory()->createOne();
$subscriber->subscribeTo($plan);

$hasSubscription = $subscriber->missingSubscriptionTo($plan);
$isSubscribed = $subscriber->isNotSubscribedTo($plan);

$this->assertFalse($hasSubscription);
$this->assertFalse($isSubscribed);
}
}
39 changes: 37 additions & 2 deletions tests/Models/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,47 @@ class SubscriptionTest extends TestCase

public function testModelRenews()
{
Carbon::setTestNow(now());

$plan = Plan::factory()->create();
$subscriber = User::factory()->create();
$subscription = Subscription::factory()
->for($plan)
->for($subscriber, 'subscriber')
->create();
->create([
'expired_at' => now()->addDays(1),
]);

$expectedExpiredAt = $plan->calculateNextRecurrenceEnd($subscription->expired_at)->toDateTimeString();

Event::fake();

$subscription->renew();

Event::assertDispatched(SubscriptionRenewed::class);

$this->assertDatabaseHas('subscriptions', [
'plan_id' => $plan->id,
'subscriber_id' => $subscriber->id,
'subscriber_type' => User::class,
'expired_at' => $expectedExpiredAt,
]);
}

public function testModelRenewsBasedOnCurrentDateIfOverdue()
{
Carbon::setTestNow(now());

$plan = Plan::factory()->create();
$subscriber = User::factory()->create();
$subscription = Subscription::factory()
->for($plan)
->for($subscriber, 'subscriber')
->create([
'expired_at' => now()->subDay(),
]);

$expectedExpiredAt = $plan->calculateNextRecurrenceEnd()->toDateTimeString();

Event::fake();

Expand All @@ -39,7 +74,7 @@ public function testModelRenews()
'plan_id' => $plan->id,
'subscriber_id' => $subscriber->id,
'subscriber_type' => User::class,
'expired_at' => $plan->calculateNextRecurrenceEnd(),
'expired_at' => $expectedExpiredAt,
]);
}

Expand Down

0 comments on commit 955b4d8

Please sign in to comment.