Initial commit

This commit is contained in:
James
2024-12-03 21:27:44 +01:00
commit 613e1a767c
125 changed files with 16298 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
use App\Actions\PerformWalletTransaction;
use App\Enums\WalletTransactionType;
use App\Exceptions\InsufficientBalance;
use App\Models\Wallet;
use function Pest\Laravel\assertDatabaseCount;
use function Pest\Laravel\assertDatabaseHas;
beforeEach(function () {
$this->action = app(PerformWalletTransaction::class);
});
test('perform a credit transaction', function () {
$wallet = Wallet::factory()->forUser()->richChillGuy()->create();
$this->action->execute($wallet, WalletTransactionType::CREDIT, 100, 'test');
expect($wallet->balance)->toBe(1_000_100);
assertDatabaseHas('wallets', [
'id' => $wallet->id,
'balance' => 1_000_100,
]);
assertDatabaseHas('wallet_transactions', [
'amount' => 100,
'wallet_id' => $wallet->id,
'type' => WalletTransactionType::CREDIT,
'reason' => 'test',
]);
});
test('perform a debit transaction', function () {
$wallet = Wallet::factory()->forUser()->richChillGuy()->create();
$this->action->execute($wallet, WalletTransactionType::DEBIT, 100, 'test');
expect($wallet->balance)->toBe(999_900);
assertDatabaseHas('wallets', [
'id' => $wallet->id,
'balance' => 999_900,
]);
assertDatabaseHas('wallet_transactions', [
'amount' => 100,
'wallet_id' => $wallet->id,
'type' => WalletTransactionType::DEBIT,
'reason' => 'test',
]);
});
test('cannot perform a debit transaction if balance is insufficient', function () {
$wallet = Wallet::factory()->forUser()->create();
expect(function () use ($wallet) {
$this->action->execute($wallet, WalletTransactionType::DEBIT, 100, 'test');
})->toThrow(InsufficientBalance::class);
assertDatabaseHas('wallets', [
'id' => $wallet->id,
'balance' => 0,
]);
assertDatabaseCount('wallet_transactions', 0);
});
test('force a debit transaction when balance is insufficient', function () {
$wallet = Wallet::factory()->forUser()->create();
$this->action->execute(wallet: $wallet, type: WalletTransactionType::DEBIT, amount: 100, reason: 'test', force: true);
expect($wallet->balance)->toBe(-100);
assertDatabaseHas('wallets', [
'id' => $wallet->id,
'balance' => -100,
]);
assertDatabaseHas('wallet_transactions', [
'amount' => 100,
'wallet_id' => $wallet->id,
'type' => WalletTransactionType::DEBIT,
'reason' => 'test',
]);
});

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
use App\Actions\PerformWalletTransfer;
use App\Enums\WalletTransactionType;
use App\Exceptions\InsufficientBalance;
use App\Models\User;
use App\Models\Wallet;
use function Pest\Laravel\assertDatabaseCount;
use function Pest\Laravel\assertDatabaseHas;
beforeEach(function () {
$this->action = app(PerformWalletTransfer::class);
});
test('perform a transfer', function () {
$sender = User::factory()->create();
$recipient = User::factory()->create();
$source = Wallet::factory()->for($sender)->richChillGuy()->create();
$target = Wallet::factory()->for($recipient)->create();
$transfer = $this->action->execute($sender, $recipient, 100, 'test');
expect($source->refresh()->balance)->toBe(999_900);
expect($target->refresh()->balance)->toBe(100);
assertDatabaseHas('wallet_transactions', [
'amount' => 100,
'wallet_id' => $target->id,
'type' => WalletTransactionType::CREDIT,
'transfer_id' => $transfer->id,
]);
assertDatabaseHas('wallet_transactions', [
'amount' => 100,
'wallet_id' => $sender->id,
'type' => WalletTransactionType::DEBIT,
'transfer_id' => $transfer->id,
]);
assertDatabaseHas('wallet_transfers', [
'amount' => 100,
'source_id' => $source->id,
'target_id' => $target->id,
]);
});
test('cannot perform a transfer with insufficient balance', function () {
$sender = User::factory()->create();
$recipient = User::factory()->create();
$source = Wallet::factory()->balance(90)->for($sender)->create();
$target = Wallet::factory()->for($recipient)->create();
expect(function () use ($sender, $recipient) {
$this->action->execute($sender, $recipient, 100, 'test');
})->toThrow(InsufficientBalance::class);
expect($source->refresh()->balance)->toBe(90);
expect($target->refresh()->balance)->toBe(0);
assertDatabaseCount('wallet_transfers', 0);
});