Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/BillingTest.php
Show First 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | class BillingTest extends TestCase | ||||
/** | /** | ||||
* Verify the last day before the end of a full month's trial. | * Verify the last day before the end of a full month's trial. | ||||
*/ | */ | ||||
public function testNearFullTrial(): void | public function testNearFullTrial(): void | ||||
{ | { | ||||
$this->backdateEntitlements( | $this->backdateEntitlements( | ||||
$this->wallet->entitlements, | $this->wallet->entitlements, | ||||
Carbon::now()->subMonths(1)->addDays(1) | Carbon::now()->subMonthsWithoutOverflow(1)->addDays(1) | ||||
); | ); | ||||
$this->assertEquals(0, $this->wallet->expectedCharges()); | $this->assertEquals(0, $this->wallet->expectedCharges()); | ||||
} | } | ||||
/** | /** | ||||
* Verify the exact end of the month's trial. | * Verify the exact end of the month's trial. | ||||
*/ | */ | ||||
public function testFullTrial(): void | public function testFullTrial(): void | ||||
{ | { | ||||
$this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonths(1)); | $this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonthsWithoutOverflow(1)); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
} | } | ||||
/** | /** | ||||
* Verify that over-running the trial by a single day causes charges to be incurred. | * Verify that over-running the trial by a single day causes charges to be incurred. | ||||
*/ | */ | ||||
public function testOutRunTrial(): void | public function testOutRunTrial(): void | ||||
{ | { | ||||
$this->backdateEntitlements( | $this->backdateEntitlements( | ||||
$this->wallet->entitlements, | $this->wallet->entitlements, | ||||
Carbon::now()->subMonths(1)->subDays(1) | Carbon::now()->subMonthsWithoutOverflow(1)->subDays(1) | ||||
); | ); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
} | } | ||||
/** | /** | ||||
* Verify additional storage configuration entitlement created 'early' does incur additional | * Verify additional storage configuration entitlement created 'early' does incur additional | ||||
* charges to the wallet. | * charges to the wallet. | ||||
*/ | */ | ||||
public function testAddtStorageEarly(): void | public function testAddtStorageEarly(): void | ||||
{ | { | ||||
$this->backdateEntitlements( | $this->backdateEntitlements( | ||||
$this->wallet->entitlements, | $this->wallet->entitlements, | ||||
Carbon::now()->subMonths(1)->subDays(1) | Carbon::now()->subMonthsWithoutOverflow(1)->subDays(1) | ||||
); | ); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
$sku = \App\Sku::where(['title' => 'storage'])->first(); | $sku = \App\Sku::where(['title' => 'storage'])->first(); | ||||
$entitlement = \App\Entitlement::create( | $entitlement = \App\Entitlement::create( | ||||
[ | [ | ||||
'wallet_id' => $this->wallet_id, | 'wallet_id' => $this->wallet_id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $sku->cost, | 'cost' => $sku->cost, | ||||
'entitleable_id' => $this->user->id, | 'entitleable_id' => $this->user->id, | ||||
'entitleable_type' => \App\User::class | 'entitleable_type' => \App\User::class | ||||
] | ] | ||||
); | ); | ||||
$this->backdateEntitlements( | $this->backdateEntitlements( | ||||
[$entitlement], | [$entitlement], | ||||
Carbon::now()->subMonths(1)->subDays(1) | Carbon::now()->subMonthsWithoutOverflow(1)->subDays(1) | ||||
); | ); | ||||
$this->assertEquals(1024, $this->wallet->expectedCharges()); | $this->assertEquals(1024, $this->wallet->expectedCharges()); | ||||
} | } | ||||
/** | /** | ||||
* Verify additional storage configuration entitlement created 'late' does not incur additional | * Verify additional storage configuration entitlement created 'late' does not incur additional | ||||
* charges to the wallet. | * charges to the wallet. | ||||
*/ | */ | ||||
public function testAddtStorageLate(): void | public function testAddtStorageLate(): void | ||||
{ | { | ||||
$this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonths(1)); | $this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonthsWithoutOverflow(1)); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
$sku = \App\Sku::where(['title' => 'storage'])->first(); | $sku = \App\Sku::where(['title' => 'storage'])->first(); | ||||
$entitlement = \App\Entitlement::create( | $entitlement = \App\Entitlement::create( | ||||
[ | [ | ||||
'wallet_id' => $this->wallet_id, | 'wallet_id' => $this->wallet_id, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'cost' => $sku->cost, | 'cost' => $sku->cost, | ||||
'entitleable_id' => $this->user->id, | 'entitleable_id' => $this->user->id, | ||||
'entitleable_type' => \App\User::class | 'entitleable_type' => \App\User::class | ||||
] | ] | ||||
); | ); | ||||
$this->backdateEntitlements([$entitlement], Carbon::now()->subDays(14)); | $this->backdateEntitlements([$entitlement], Carbon::now()->subDays(14)); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
} | } | ||||
public function testFifthWeek(): void | public function testFifthWeek(): void | ||||
{ | { | ||||
$targetDateA = Carbon::now()->subWeeks(5); | $targetDateA = Carbon::now()->subWeeks(5); | ||||
$targetDateB = $targetDateA->copy()->addMonths(1); | $targetDateB = $targetDateA->copy()->addMonthsWithoutOverflow(1); | ||||
$this->backdateEntitlements($this->wallet->entitlements, $targetDateA); | $this->backdateEntitlements($this->wallet->entitlements, $targetDateA); | ||||
$this->assertEquals(999, $this->wallet->expectedCharges()); | $this->assertEquals(999, $this->wallet->expectedCharges()); | ||||
$this->wallet->chargeEntitlements(); | $this->wallet->chargeEntitlements(); | ||||
$this->assertEquals(-999, $this->wallet->balance); | $this->assertEquals(-999, $this->wallet->balance); | ||||
foreach ($this->wallet->entitlements()->get() as $entitlement) { | foreach ($this->wallet->entitlements()->get() as $entitlement) { | ||||
$this->assertTrue($entitlement->created_at->isSameSecond($targetDateA)); | $this->assertTrue($entitlement->created_at->isSameSecond($targetDateA)); | ||||
$this->assertTrue($entitlement->updated_at->isSameSecond($targetDateB)); | $this->assertTrue($entitlement->updated_at->isSameSecond($targetDateB)); | ||||
} | } | ||||
} | } | ||||
public function testSecondMonth(): void | public function testSecondMonth(): void | ||||
{ | { | ||||
$this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonths(2)); | $this->backdateEntitlements($this->wallet->entitlements, Carbon::now()->subMonthsWithoutOverflow(2)); | ||||
$this->assertCount(4, $this->wallet->entitlements); | $this->assertCount(4, $this->wallet->entitlements); | ||||
$this->assertEquals(1998, $this->wallet->expectedCharges()); | $this->assertEquals(1998, $this->wallet->expectedCharges()); | ||||
$sku = \App\Sku::where(['title' => 'storage'])->first(); | $sku = \App\Sku::where(['title' => 'storage'])->first(); | ||||
$entitlement = \App\Entitlement::create( | $entitlement = \App\Entitlement::create( | ||||
[ | [ | ||||
'entitleable_id' => $this->user->id, | 'entitleable_id' => $this->user->id, | ||||
'entitleable_type' => \App\User::class, | 'entitleable_type' => \App\User::class, | ||||
'cost' => $sku->cost, | 'cost' => $sku->cost, | ||||
'sku_id' => $sku->id, | 'sku_id' => $sku->id, | ||||
'wallet_id' => $this->wallet_id | 'wallet_id' => $this->wallet_id | ||||
] | ] | ||||
); | ); | ||||
$this->backdateEntitlements([$entitlement], Carbon::now()->subMonths(1)); | $this->backdateEntitlements([$entitlement], Carbon::now()->subMonthsWithoutOverflow(1)); | ||||
$this->assertEquals(2023, $this->wallet->expectedCharges()); | $this->assertEquals(2023, $this->wallet->expectedCharges()); | ||||
} | } | ||||
public function testWithDiscount(): void | public function testWithDiscount(): void | ||||
{ | { | ||||
$package = \App\Package::create( | $package = \App\Package::create( | ||||
[ | [ | ||||
Show All 21 Lines | public function testWithDiscount(): void | ||||
$user = $this->getTestUser('jack@kolabnow.com'); | $user = $this->getTestUser('jack@kolabnow.com'); | ||||
$user->assignPackage($package); | $user->assignPackage($package); | ||||
$wallet = $user->wallets->first(); | $wallet = $user->wallets->first(); | ||||
$wallet_id = $wallet->id; | $wallet_id = $wallet->id; | ||||
$this->backdateEntitlements($wallet->entitlements, Carbon::now()->subMonths(1)); | $this->backdateEntitlements($wallet->entitlements, Carbon::now()->subMonthsWithoutOverflow(1)); | ||||
$this->assertEquals(500, $wallet->expectedCharges()); | $this->assertEquals(500, $wallet->expectedCharges()); | ||||
} | } | ||||
} | } |