Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/BillingTest.php
- This file was added.
<?php | |||||
namespace Tests\Feature; | |||||
use Carbon\Carbon; | |||||
use Illuminate\Support\Facades\Queue; | |||||
use Tests\TestCase; | |||||
class BillingTest extends TestCase | |||||
{ | |||||
public function setUp(): void | |||||
{ | |||||
parent::setUp(); | |||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$this->user = $this->getTestUser('jane@kolabnow.com'); | |||||
$this->package = \App\Package::where('title', 'kolab')->first(); | |||||
$this->user->assignPackage($this->package); | |||||
$this->wallet = $this->user->wallets->first(); | |||||
$this->wallet_id = $this->wallet->id; | |||||
} | |||||
public function tearDown(): void | |||||
{ | |||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$this->deleteTestUser('jack@kolabnow.com'); | |||||
parent::tearDown(); | |||||
} | |||||
public function testTouchAndGo(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
$this->assertCount(4, $changelogs); | |||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
$this->assertCount(8, $changelogs); | |||||
$costs = $changelogs->sum('cost'); | |||||
$this->assertTrue($costs == 0); | |||||
} | |||||
public function testNearFullTrial(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subDays(13); | |||||
$entry->updated_at = Carbon::now()->subDays(13); | |||||
$entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
$this->deleteTestUser('jane@kolabnow.com'); | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
$this->assertCount(8, $changelogs); | |||||
$costs = $changelogs->sum('cost'); | |||||
$this->assertTrue($costs == 0); | |||||
$this->assertTrue($this->wallet->expectedCharges() == 0); | |||||
} | |||||
public function testFullTrial(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subDays(14); | |||||
$entry->updated_at = Carbon::now()->subDays(14); | |||||
$entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
$this->assertTrue($this->wallet->expectedCharges() == 999); | |||||
} | |||||
public function testAddtStorage(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subDays(16); | |||||
$entry->updated_at = Carbon::now()->subDays(16); | |||||
$entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
$this->assertTrue($this->wallet->expectedCharges() == 999); | |||||
$entitlement = \App\Entitlement::create( | |||||
[ | |||||
'owner_id' => $this->user->id, | |||||
'wallet_id' => $this->wallet_id, | |||||
'sku_id' => \App\Sku::firstOrCreate(['title' => 'storage'])->id, | |||||
'cost' => 25, | |||||
'entitleable_id' => $this->user->id, | |||||
'entitleable_type' => \App\User::class | |||||
] | |||||
); | |||||
$changelog = \App\Changelog::where('wallet_id', $this->wallet_id)->latest()->first(); | |||||
$changelog->created_at = Carbon::now()->subDays(14); | |||||
$changelog->updated_at = Carbon::now()->subDays(14); | |||||
$changelog->save(); | |||||
$this->assertTrue($this->wallet->expectedCharges() == 1024); | |||||
} | |||||
public function testOutRunTrial(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subDays(15); | |||||
$entry->updated_at = Carbon::now()->subDays(15); | |||||
$entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
$this->assertTrue($this->wallet->expectedCharges() >= 999); | |||||
} | |||||
public function testFifthWeek(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subWeeks(5); | |||||
$entry->updated_at = Carbon::now()->subWeeks(5); | |||||
$entry->save(); | |||||
$addt_entry = \App\Changelog::create( | |||||
[ | |||||
'wallet_id' => $entry->wallet_id, | |||||
'sku_id' => $entry->sku_id, | |||||
'action' => 'charging', | |||||
'cost' => $entry->cost | |||||
] | |||||
); | |||||
$addt_entry->created_at = Carbon::now()->subMonths(1); | |||||
$addt_entry->updated_at = Carbon::now()->subMonths(1); | |||||
$addt_entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
// A fresh query should now return 8 entries. | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
$this->assertCount(8, $changelogs); | |||||
$this->assertTrue($this->wallet->expectedCharges() == 0); | |||||
} | |||||
public function testSecondMonth(): void | |||||
{ | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subMonths(2); | |||||
$entry->updated_at = Carbon::now()->subMonths(2); | |||||
$entry->save(); | |||||
$addt_entry = \App\Changelog::create( | |||||
[ | |||||
'wallet_id' => $entry->wallet_id, | |||||
'sku_id' => $entry->sku_id, | |||||
'action' => 'charging', | |||||
'cost' => $entry->cost | |||||
] | |||||
); | |||||
$addt_entry->created_at = Carbon::now()->subMonths(1); | |||||
$addt_entry->updated_at = Carbon::now()->subMonths(1); | |||||
$addt_entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
// A fresh query should now return 8 entries. | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id)->get(); | |||||
$this->assertCount(8, $changelogs); | |||||
$this->assertTrue($this->wallet->expectedCharges() >= 999); | |||||
$changelogs = \App\Changelog::where('wallet_id', $this->wallet_id) | |||||
->where('action', 'charging')->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->updated_at = Carbon::now(); | |||||
$entry->save(); | |||||
} | |||||
$this->assertTrue($this->wallet->expectedCharges() == 999); | |||||
} | |||||
public function testWithDiscount(): void | |||||
{ | |||||
$package = \App\Package::create( | |||||
[ | |||||
'title' => 'kolab-kube', | |||||
'description' => 'Kolab for Kube fans', | |||||
'discount_rate' => 50 | |||||
] | |||||
); | |||||
$skus = [ | |||||
\App\Sku::firstOrCreate(['title' => 'mailbox']), | |||||
\App\Sku::firstOrCreate(['title' => 'storage']), | |||||
\App\Sku::firstOrCreate(['title' => 'groupware']) | |||||
]; | |||||
$package->skus()->saveMany($skus); | |||||
$package->skus()->updateExistingPivot( | |||||
\App\Sku::firstOrCreate(['title' => 'storage']), | |||||
['qty' => 2], | |||||
false | |||||
); | |||||
$user = $this->getTestUser('jack@kolabnow.com'); | |||||
$package = \App\Package::where('title', 'kolab-kube')->first(); | |||||
$user->assignPackage($package); | |||||
$wallet = $user->wallets->first(); | |||||
$wallet_id = $wallet->id; | |||||
$changelogs = \App\Changelog::where('wallet_id', $wallet_id)->get(); | |||||
foreach ($changelogs as $entry) { | |||||
$entry->created_at = Carbon::now()->subDays(15); | |||||
$entry->updated_at = Carbon::now()->subDays(15); | |||||
$entry->save(); | |||||
} | |||||
$this->assertCount(4, $changelogs); | |||||
$this->assertTrue($wallet->expectedCharges() == 500); | |||||
} | |||||
} |