Skip to content

Commit

Permalink
Merge pull request #262 from ignaciocunado/feat/user_has_any_subscrip…
Browse files Browse the repository at this point in the history
…tion

User has any subscription
  • Loading branch information
Naoray authored Aug 26, 2024
2 parents 3c5bde3 + d7612f5 commit a13c992
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,18 @@ public function subscribed($subscription = 'default', $plan = null)
$subscription->plan === $plan;
}

/**
* Determine if the billable model has any active subscription.
*
* @return bool
*/
public function hasActiveSubscription(): bool
{
return $this
->subscriptions()
->whereActive()
->exists();
}
/**
* @param $plans
* @param string $subscription
Expand Down
27 changes: 27 additions & 0 deletions tests/BillableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Coupon\RedeemedCouponCollection;
use Laravel\Cashier\Subscription;
use Laravel\Cashier\Tests\Database\Factories\OrderFactory;
use Laravel\Cashier\Tests\Database\Factories\RedeemedCouponFactory;
use Laravel\Cashier\Events\MandateClearedFromBillable;
use Laravel\Cashier\Exceptions\MandateIsNotYetFinalizedException;
use Laravel\Cashier\Order\Invoice;
use Laravel\Cashier\SubscriptionBuilder\FirstPaymentSubscriptionBuilder;
use Laravel\Cashier\SubscriptionBuilder\MandatedSubscriptionBuilder;
use Laravel\Cashier\Tests\Database\Factories\SubscriptionFactory;
use Laravel\Cashier\Tests\Fixtures\User;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -286,4 +288,29 @@ public function canFindInvoiceByOrderIdUsingFindInvoiceByOrderIdOrFail()
$this->assertInstanceOf(Invoice::class, $invoice);
$this->assertEquals('2018-0000-0002', $invoice->id());
}

/** @test */
public function testHasActiveSubscription()
{
$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',
]);
$user->newSubscription('default', 'monthly-10-1')->create();

$this->assertTrue($user->hasActiveSubscription());
}

/** @test */
public function testHasActiveSubscriptionsFalse()
{
$user = User::factory()->create();

$this->assertFalse($user->hasActiveSubscription());
}
}

0 comments on commit a13c992

Please sign in to comment.