Skip to content

Commit

Permalink
Merge branch 'release/3.0.0' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdotvin committed Oct 12, 2022
2 parents b1d497e + 68fdc3b commit 8fb2da3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ public function missingFeature($featureName): bool
}

public function getRemainingCharges($featureName): float
{
$balance = $this->balance($featureName);

return max($balance, 0);
}

public function balance($featureName)
{
if (empty($this->getFeature($featureName))) {
return 0;
Expand All @@ -250,11 +257,6 @@ public function getRemainingCharges($featureName): float
return $totalCharges - $currentConsumption;
}

public function balance($featureName)
{
return $this->getRemainingCharges($featureName);
}

public function getCurrentConsumption($featureName): float
{
if (empty($feature = $this->getFeature($featureName))) {
Expand Down
38 changes: 38 additions & 0 deletions tests/Models/Concerns/HasSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,4 +913,42 @@ public function testItCanConsumeAFeatureAfterItsChargesIfThisFeatureIsPostpaid()
'subscriber_id' => $subscriber->id,
]);
}

public function testItDoesNotReturnNegativeChargesForFeatures()
{
$charges = $this->faker->numberBetween(5, 10);
$consumption = $this->faker->numberBetween($charges, $charges * 2);

$plan = Plan::factory()->createOne();
$feature = Feature::factory()->postpaid()->createOne();
$feature->plans()->attach($plan, [
'charges' => $charges,
]);

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

$subscriber->consume($feature->name, $consumption);

$this->assertEquals(0, $subscriber->getRemainingCharges($feature->name));
}

public function testItReturnsNegativeBalanceForFeatures()
{
$charges = $this->faker->numberBetween(5, 10);
$consumption = $this->faker->numberBetween($charges, $charges * 2);

$plan = Plan::factory()->createOne();
$feature = Feature::factory()->postpaid()->createOne();
$feature->plans()->attach($plan, [
'charges' => $charges,
]);

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

$subscriber->consume($feature->name, $consumption);

$this->assertLessThan(0, $subscriber->balance($feature->name));
}
}

0 comments on commit 8fb2da3

Please sign in to comment.