Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/EntitlementTest.php
Show First 20 Lines • Show All 144 Lines • ▼ Show 20 Lines | public function testBillDeletedEntitlement(): void | ||||
// some additional SKUs so we have something to delete. | // some additional SKUs so we have something to delete. | ||||
$user->assignSku($storage, 4); | $user->assignSku($storage, 4); | ||||
// the mailbox, the groupware, the 2 original storage and the additional 4 | // the mailbox, the groupware, the 2 original storage and the additional 4 | ||||
$this->assertCount(8, $user->fresh()->entitlements); | $this->assertCount(8, $user->fresh()->entitlements); | ||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
$this->backdateEntitlements($user->entitlements, Carbon::now()->subWeeks(7)); | $backdate = Carbon::now()->subWeeks(7); | ||||
$this->backdateEntitlements($user->entitlements, $backdate); | |||||
$charge = $wallet->chargeEntitlements(); | $charge = $wallet->chargeEntitlements(); | ||||
$this->assertTrue($wallet->balance < 0); | $this->assertSame(-1099, $wallet->balance); | ||||
$balance = $wallet->balance; | $balance = $wallet->balance; | ||||
$discount = \App\Discount::where('discount', 30)->first(); | |||||
$wallet->discount()->associate($discount); | |||||
$wallet->save(); | |||||
$user->removeSku($storage, 4); | $user->removeSku($storage, 4); | ||||
// we expect the wallet to have been charged. | // we expect the wallet to have been charged for ~3 weeks of use of | ||||
$this->assertTrue($wallet->fresh()->balance < $balance); | // 4 deleted storage entitlements, it should also take discount into account | ||||
$backdate->addMonthsWithoutOverflow(1); | |||||
$diffInDays = $backdate->diffInDays(Carbon::now()); | |||||
// entitlements-num * cost * discount * days-in-month | |||||
$max = intval(4 * 25 * 0.7 * $diffInDays / 28); | |||||
$min = intval(4 * 25 * 0.7 * $diffInDays / 31); | |||||
$wallet->refresh(); | |||||
$this->assertTrue($wallet->balance >= $balance - $max); | |||||
$this->assertTrue($wallet->balance <= $balance - $min); | |||||
$transactions = \App\Transaction::where('object_id', $wallet->id) | $transactions = \App\Transaction::where('object_id', $wallet->id) | ||||
->where('object_type', \App\Wallet::class)->get(); | ->where('object_type', \App\Wallet::class)->get(); | ||||
// one round of the monthly invoicing, four sku deletions getting invoiced | // one round of the monthly invoicing, four sku deletions getting invoiced | ||||
$this->assertCount(5, $transactions); | $this->assertCount(5, $transactions); | ||||
} | } | ||||
} | } |