Skip to content

Commit

Permalink
Improved overall code coverage and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhartman committed Nov 18, 2019
1 parent 54e947a commit b53e947
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function isLocal()
/**
* Determine if the subscription is fastspring.
*
* @return bool
* @return string
*/
public function isFastspring()
{
Expand Down Expand Up @@ -434,14 +434,14 @@ public function swap($plan, $prorate, $quantity = 1, $coupons = [])
return $this;
}

// else
// TODO: it might be better to create custom exception
throw new Exception('Swap operation failed. Response: ' . json_encode($response));
}

/**
* Cancel the subscription at the end of the billing period.
*
* @throws \Exception
*
* @return object Response of fastspring
*/
public function cancel()
Expand All @@ -460,14 +460,14 @@ public function cancel()
return $this;
}

// else
// TODO: it might be better to create custom exception
throw new Exception('Cancel operation failed. Response: ' . json_encode($response));
}

/**
* Cancel the subscription immediately.
*
* @throws \Exception
*
* @return object Response of fastspring
*/
public function cancelNow()
Expand All @@ -484,8 +484,6 @@ public function cancelNow()
return $this;
}

// else
// TODO: it might be better to create custom exception
throw new Exception('CancelNow operation failed. Response: ' . json_encode($response));
}

Expand Down Expand Up @@ -518,8 +516,6 @@ public function resume()
return $this;
}

// else
// TODO: it might be better to create custom exception
throw new Exception('Resume operation failed. Response: ' . json_encode($response));
}
}
143 changes: 138 additions & 5 deletions tests/SubscriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ public function testPeriods()
$this->assertEquals($subscription->periods[1]->id, $period2->id);
}

public function testActiveLocalPeriod()
{
$today = Carbon::today()->format('Y-m-d');

$user = $this->createUser(['email' => 'bilal@gultekin.me', 'fastspring_id' => 'fastspring_id']);
$subscription = $this->createSubscription($user, ['state' => 'active']);

$activePeriod = $this->createSubscriptionPeriod($subscription, [
'subscription_id' => 'fastspring_id',
'start_date' => $today,
'end_date' => $today,
'type' => 'local'
]);

$this->assertNotNull($activePeriod);
$this->assertNotNull($subscription);
$this->assertEquals($activePeriod['subscription_id'], $activePeriod->id);
$this->assertEquals($subscription->id, $activePeriod->id);
}

public function testActivePeriodOrCreateWhileThereIsOne()
{
$user = $this->createUser(['email' => 'bilal@gultekin.me', 'fastspring_id' => 'fastspring_id']);
Expand Down Expand Up @@ -222,15 +242,17 @@ public function testActivePeriodOrCreateWithNonExistIntervalUnit()
$user = $this->createUser(['email' => 'bilal@gultekin.me', 'fastspring_id' => 'fastspring_id']);
$subscription = $this->createSubscription($user, [
'state' => 'active',
'interval_unit' => 'none',
'fastspring_id' => null,
'interval_unit' => 'martian-second',
]);

if ($activePeriod) {
return $activePeriod;
}
// create a period
$lastCreatedPeriod = $this->createSubscriptionPeriod($subscription, [
'start_date' => Carbon::now()->subDays(465)->format('Y-m-d'),
'end_date' => Carbon::now()->subDays(100)->format('Y-m-d'),
]);

$activePeriod = $subscription->activePeriodOrCreate();
$lastPeriod = $subscription->activePeriodOrCreate();
}

public function testActiveSubscription()
Expand Down Expand Up @@ -356,6 +378,37 @@ public function testSwapNoProrate()
$this->assertEquals($subscription->swap_at->format('Y-m-d'), $endDate);
}

public function testSwapException()
{
$this->expectException(\Exception::class);

$this->setMockResponsesAndHistory([
new Response(200, [], json_encode([
'subscriptions' => [
[
'subscription' => 'fastspring_id',
'result' => 'error',
],
],
])),
new Response(200, [], json_encode([
[
'beginPeriodDate' => Carbon::now()->subDays(14)->format('Y-m-d'),
'endPeriodDate' => $endDate,
],
])),
]);

$user = $this->createUser([
'fastspring_id' => 'fastspring_id',
]);

$subscription = $this->createSubscription($user, ['fastspring_id' => 'fastspring_id']);

$subscription->swap('new_plan', false);
$activePeriod = $subscription->activePeriodOrCreate();
}

public function testCancel()
{
$endDate = Carbon::now()->addDays(16)->format('Y-m-d');
Expand Down Expand Up @@ -388,6 +441,38 @@ public function testCancel()
$this->assertEquals($subscription->swap_at->format('Y-m-d'), $endDate);
}

public function testCancelException()
{
$this->expectException(\Exception::class);

$endDate = Carbon::now()->addDays(16)->format('Y-m-d');

$this->setMockResponsesAndHistory([
new Response(200, [], json_encode([
'subscriptions' => [
[
'subscription' => 'fastspring_id',
'result' => 'error',
],
],
])),
new Response(200, [], json_encode([
[
'beginPeriodDate' => Carbon::now()->subDays(14)->format('Y-m-d'),
'endPeriodDate' => $endDate,
],
])),
]);

$user = $this->createUser([
'fastspring_id' => 'fastspring_id',
]);

$subscription = $this->createSubscription($user, ['fastspring_id' => 'fastspring_id']);

$subscription->cancel();
}

public function testCancelNow()
{
$this->setMockResponsesAndHistory([
Expand All @@ -411,6 +496,30 @@ public function testCancelNow()
$this->assertEquals($subscription->state, 'deactivated');
}

public function testCancelNowException()
{
$this->expectException(\Exception::class);

$this->setMockResponsesAndHistory([
new Response(200, [], json_encode([
'subscriptions' => [
[
'subscription' => 'fastspring_id',
'result' => 'error',
],
],
])),
]);

$user = $this->createUser([
'fastspring_id' => 'fastspring_id',
]);

$subscription = $this->createSubscription($user, ['fastspring_id' => 'fastspring_id']);

$subscription->cancelNow();
}

public function testResume()
{
$this->setMockResponsesAndHistory([
Expand Down Expand Up @@ -459,6 +568,30 @@ public function testTryToResumeNoncanceled()
$this->assertObjectHasAttribute('subscription', $response->subscriptions[0]);
}

public function testResumeException()
{
$this->expectException(\Exception::class);

$this->setMockResponsesAndHistory([
new Response(200, [], json_encode([
'subscriptions' => [
[
'subscription' => 'fastspring_id',
'result' => 'error',
],
],
])),
]);

$user = $this->createUser([
'fastspring_id' => 'fastspring_id',
]);

$subscription = $this->createSubscription($user, ['fastspring_id' => 'fastspring_id', 'state' => 'canceled']);

$subscription->resume();
}

public function testType()
{
$user = $this->createUser([
Expand Down

0 comments on commit b53e947

Please sign in to comment.