From e4b63c292398e8bf801c1fdc117080b57cd0886d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Cu=C3=B1ado=20Barral?= Date: Thu, 24 Oct 2024 16:33:59 +0200 Subject: [PATCH 1/2] feat: Add method to get all scheduled order items for a user --- src/Billable.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Billable.php b/src/Billable.php index 0db1279..8303577 100644 --- a/src/Billable.php +++ b/src/Billable.php @@ -318,6 +318,16 @@ public function orderItems() return $this->morphMany(Cashier::$orderItemModel, 'owner'); } + /** + * Get all the Scheduled Order Items for the billable model. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany + */ + public function scheduledOrderItems() + { + return $this->morphToMany(Cashier::$orderItemModel, 'owner', 'subscriptions', 'owner_id', 'scheduled_order_item_id'); + } + /** * Get the balances for the billable model. A separate balance is kept for each currency. * From d0f75eb8e66b421939cb368ef85dbe24567a99b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ignacio=20Cu=C3=B1ado=20Barral?= Date: Thu, 24 Oct 2024 16:34:08 +0200 Subject: [PATCH 2/2] test: Test scheduledOrderItems() method --- tests/BillableTest.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/BillableTest.php b/tests/BillableTest.php index 54e3acf..2c035ca 100644 --- a/tests/BillableTest.php +++ b/tests/BillableTest.php @@ -313,4 +313,53 @@ public function testHasActiveSubscriptionsFalse() $this->assertFalse($user->hasActiveSubscription()); } + + public function testGetScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + $this->withMockedGetMollieCustomer(3); + $this->withMockedGetMollieMandateAccepted(3); + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + $subscription = $user->newSubscription('default', 'monthly-10-1')->create(); + + $this->assertEquals($subscription->scheduledOrderItem->original, $user->scheduledOrderItems[0]->original); + } + + public function testGetTwoScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + $this->withMockedGetMollieCustomer(6); + $this->withMockedGetMollieMandateAccepted(6); + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + + $subscription1 = $user->newSubscription('default', 'monthly-10-1')->create(); + $subscription2 = $user->newSubscription('default', 'monthly-20-1')->create(); + + $this->assertEquals($subscription1->scheduledOrderItem->original, $user->scheduledOrderItems[0]->original); + $this->assertEquals($subscription2->scheduledOrderItem->original, $user->scheduledOrderItems[1]->original); + } + + public function testGetNoScheduledOrderItems() + { + $this->withConfiguredPlans(); + $this->withMockedCouponRepository(); // 'test-coupon' + + $user = $this->getMandatedUser(true, [ + 'mollie_mandate_id' => 'mdt_unique_mandate_id', + 'mollie_customer_id' => 'cst_unique_customer_id', + ]); + + + $this->assertEquals($user->scheduledOrderItems, $user->orderItems); + } }