Skip to content

Commit

Permalink
add tests for getReimburesmentAmountForUnusedTime`
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jul 16, 2024
1 parent bd2b86a commit 9353c9c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ public static function handlePaymentPaid(OrderItem $item)

if ($subscription->ends_at !== null) {
DB::transaction(function () use ($item, $subscription) {
if (! $subscription->scheduled_order_item_id) {
if (!$subscription->scheduled_order_item_id) {
$item = $subscription->scheduleNewOrderItemAt($subscription->ends_at);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Database/Factories/OrderItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ public function USD()
'currency' => 'USD',
]);
}

public function withOrder(): self
{
return $this->state(fn () => [
'order_id' => OrderFactory::new(),
]);
}
}
49 changes: 49 additions & 0 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Events\SubscriptionResumed;
use Laravel\Cashier\Subscription;
use Laravel\Cashier\Tests\Database\Factories\OrderFactory;
use Laravel\Cashier\Tests\Database\Factories\OrderItemFactory;
use Laravel\Cashier\Tests\Database\Factories\SubscriptionFactory;
use Laravel\Cashier\Tests\Fixtures\User;
Expand Down Expand Up @@ -589,4 +590,52 @@ public function canQueryRecurringSubscriptions()
$this->assertEquals(1, Subscription::whereRecurring()->count());
$this->assertEquals(2, Subscription::whereNotRecurring()->count());
}

/** @test */
public function halfWayThroughSubscriptionReturnsPositiveReimburesmentAmount()
{
$this->withConfiguredPlans();

$subscriptionHalfWayThrough = SubscriptionFactory::new()
->has(
OrderItemFactory::new(['unit_price' => 100])
->processed()
->withOrder()
->EUR(),
'scheduledOrderItem'
)
->create([
'cycle_started_at' => now()->subDays(20),
'cycle_ends_at' => now()->addDays(20),
]);

$this->assertEquals(
money('-50', 'EUR'),
$subscriptionHalfWayThrough->getReimburseAmountForUnusedTime()
);
}

/** @test */
public function nonReimbursableSubscriptionReturnsNoReimbursementAmount()
{
$this->withConfiguredPlans();

$nonReimbursable = SubscriptionFactory::new()
->has(
OrderItemFactory::new(['unit_price' => 100])
->processed()
->withOrder()
->EUR(),
'scheduledOrderItem'
)
->create([
'cycle_started_at' => now()->subMonth(),
'cycle_ends_at' => now()->subDay(),
]);

$this->assertEquals(
null,
$nonReimbursable->getReimburseAmountForUnusedTime()
);
}
}

0 comments on commit 9353c9c

Please sign in to comment.