Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/WalletTest.php
<?php | <?php | ||||
namespace Tests\Feature; | namespace Tests\Feature; | ||||
use App\Package; | use App\Package; | ||||
use App\User; | use App\User; | ||||
use App\Sku; | use App\Sku; | ||||
use App\Wallet; | use App\Wallet; | ||||
use Carbon\Carbon; | |||||
use Tests\TestCase; | use Tests\TestCase; | ||||
use Illuminate\Foundation\Testing\WithFaker; | use Illuminate\Foundation\Testing\WithFaker; | ||||
use Illuminate\Foundation\Testing\RefreshDatabase; | use Illuminate\Foundation\Testing\RefreshDatabase; | ||||
class WalletTest extends TestCase | class WalletTest extends TestCase | ||||
{ | { | ||||
private $users = [ | private $users = [ | ||||
'UserWallet1@UserWallet.com', | 'UserWallet1@UserWallet.com', | ||||
Show All 21 Lines | class WalletTest extends TestCase | ||||
{ | { | ||||
foreach ($this->users as $user) { | foreach ($this->users as $user) { | ||||
$this->deleteTestUser($user); | $this->deleteTestUser($user); | ||||
} | } | ||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | |||||
* Test for Wallet::balanceLastsUntil() | |||||
*/ | |||||
public function testBalanceLastsUntil(): void | public function testBalanceLastsUntil(): void | ||||
{ | { | ||||
// Monthly cost of all entitlements: 999 | |||||
// 28 days: 35.68 per day | |||||
// 31 days: 32.22 per day | |||||
$user = $this->getTestUser('jane@kolabnow.com'); | $user = $this->getTestUser('jane@kolabnow.com'); | ||||
$package = Package::where('title', 'kolab')->first(); | $package = Package::where('title', 'kolab')->first(); | ||||
$mailbox = Sku::where('title', 'mailbox')->first(); | |||||
$user->assignPackage($package); | $user->assignPackage($package); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
// User/entitlements created today, balance=0 | |||||
$until = $wallet->balanceLastsUntil(); | |||||
$this->assertSame(Carbon::now()->toDateString(), $until->toDateString()); | |||||
// User/entitlements created today, balance=-10 CHF | |||||
$wallet->balance = -1000; | |||||
$until = $wallet->balanceLastsUntil(); | |||||
$this->assertSame(null, $until); | |||||
// User/entitlements created today, balance=-9,99 CHF (monthly cost) | |||||
$wallet->balance = 999; | |||||
$until = $wallet->balanceLastsUntil(); | $until = $wallet->balanceLastsUntil(); | ||||
// TODO: Test this for typical cases | $daysInLastMonth = \App\Utils::daysInLastMonth(); | ||||
// TODO: Test this for a user with no entitlements | |||||
// TODO: Test this for a user with 100% discount | $this->assertSame( | ||||
$this->markTestIncomplete(); | Carbon::now()->addDays($daysInLastMonth)->toDateString(), | ||||
$until->toDateString() | |||||
); | |||||
// Old entitlements, 100% discount | |||||
$this->backdateEntitlements($wallet->entitlements, Carbon::now()->subDays(40)); | |||||
$discount = \App\Discount::where('discount', 100)->first(); | |||||
$wallet->discount()->associate($discount); | |||||
$until = $wallet->refresh()->balanceLastsUntil(); | |||||
$this->assertSame(null, $until); | |||||
// User with no entitlements | |||||
$wallet->discount()->dissociate($discount); | |||||
$wallet->entitlements()->delete(); | |||||
$until = $wallet->refresh()->balanceLastsUntil(); | |||||
$this->assertSame(null, $until); | |||||
} | } | ||||
/** | |||||
* Test for Wallet::costsPerDay() | |||||
*/ | |||||
public function testCostsPerDay(): void | public function testCostsPerDay(): void | ||||
{ | { | ||||
// 999 | // 999 | ||||
// 28 days: 35.68 | // 28 days: 35.68 | ||||
// 31 days: 32.22 | // 31 days: 32.22 | ||||
$user = $this->getTestUser('jane@kolabnow.com'); | $user = $this->getTestUser('jane@kolabnow.com'); | ||||
$package = Package::where('title', 'kolab')->first(); | $package = Package::where('title', 'kolab')->first(); | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |