Skip to content

Commit

Permalink
tests for subscribing or migrating to free plans
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Baettig committed Feb 7, 2020
1 parent 33e1476 commit f2e0552
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
28 changes: 22 additions & 6 deletions tests/feature/MigrateSubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ public function can_migrate_a_yearly_plan_in_test_phase_to_a_monthly_plan_immedi
$subscription = $this->user->active_subscription;
$this->assertEquals('monthly', $subscription->plan->type);
$this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1);
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(NewSubscription::class);
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionMigrated::class);
}

Expand Down Expand Up @@ -99,7 +97,6 @@ public function can_migrate_a_yearly_plan_to_a_non_recurring_plan_with_set_durat

$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($newSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(SubscriptionMigrated::class);
}

Expand All @@ -119,7 +116,6 @@ public function can_not_migrate_a_yearly_plan_to_a_non_recurring_plan_with_zero_
$this->user->migrateSubscriptionTo($durationPlan, false, true, 0);
} catch (SubscriptionException $e) {
$this->assertTrue($activeSubscription->is($oldSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionMigrated::class);
return;
}
Expand All @@ -146,7 +142,6 @@ public function cannot_migrate_a_testing_subscription_on_the_expiry_date(): void
} catch (SubscriptionException $e) {
$activeSubscription = $this->user->active_subscription;
$this->assertTrue($activeSubscription->is($activeSubscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionMigrated::class);
return;
}
Expand All @@ -166,11 +161,32 @@ public function can_only_migrate_active_subscriptions(): void
$this->user->migrateSubscriptionTo($monthlyPlan, true, false);
} catch (SubscriptionException $e) {
$this->assertFalse($this->user->hasActiveSubscription());
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(SubscriptionMigrated::class);
return;
}

$this->fail();
}

/** @test *
* @throws SubscriptionException
*/
public function migrations_to_free_plans_are_marked_as_paid(): void
{
$this->user->subscribeTo($this->plan, false, 30);
$oldsubscription = $this->user->active_subscription;
$this->assertEquals('yearly', $oldsubscription->plan->type);
$monthlyPlan = factory(Plan::class)->states('active', 'monthly')->create(['price' => 0]);

Event::fake();
$this->user->migrateSubscriptionTo($monthlyPlan, true, true);

$subscription = $this->user->active_subscription;
$this->assertEquals('monthly', $subscription->plan->type);
$this->assertTrue($subscription->is_paid);
$this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1);
Event::assertNotDispatched(NewSubscription::class);
Event::assertDispatched(SubscriptionMigrated::class);
}

}
28 changes: 17 additions & 11 deletions tests/feature/SubscribeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public function can_subscribe_to_a_recurring_yearly_plan(): void
/** @var Subscription $subscription */
$subscription = $this->user->active_or_last_subscription;
$this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1);
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEqualsWithDelta(Carbon::now()->addYear()->endOfDay(), $subscription->expires_at, 1);
$this->assertTrue($plan->is($subscription->plan));
$this->assertEquals($plan->price, $subscription->price);
$this->assertEquals($plan->currency, $subscription->currency);
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEquals(Carbon::now()->addYear()->diffInDays(Carbon::now()), $subscription->remaining_days);

$this->assertTrue($subscription->is_recurring);
Expand All @@ -54,11 +52,9 @@ public function can_subscribe_to_a_recurring_yearly_plan(): void
$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()->is_paid);
$this->assertTrue($subscription->fresh()->is_active);
Expand All @@ -74,14 +70,12 @@ public function can_subscribe_to_a_non_recurring_monthly_plan_with_test_period()

$this->user->subscribeTo($plan, false,30);

/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(NewSubscription::class);
$this->assertCount(1, $this->user->subscriptions);
/** @var Subscription $subscription */
$subscription = $this->user->active_subscription;
$this->assertTrue($plan->is($subscription->plan));
$this->assertEqualsWithDelta(Carbon::now()->addDays(30), $subscription->starts_at, 1);
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEqualsWithDelta(Carbon::now()->addDays(30)->addMonth()->endOfDay(), $subscription->expires_at, 1);
$this->assertFalse($subscription->is_recurring);
$this->assertNull($subscription->refunded_at);
Expand All @@ -105,14 +99,12 @@ public function can_subscribe_to_a_recurring_plan_with_set_duration(): void
$subscription = $this->user->subscribeTo($plan, true,0, 10);
$subscription->markAsPaid();

/** @noinspection PhpUndefinedMethodInspection */
Event::assertDispatched(NewSubscription::class);
$this->assertCount(1, $this->user->subscriptions);
/** @var Subscription $subscription */
$subscription = $this->user->active_subscription;
$this->assertEqualsWithDelta(Carbon::now(), $subscription->starts_at, 1);
$this->assertTrue($plan->is($subscription->plan));
/** @noinspection PhpUndefinedMethodInspection */
$this->assertEqualsWithDelta(Carbon::now()->addDays(10)->endOfDay(), $subscription->expires_at, 1);
$this->assertTrue($subscription->is_recurring);
$this->assertNull($subscription->refunded_at);
Expand All @@ -135,7 +127,6 @@ public function can_not_subscribe_to_a_plan_with_zero_day_duration(): void
} catch (SubscriptionException $e) {
$this->assertCount(0, $this->user->subscriptions);
$this->assertCount(0, Subscription::all());
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(NewSubscription::class);
return;
}
Expand All @@ -156,7 +147,6 @@ public function can_not_subscribe_overlapping_active_subscription(): void
$this->assertCount(1, $this->user->subscriptions);
/** @var Subscription $subscription */
$this->assertTrue($this->user->active_subscription->is($subscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(NewSubscription::class);
return;
}
Expand All @@ -177,10 +167,26 @@ public function can_not_subscribe_overlapping_other_future_subscription(): void
$this->assertCount(1, $this->user->subscriptions);
/** @noinspection PhpUndefinedVariableInspection */
$this->assertTrue($this->user->upcoming_subscription->is($subscription));
/** @noinspection PhpUndefinedMethodInspection */
Event::assertNotDispatched(NewSubscription::class);
return;
}
$this->fail('SubscriptionException expected');
}

/** @test *
* @throws SubscriptionException
*/
public function subscriptions_for_free_plans_are_marked_as_paid(): void
{
Event::fake();
$plan = factory(Plan::class)->states(['active', 'yearly'])->create(['price' => 0]);
$this->user->subscribeTo($plan);
Event::assertDispatched(NewSubscription::class);
$this->assertCount(1, $this->user->subscriptions);
/** @var Subscription $subscription */
$subscription = $this->user->active_or_last_subscription;

$this->assertTrue($subscription->fresh()->is_paid);
$this->assertTrue($subscription->fresh()->is_active);
}
}

0 comments on commit f2e0552

Please sign in to comment.