Skip to content

Commit

Permalink
add possibility to check whether a plan has a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baettig committed Jul 11, 2022
1 parent 86aa24c commit cf5dcc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Models/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public function subscriptions(): HasMany
return $this->hasMany(config('subscriptions.models.subscription'), 'plan_id');
}

public function hasFeature(string $code): bool
{
/** @noinspection PhpUndefinedMethodInspection */
if (count($this->features()->code($code)->get())) return true;
return false;
}

public function getPlanTypeDefinitionAttribute(): array
{
return $this::getPlanTypeDefinitionForCode($this->type);
Expand Down
2 changes: 1 addition & 1 deletion tests/feature/SubscribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function can_subscribe_to_a_recurring_yearly_plan(): void
$this->assertTrue($plan->is($subscription->plan));
$this->assertEquals($plan->price, $subscription->price);
$this->assertEquals($plan->currency, $subscription->currency);
$this->assertEquals(Carbon::now()->addYear()->diffInDays(Carbon::now()), $subscription->remaining_days);
$this->assertEqualsWithDelta(Carbon::now()->addYear()->diffInDays(Carbon::now()), $subscription->remaining_days, 1);
$this->assertEqualsWithDelta(Carbon::now()->addDays(30)->endOfDay(), $subscription->payment_tolerance_ends_at, 1);
$this->assertTrue($subscription->is_recurring);
$this->assertNull($subscription->refunded_at);
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,29 @@ public function info_on_unlimited_feature_is_correct(): void
$this->assertFalse($unlimitedFeature->isLimited());
$this->assertTrue($unlimitedFeature->isUnlimited());
}

/** @test */
public function a_plan_also_knows_its_features(): void
{
$plan = factory(Plan::class)->create();
$plan->features()->saveMany([
new Feature([
'name' => 'Limited feature',
'code' => 'feature.limited',
'description' => 'Some limited feature',
'type' => 'limit',
'limit' => 10,
]),
new Feature([
'name' => 'Unlimited feature',
'code' => 'feature.feature',
'description' => 'Some unlimited feature',
'type' => 'feature',
'limit' => 20,
])
]);
$this->assertTrue($plan->hasFeature('feature.limited'));
$this->assertTrue($plan->hasFeature('feature.feature'));
$this->assertFalse($plan->hasFeature('some.feature.that.does.not.exist'));
}
}

0 comments on commit cf5dcc8

Please sign in to comment.