From dcc472f2aa6658b54b599523d4152310f54701c3 Mon Sep 17 00:00:00 2001 From: Peter Baettig Date: Fri, 22 Nov 2019 14:07:45 +0100 Subject: [PATCH] change some methods to getters (where it made more sense) --- src/Models/HasPlans.php | 19 ++-- src/Models/Subscription.php | 87 ++++++++++++++++--- tests/feature/CancelSubscriptionTest.php | 26 +++--- tests/feature/ExtendSubscriptionTest.php | 18 ++-- tests/feature/MigrateSubscriptionTest.php | 18 ++-- .../feature/RenewExpiredSubscriptionTest.php | 2 +- .../feature/RenewExpiringSubscriptionTest.php | 16 ++-- tests/feature/SubscribeTest.php | 36 ++++---- tests/unit/SubscriptionTest.php | 82 ++++++++--------- 9 files changed, 183 insertions(+), 121 deletions(-) diff --git a/src/Models/HasPlans.php b/src/Models/HasPlans.php index 36d5a23..cb79104 100644 --- a/src/Models/HasPlans.php +++ b/src/Models/HasPlans.php @@ -24,15 +24,17 @@ public function subscriptions(): MorphMany return $this->morphMany(config('subscriptions.models.subscription'), 'model'); } - /** - * @return Subscription | null - */ - public function activeSubscription(): ?Subscription + private function activeSubscription() { /** @noinspection PhpUndefinedMethodInspection */ return $this->subscriptions()->active()->first(); } + public function getActiveSubscriptionAttribute() + { + return $this->activeSubscription(); + } + /** * @return Subscription | null */ @@ -158,7 +160,7 @@ public function migrateSubscriptionTo(Plan $plan, throw new SubscriptionException('no active subscription found'); } - if (false === $immediate && $previousSubscription->isTesting()) { + if (false === $immediate && $previousSubscription->is_testing) { throw new SubscriptionException('can only migrate a subscription in the test phase immediately'); } @@ -292,15 +294,16 @@ public function cancelSubscription(bool $immediate = false): Subscription */ public function renewExpiringSubscription(bool $markAsPaid = false): Subscription { + /** @var Subscription $activeSubscription */ if (!$activeSubscription = $this->activeSubscription()) { throw new SubscriptionException('No active subscription found'); } - if (!$activeSubscription->isPaid()) { + if (!$activeSubscription->is_paid) { throw new SubscriptionException('Renewing is not possible if currently active Subscription is not paid'); } - if (!$activeSubscription->isExpiring()) { + if (!$activeSubscription->is_expiring) { throw new SubscriptionException('Renewing is not possible if subscription is expiring earlyer than tomorrow midnight'); } @@ -308,7 +311,7 @@ public function renewExpiringSubscription(bool $markAsPaid = false): Subscriptio throw new SubscriptionException('Renewing a non-recurring subscription is not possible'); } - if ($activeSubscription->isPendingCancellation()) { + if ($activeSubscription->is_pending_cancellation) { throw new SubscriptionException('Renewing a subscription that is pending cancellation is not possible'); } diff --git a/src/Models/Subscription.php b/src/Models/Subscription.php index 701da8d..9418d1d 100644 --- a/src/Models/Subscription.php +++ b/src/Models/Subscription.php @@ -45,6 +45,15 @@ * * @property Plan plan * @property HasMany features + * @property boolean has_started + * @property boolean is_testing + * @property boolean is_paid + * @property boolean is_pending_cancellation + * @property boolean is_cancelled + * @property boolean is_renewed + * @property boolean is_active + * @property boolean is_refunded + * @property boolean is_within_payment_tolerance_time * * @method static Builder active * @method static Builder expiring @@ -188,68 +197,118 @@ public function scopeWithinPaymentTolerance($query): Builder return $query->where('payment_tolerance_ends_at', '>', Carbon::now()); } - public function hasStarted(): bool + private function hasStarted(): bool { return Carbon::now()->greaterThanOrEqualTo(Carbon::parse($this->starts_at)->startOfDay()); } - public function isTesting(): bool + public function getHasStartedAttribute(): bool + { + return $this->hasStarted(); + } + + private function isTesting(): bool { return (null !== $this->test_ends_at && Carbon::now()->lessThan(Carbon::parse($this->test_ends_at))); } - public function isUpcoming(): bool + public function getIsTestingAttribute(): bool + { + return $this->isTesting(); + } + + private function isUpcoming(): bool { return $this->starts_at > Carbon::now(); } - public function isWithinPaymentToleranceTime(): bool + public function getIsUpcomingAttribute(): bool + { + return $this->isUpcoming(); + } + + private function isWithinPaymentToleranceTime(): bool { return (Carbon::now()->lessThan(Carbon::parse($this->payment_tolerance_ends_at))); } - public function isPaid(): bool + public function getIsWithinPaymentToleranceTimeAttribute(): bool + { + return $this->isWithinPaymentToleranceTime(); + } + + private function isPaid(): bool { return $this->paid_at !== null; } - public function isCancelled(): bool + public function getIsPaidAttribute(): bool + { + return $this->isPaid(); + } + + private function isCancelled(): bool { return ($this->cancelled_at !== null && $this->cancelled_at <= Carbon::now()); } - public function isPendingCancellation(): bool + public function getIsCancelledAttribute(): bool + { + return $this->isCancelled(); + } + + private function isPendingCancellation(): bool { return ($this->cancelled_at !== null && $this->cancelled_at >= Carbon::now()); } - public function isRecurring(): bool + public function getIsPendingCancellationAttribute(): bool { - return ($this->is_recurring === true); + return $this->isPendingCancellation(); } - public function isRefunded(): bool + private function isRefunded(): bool { return ($this->refunded_at !== null); } - public function isRenewed(): bool + public function getIsRefundedAttribute(): bool + { + return $this->isRefunded(); + } + + private function isRenewed(): bool { return ($this->renewed_at !== null); } - public function isExpiring(): bool + public function getIsRenewedAttribute(): bool + { + return $this->isRenewed(); + } + + private function isExpiring(): bool { return ($this->expires_at > Carbon::tomorrow()->endOfDay()->subSecond() && $this->expires_at < Carbon::tomorrow()->endOfDay()->addSecond()); } - public function hasExpired(): bool + public function getIsExpiringAttribute(): bool + { + return $this->isExpiring(); + } + + private function hasExpired(): bool { return Carbon::now()->greaterThan(Carbon::parse($this->expires_at)); } - public function isActive(): bool + public function getHasExpiredAttribute(): bool + { + return $this->hasExpired(); + } + + private function isActive(): bool { if ($this->isTesting()) { return true; diff --git a/tests/feature/CancelSubscriptionTest.php b/tests/feature/CancelSubscriptionTest.php index 8fba762..50fd6f9 100644 --- a/tests/feature/CancelSubscriptionTest.php +++ b/tests/feature/CancelSubscriptionTest.php @@ -41,12 +41,12 @@ public function can_cancel_a_subscription_immediately(): void { $subscription = $this->user->subscribeTo($this->plan, false); $subscription->markAsPaid(); - $this->assertTrue($subscription->isActive()); + $this->assertTrue($subscription->is_active); Event::fake(); $this->user->cancelSubscription(true); - $this->assertFalse($subscription->fresh()->isActive()); - $this->assertTrue($subscription->fresh()->isCancelled()); + $this->assertFalse($subscription->fresh()->is_active); + $this->assertTrue($subscription->fresh()->is_cancelled); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionCancelled::class); } @@ -58,13 +58,13 @@ public function can_cancel_a_subscription_on_expiration_date(): void { $subscription = $this->user->subscribeTo($this->plan, false); $subscription->markAsPaid(); - $this->assertTrue($subscription->isActive()); + $this->assertTrue($subscription->is_active); Event::fake(); $this->user->cancelSubscription(); - $this->assertTrue($subscription->fresh()->isActive()); - $this->assertFalse($subscription->fresh()->isCancelled()); - $this->assertTrue($subscription->fresh()->isPendingCancellation()); + $this->assertTrue($subscription->fresh()->is_active); + $this->assertFalse($subscription->fresh()->is_cancelled); + $this->assertTrue($subscription->fresh()->is_pending_cancellation); $this->assertEquals($subscription->fresh()->expires_at, $subscription->fresh()->cancelled_at); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionCancelled::class); @@ -81,7 +81,7 @@ public function can_only_cancel_active_subscriptions(): void try{ $this->user->cancelSubscription(true); } catch (SubscriptionException $e) { - $this->assertFalse($subscription->fresh()->isCancelled()); + $this->assertFalse($subscription->fresh()->is_cancelled); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionCancelled::class); return; @@ -97,7 +97,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -106,7 +106,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat $newSubscription = $this->user->migrateSubscriptionTo($durationPlan, false, true, 30); $newSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($newSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionMigrated::class); @@ -119,7 +119,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); $durationPlan = factory(Plan::class)->states('active', 'duration')->create(); Event::fake(); @@ -142,7 +142,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void { $this->user->subscribeTo($this->plan, false, 30); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -153,7 +153,7 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void $newSubscription->markAsPaid(); } catch (SubscriptionException $e) { - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionMigrated::class); diff --git a/tests/feature/ExtendSubscriptionTest.php b/tests/feature/ExtendSubscriptionTest.php index cbf8d47..56e084a 100644 --- a/tests/feature/ExtendSubscriptionTest.php +++ b/tests/feature/ExtendSubscriptionTest.php @@ -48,7 +48,7 @@ public function can_extend_an_existing_subscription(): void Event::fake(); $this->user->extendSubscription(10); - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertTrue($subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ /** @noinspection ArgumentEqualsDefaultValueInspection */ @@ -93,7 +93,7 @@ public function can_extend_an_existing_subscription_to_a_certain_date(): void Event::fake(); $this->user->extendSubscriptionTo(Carbon::parse('+ 2 weeks')); - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertTrue($subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ $this->assertEqualsWithDelta(Carbon::now()->addWeeks(2)->endOfDay(), $subscription->expires_at, 1); @@ -132,7 +132,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date() { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); sleep(1); @@ -141,7 +141,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date() $newSubscription = $this->user->migrateSubscriptionTo($monthlyPlan, true, false); $newSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($oldSubscription)); $latestSubscription = $this->user->latestSubscription(); $this->assertEquals('monthly', $latestSubscription->plan->type); @@ -155,7 +155,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -164,7 +164,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat $newSubscription = $this->user->migrateSubscriptionTo($durationPlan, false, true, 30); $newSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($newSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionMigrated::class); @@ -177,7 +177,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); $durationPlan = factory(Plan::class)->states('active', 'duration')->create(); Event::fake(); @@ -200,7 +200,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void { $this->user->subscribeTo($this->plan, false, 30); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -211,7 +211,7 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void $newSubscription->markAsPaid(); } catch (SubscriptionException $e) { - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionMigrated::class); diff --git a/tests/feature/MigrateSubscriptionTest.php b/tests/feature/MigrateSubscriptionTest.php index 6dab251..0e52d48 100644 --- a/tests/feature/MigrateSubscriptionTest.php +++ b/tests/feature/MigrateSubscriptionTest.php @@ -41,7 +41,7 @@ public function setUp(): void public function can_migrate_a_yearly_plan_in_test_phase_to_a_monthly_plan_immediately(): void { $this->user->subscribeTo($this->plan, false, 30); - $oldsubscription = $this->user->activeSubscription(); + $oldsubscription = $this->user->active_subscription; $this->assertEquals('yearly', $oldsubscription->plan->type); $monthlyPlan = factory(Plan::class)->states('active', 'monthly')->create(); @@ -49,7 +49,7 @@ public function can_migrate_a_yearly_plan_in_test_phase_to_a_monthly_plan_immedi $newSubscription = $this->user->migrateSubscriptionTo($monthlyPlan, true, true); $newSubscription->markAsPaid(); - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertEquals('monthly', $subscription->plan->type); $this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1); /** @noinspection PhpUndefinedMethodInspection */ @@ -65,7 +65,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date() { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); sleep(1); @@ -74,7 +74,7 @@ public function can_migrate_a_yearly_plan_to_a_monthly_plan_on_the_expiry_date() $newSubscription = $this->user->migrateSubscriptionTo($monthlyPlan, true, false); $newSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($oldSubscription)); $latestSubscription = $this->user->latestSubscription(); $this->assertEquals('monthly', $latestSubscription->plan->type); @@ -88,7 +88,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -97,7 +97,7 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat $newSubscription = $this->user->migrateSubscriptionTo($durationPlan, false, true, 30); $newSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($newSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionMigrated::class); @@ -110,7 +110,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ { $oldSubscription = $this->user->subscribeTo($this->plan, false); $oldSubscription->markAsPaid(); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); $durationPlan = factory(Plan::class)->states('active', 'duration')->create(); Event::fake(); @@ -133,7 +133,7 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void { $this->user->subscribeTo($this->plan, false, 30); - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertEquals('yearly', $activeSubscription->plan->type); Event::fake(); @@ -144,7 +144,7 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void $newSubscription->markAsPaid(); } catch (SubscriptionException $e) { - $activeSubscription = $this->user->activeSubscription(); + $activeSubscription = $this->user->active_subscription; $this->assertTrue($activeSubscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionMigrated::class); diff --git a/tests/feature/RenewExpiredSubscriptionTest.php b/tests/feature/RenewExpiredSubscriptionTest.php index f36fb02..8c7779f 100644 --- a/tests/feature/RenewExpiredSubscriptionTest.php +++ b/tests/feature/RenewExpiredSubscriptionTest.php @@ -48,7 +48,7 @@ public function can_renew_the_last_expired_subscription(): void Event::fake(); $this->user->renewExpiredSubscription(true); - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertEquals('yearly', $subscription->plan->type); /** @noinspection PhpUndefinedMethodInspection */ $this->assertEqualsWithDelta(Carbon::now()->addYear()->endOfDay(), $subscription->expires_at, 1); diff --git a/tests/feature/RenewExpiringSubscriptionTest.php b/tests/feature/RenewExpiringSubscriptionTest.php index 9072e68..9d8208c 100644 --- a/tests/feature/RenewExpiringSubscriptionTest.php +++ b/tests/feature/RenewExpiringSubscriptionTest.php @@ -51,10 +51,10 @@ public function can_renew_a_recurring_subscription_that_expires_tomorrow(): void $upcomingSubscription = $this->user->renewExpiringSubscription(true); $this->assertTrue($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); $this->assertTrue($this->user->upcomingSubscription()->is($upcomingSubscription)); - $this->assertTrue($upcomingSubscription->isPaid()); - $this->assertTrue($upcomingSubscription->isRenewed()); + $this->assertTrue($upcomingSubscription->is_paid); + $this->assertTrue($upcomingSubscription->is_renewed); $this->assertEqualsWithDelta(Carbon::now(), $upcomingSubscription->renewed_at, 1); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionRenewed::class); @@ -75,7 +75,7 @@ public function cannot_renew_a_recurring_subscription_that_expires_later_than_to $this->user->renewExpiringSubscription(true); } catch (SubscriptionException $e) { $this->assertFalse($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionRenewed::class); return; @@ -99,7 +99,7 @@ public function cannot_renew_a_recurring_subscription_that_expires_earlier_than_ $this->user->renewExpiringSubscription(true); } catch (SubscriptionException $e) { $this->assertFalse($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionRenewed::class); return; @@ -123,7 +123,7 @@ public function cannot_renew_a_non_recurring_subscription(): void $this->user->renewExpiringSubscription(true); } catch (SubscriptionException $e) { $this->assertFalse($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionRenewed::class); return; @@ -147,7 +147,7 @@ public function cannot_renew_an_unpaid_subscription(): void $this->user->renewExpiringSubscription(true); } catch (SubscriptionException $e) { $this->assertFalse($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionRenewed::class); return; @@ -188,7 +188,7 @@ public function cannot_renew_a_subscription_pending_cancellation(): void $this->user->renewExpiringSubscription(true); } catch (SubscriptionException $e) { $this->assertFalse($this->user->hasUpcomingSubscription()); - $this->assertTrue($this->user->activeSubscription()->is($activeSubscription)); + $this->assertTrue($this->user->active_subscription->is($activeSubscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(SubscriptionRenewed::class); return; diff --git a/tests/feature/SubscribeTest.php b/tests/feature/SubscribeTest.php index 203da4f..90d6149 100644 --- a/tests/feature/SubscribeTest.php +++ b/tests/feature/SubscribeTest.php @@ -51,17 +51,17 @@ public function can_subscribe_to_a_recurring_yearly_plan(): void $this->assertTrue($subscription->is_recurring); $this->assertNull($subscription->refunded_at); $this->assertNull($subscription->cancelled_at); - $this->assertFalse($subscription->isTesting()); - $this->assertFalse($subscription->isPaid()); - $this->assertFalse($subscription->isActive()); + $this->assertFalse($subscription->is_testing); + $this->assertFalse($subscription->is_paid); + $this->assertFalse($subscription->is_active); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(NewSubscription::class); $subscription->markAsPaid(); /** @noinspection PhpUndefinedMethodInspection */ Event::assertDispatched(SubscriptionPaymentSucceeded::class); - $this->assertTrue($subscription->fresh()->isPaid()); - $this->assertTrue($subscription->fresh()->isActive()); + $this->assertTrue($subscription->fresh()->is_paid); + $this->assertTrue($subscription->fresh()->is_active); } /** @test * @@ -78,7 +78,7 @@ public function can_subscribe_to_a_non_recurring_monthly_plan_with_test_period() Event::assertDispatched(NewSubscription::class); $this->assertCount(1, $this->user->subscriptions); /** @var Subscription $subscription */ - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertTrue($plan->is($subscription->plan)); $this->assertEqualsWithDelta(Carbon::now()->addDays(30), $subscription->starts_at, 1); /** @noinspection PhpUndefinedMethodInspection */ @@ -86,12 +86,12 @@ public function can_subscribe_to_a_non_recurring_monthly_plan_with_test_period() $this->assertFalse($subscription->is_recurring); $this->assertNull($subscription->refunded_at); $this->assertNull($subscription->cancelled_at); - $this->assertTrue($subscription->isTesting()); + $this->assertTrue($subscription->is_testing); $this->assertNull($subscription->paid_at); - $this->assertTrue($subscription->isActive()); - $this->assertFalse($subscription->isPaid()); - $this->assertFalse($subscription->isPendingCancellation()); - $this->assertFalse($subscription->isRefunded()); + $this->assertTrue($subscription->is_active); + $this->assertFalse($subscription->is_paid); + $this->assertFalse($subscription->is_pending_cancellation); + $this->assertFalse($subscription->is_refunded); } /** @test * @@ -109,7 +109,7 @@ public function can_subscribe_to_a_recurring_plan_with_set_duration(): void Event::assertDispatched(NewSubscription::class); $this->assertCount(1, $this->user->subscriptions); /** @var Subscription $subscription */ - $subscription = $this->user->activeSubscription(); + $subscription = $this->user->active_subscription; $this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1); $this->assertTrue($plan->is($subscription->plan)); /** @noinspection PhpUndefinedMethodInspection */ @@ -117,12 +117,12 @@ public function can_subscribe_to_a_recurring_plan_with_set_duration(): void $this->assertTrue($subscription->is_recurring); $this->assertNull($subscription->refunded_at); $this->assertNull($subscription->cancelled_at); - $this->assertFalse($subscription->isTesting()); + $this->assertFalse($subscription->is_testing); $this->assertEqualsWithDelta(Carbon::now(), $subscription->paid_at, 1); - $this->assertTrue($subscription->isActive()); - $this->assertTrue($subscription->isPaid()); - $this->assertFalse($subscription->isPendingCancellation()); - $this->assertFalse($subscription->isRefunded()); + $this->assertTrue($subscription->is_active); + $this->assertTrue($subscription->is_paid); + $this->assertFalse($subscription->is_pending_cancellation); + $this->assertFalse($subscription->is_refunded); } /** @test * */ @@ -155,7 +155,7 @@ public function can_not_subscribe_overlapping_active_subscription(): void } catch (SubscriptionException $e) { $this->assertCount(1, $this->user->subscriptions); /** @var Subscription $subscription */ - $this->assertTrue($this->user->activeSubscription()->is($subscription)); + $this->assertTrue($this->user->active_subscription->is($subscription)); /** @noinspection PhpUndefinedMethodInspection */ Event::assertNotDispatched(NewSubscription::class); return; diff --git a/tests/unit/SubscriptionTest.php b/tests/unit/SubscriptionTest.php index cb6ea45..d251516 100644 --- a/tests/unit/SubscriptionTest.php +++ b/tests/unit/SubscriptionTest.php @@ -47,9 +47,9 @@ public function can_get_all_active_subscriptions(): void $this->assertTrue($activeSubscriptionA->is(Subscription::active()->get()[0])); $this->assertTrue($activeSubscriptionB->is(Subscription::active()->get()[1])); $this->assertTrue($activeSubscriptionC->is(Subscription::active()->get()[2])); - $this->assertTrue($activeSubscriptionA->isActive()); - $this->assertTrue($activeSubscriptionB->isActive()); - $this->assertTrue($activeSubscriptionC->isActive()); + $this->assertTrue($activeSubscriptionA->is_active); + $this->assertTrue($activeSubscriptionB->is_active); + $this->assertTrue($activeSubscriptionC->is_active); } /** @test */ @@ -69,10 +69,10 @@ public function can_get_paid_subscriptions(): void $this->assertTrue($paidSubscriptionB->is(Subscription::paid()->get()[1])); $this->assertTrue($unpaidSubscriptionC->is(Subscription::unpaid()->get()[0])); $this->assertTrue($unpaidSubscriptionD->is(Subscription::unpaid()->get()[1])); - $this->assertTrue($paidSubscriptionA->isPaid()); - $this->assertTrue($paidSubscriptionB->isPaid()); - $this->assertFalse($unpaidSubscriptionC->isPaid()); - $this->assertFalse($unpaidSubscriptionD->isPaid()); + $this->assertTrue($paidSubscriptionA->is_paid); + $this->assertTrue($paidSubscriptionB->is_paid); + $this->assertFalse($unpaidSubscriptionC->is_paid); + $this->assertFalse($unpaidSubscriptionD->is_paid); } /** @test */ @@ -89,10 +89,10 @@ public function can_get_subscriptions_within_payment_tolerance(): void $this->assertCount(2, Subscription::withinPaymentTolerance()->get()); $this->assertTrue($subscriptionWithinPaymentToleranceA->is(Subscription::withinPaymentTolerance()->get()[0])); $this->assertTrue($paidSubscriptionWithinPaymentToleranceB->is(Subscription::withinPaymentTolerance()->get()[1])); - $this->assertTrue($subscriptionWithinPaymentToleranceA->isWithinPaymentToleranceTime()); - $this->assertTrue($paidSubscriptionWithinPaymentToleranceB->isWithinPaymentToleranceTime()); - $this->assertFalse($subscriptionOutsidePaymentToleranceC->isWithinPaymentToleranceTime()); - $this->assertFalse($subscriptionOutsidePaymentToleranceD->isWithinPaymentToleranceTime()); + $this->assertTrue($subscriptionWithinPaymentToleranceA->is_within_payment_tolerance_time); + $this->assertTrue($paidSubscriptionWithinPaymentToleranceB->is_within_payment_tolerance_time); + $this->assertFalse($subscriptionOutsidePaymentToleranceC->is_within_payment_tolerance_time); + $this->assertFalse($subscriptionOutsidePaymentToleranceD->is_within_payment_tolerance_time); } /** @test */ @@ -105,10 +105,10 @@ public function can_get_testing_subscriptions(): void $this->assertCount(2, Subscription::testing()->get()); $this->assertTrue($testingSubscriptionA->is(Subscription::testing()->get()[0])); $this->assertTrue($testingSubscriptionB->is(Subscription::testing()->get()[1])); - $this->assertTrue($testingSubscriptionA->isTesting()); - $this->assertTrue($testingSubscriptionB->isTesting()); - $this->assertFalse($activeSubscriptionC->isTesting()); - $this->assertFalse($activeSubscriptionD->isTesting()); + $this->assertTrue($testingSubscriptionA->is_testing); + $this->assertTrue($testingSubscriptionB->is_testing); + $this->assertFalse($activeSubscriptionC->is_testing); + $this->assertFalse($activeSubscriptionD->is_testing); } /** @test */ @@ -125,12 +125,12 @@ public function can_get_upcoming_subscriptions_incl_testing(): void $this->assertTrue($upcomingSubscriptionB->is(Subscription::upcoming()->get()[1])); $this->assertTrue($testingSubscriptionE->is(Subscription::upcoming()->get()[2])); $this->assertTrue($testingSubscriptionF->is(Subscription::upcoming()->get()[3])); - $this->assertTrue($upcomingSubscriptionA->isUpcoming()); - $this->assertTrue($upcomingSubscriptionB->isUpcoming()); - $this->assertFalse($activeSubscriptionC->isUpcoming()); - $this->assertFalse($activeSubscriptionD->isUpcoming()); - $this->assertTrue($testingSubscriptionE->isUpcoming()); - $this->assertTrue($testingSubscriptionF->isUpcoming()); + $this->assertTrue($upcomingSubscriptionA->is_upcoming); + $this->assertTrue($upcomingSubscriptionB->is_upcoming); + $this->assertFalse($activeSubscriptionC->is_upcoming); + $this->assertFalse($activeSubscriptionD->is_upcoming); + $this->assertTrue($testingSubscriptionE->is_upcoming); + $this->assertTrue($testingSubscriptionF->is_upcoming); } /** @test */ @@ -158,10 +158,10 @@ public function can_get_expiring_subscriptions(): void $this->assertCount(2, Subscription::expiring()->get()); $this->assertTrue($expiringSubscriptionA->is(Subscription::expiring()->get()[0])); $this->assertTrue($expiringSubscriptionB->is(Subscription::expiring()->get()[1])); - $this->assertTrue($expiringSubscriptionA->isExpiring()); - $this->assertTrue($expiringSubscriptionB->isExpiring()); - $this->assertFalse($activeSubscriptionC->isExpiring()); - $this->assertFalse($activeSubscriptionD->isExpiring()); + $this->assertTrue($expiringSubscriptionA->is_expiring); + $this->assertTrue($expiringSubscriptionB->is_expiring); + $this->assertFalse($activeSubscriptionC->is_expiring); + $this->assertFalse($activeSubscriptionD->is_expiring); } /** @test */ @@ -174,10 +174,10 @@ public function can_get_recurring_subscriptions(): void $this->assertCount(2, Subscription::recurring()->get()); $this->assertTrue($recurringSubscriptionA->is(Subscription::recurring()->get()[0])); $this->assertTrue($recurringSubscriptionB->is(Subscription::recurring()->get()[1])); - $this->assertTrue($recurringSubscriptionA->isRecurring()); - $this->assertTrue($recurringSubscriptionB->isRecurring()); - $this->assertFalse($nonRecurringSubscriptionC->isRecurring()); - $this->assertFalse($nonRecurringSubscriptionD->isRecurring()); + $this->assertTrue($recurringSubscriptionA->is_recurring); + $this->assertTrue($recurringSubscriptionB->is_recurring); + $this->assertFalse($nonRecurringSubscriptionC->is_recurring); + $this->assertFalse($nonRecurringSubscriptionD->is_recurring); } /** @test */ @@ -200,7 +200,7 @@ public function can_not_get_remaining_days_of_an_unstarted_subscription(): void 'expires_at' => Carbon::parse('+ 8 weeks') ]); - $this->assertFalse($subscription->hasStarted()); + $this->assertFalse($subscription->has_started); try { $subscription->remaining_days; @@ -219,10 +219,10 @@ public function can_cancel_immediately(): void { /** @var Subscription $subscription */ $subscription = factory(Subscription::class)->states('active')->create(); - $this->assertTrue($subscription->isActive()); + $this->assertTrue($subscription->is_active); $subscription->cancel(true); - $this->assertFalse($subscription->isActive()); - $this->assertTrue($subscription->isCancelled()); + $this->assertFalse($subscription->is_active); + $this->assertTrue($subscription->is_cancelled); } /** @test @@ -234,13 +234,13 @@ public function can_cancel_at_the_end_of_the_subscription(): void $subscription = factory(Subscription::class)->states('active')->create([ 'expires_at' => Carbon::parse('+ 1 week') ]); - $this->assertTrue($subscription->isActive()); + $this->assertTrue($subscription->is_active); /** @noinspection ArgumentEqualsDefaultValueInspection */ $subscription->cancel(false); $this->assertEquals($subscription->expires_at, $subscription->cancelled_at); - $this->assertTrue($subscription->isActive()); - $this->assertTrue($subscription->isPendingCancellation()); - $this->assertFalse($subscription->isCancelled()); + $this->assertTrue($subscription->is_active); + $this->assertTrue($subscription->is_pending_cancellation); + $this->assertFalse($subscription->is_cancelled); } /** @test */ @@ -248,7 +248,7 @@ public function can_not_cancel_an_already_cancelled_subscription(): void { /** @var Subscription $subscription */ $subscription = factory(Subscription::class)->states('cancelled')->create(); - $this->assertTrue($subscription->isCancelled()); + $this->assertTrue($subscription->is_cancelled); try { $subscription->cancel(true); } catch (SubscriptionException $e) { @@ -268,12 +268,12 @@ public function can_still_cancel_a_subscription_that_is_pending_cancellation(): /** @noinspection ArgumentEqualsDefaultValueInspection */ $subscription->cancel(false); - $this->assertTrue($subscription->isPendingCancellation()); + $this->assertTrue($subscription->is_pending_cancellation); $subscription->cancel(true); - $this->assertTrue($subscription->isCancelled()); - $this->assertFalse($subscription->isPendingCancellation()); + $this->assertTrue($subscription->is_cancelled); + $this->assertFalse($subscription->is_pending_cancellation); } /** @test */