Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/WalletTest.php
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | class WalletTest extends TestCase | ||||
/** | /** | ||||
* Verify a wallet is created, when a user is created. | * Verify a wallet is created, when a user is created. | ||||
*/ | */ | ||||
public function testCreateUserCreatesWallet(): void | public function testCreateUserCreatesWallet(): void | ||||
{ | { | ||||
$user = $this->getTestUser('UserWallet1@UserWallet.com'); | $user = $this->getTestUser('UserWallet1@UserWallet.com'); | ||||
$this->assertTrue($user->wallets()->count() == 1); | $this->assertCount(1, $user->wallets); | ||||
} | } | ||||
/** | /** | ||||
* Verify a user can haz more wallets. | * Verify a user can haz more wallets. | ||||
*/ | */ | ||||
public function testAddWallet(): void | public function testAddWallet(): void | ||||
{ | { | ||||
$user = $this->getTestUser('UserWallet2@UserWallet.com'); | $user = $this->getTestUser('UserWallet2@UserWallet.com'); | ||||
$user->wallets()->save( | $user->wallets()->save( | ||||
new Wallet(['currency' => 'USD']) | new Wallet(['currency' => 'USD']) | ||||
); | ); | ||||
$this->assertTrue($user->wallets()->count() >= 2); | $this->assertCount(2, $user->wallets); | ||||
$user->wallets()->each( | $user->wallets()->each( | ||||
function ($wallet) { | function ($wallet) { | ||||
$this->assertTrue($wallet->balance === 0.00); | $this->assertEquals(0, $wallet->balance); | ||||
} | } | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Verify we can not delete a user wallet that holds balance. | * Verify we can not delete a user wallet that holds balance. | ||||
*/ | */ | ||||
public function testDeleteWalletWithCredit(): void | public function testDeleteWalletWithCredit(): void | ||||
{ | { | ||||
$user = $this->getTestUser('UserWallet3@UserWallet.com'); | $user = $this->getTestUser('UserWallet3@UserWallet.com'); | ||||
$user->wallets()->each( | $user->wallets()->each( | ||||
function ($wallet) { | function ($wallet) { | ||||
$wallet->credit(1.00)->save(); | $wallet->credit(100)->save(); | ||||
} | } | ||||
); | ); | ||||
$user->wallets()->each( | $user->wallets()->each( | ||||
function ($wallet) { | function ($wallet) { | ||||
$this->assertFalse($wallet->delete()); | $this->assertFalse($wallet->delete()); | ||||
} | } | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Verify we can not delete a wallet that is the last wallet. | * Verify we can not delete a wallet that is the last wallet. | ||||
*/ | */ | ||||
public function testDeleteLastWallet(): void | public function testDeleteLastWallet(): void | ||||
{ | { | ||||
$user = $this->getTestUser('UserWallet4@UserWallet.com'); | $user = $this->getTestUser('UserWallet4@UserWallet.com'); | ||||
$this->assertTrue($user->wallets()->count() == 1); | $this->assertCount(1, $user->wallets); | ||||
$user->wallets()->each( | $user->wallets()->each( | ||||
function ($wallet) { | function ($wallet) { | ||||
$this->assertFalse($wallet->delete()); | $this->assertFalse($wallet->delete()); | ||||
} | } | ||||
); | ); | ||||
} | } | ||||
Show All 26 Lines | public function testAddWalletController(): void | ||||
$userB = $this->getTestUser('WalletControllerB@WalletController.com'); | $userB = $this->getTestUser('WalletControllerB@WalletController.com'); | ||||
$userA->wallets()->each( | $userA->wallets()->each( | ||||
function ($wallet) use ($userB) { | function ($wallet) use ($userB) { | ||||
$wallet->addController($userB); | $wallet->addController($userB); | ||||
} | } | ||||
); | ); | ||||
$this->assertTrue( | $this->assertCount(1, $userB->accounts); | ||||
$userB->accounts()->count() == 1, | |||||
"number of accounts (1 expected): {$userB->accounts()->count()}" | |||||
); | |||||
$aWallet = $userA->wallets()->get(); | $aWallet = $userA->wallets()->first(); | ||||
$bAccount = $userB->accounts()->get(); | $bAccount = $userB->accounts()->first(); | ||||
$this->assertTrue($bAccount[0]->id === $aWallet[0]->id); | $this->assertTrue($bAccount->id === $aWallet->id); | ||||
} | } | ||||
/** | /** | ||||
* Verify controllers can also be removed from wallets. | * Verify controllers can also be removed from wallets. | ||||
*/ | */ | ||||
public function testRemoveWalletController(): void | public function testRemoveWalletController(): void | ||||
{ | { | ||||
$userA = $this->getTestUser('WalletController2A@WalletController.com'); | $userA = $this->getTestUser('WalletController2A@WalletController.com'); | ||||
$userB = $this->getTestUser('WalletController2B@WalletController.com'); | $userB = $this->getTestUser('WalletController2B@WalletController.com'); | ||||
$userA->wallets()->each( | $userA->wallets()->each( | ||||
function ($wallet) use ($userB) { | function ($wallet) use ($userB) { | ||||
$wallet->addController($userB); | $wallet->addController($userB); | ||||
} | } | ||||
); | ); | ||||
$userB->refresh(); | $userB->refresh(); | ||||
$userB->accounts()->each( | $userB->accounts()->each( | ||||
function ($wallet) use ($userB) { | function ($wallet) use ($userB) { | ||||
$wallet->removeController($userB); | $wallet->removeController($userB); | ||||
} | } | ||||
); | ); | ||||
$this->assertTrue($userB->accounts()->count() == 0); | $this->assertCount(0, $userB->accounts); | ||||
} | } | ||||
} | } |