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);
});

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use App\Http\Controllers\Api\V1\AccountController;
use App\Models\User;
use App\Models\Wallet;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\getJson;
test('get account data', function () {
$user = User::factory()
->has(Wallet::factory()->richChillGuy())
->create(['name' => 'John Doe', 'email' => 'test@test.com']);
actingAs($user);
getJson(action(AccountController::class))
->assertOk()
->assertJson([
'data' => [
'id' => $user->id,
'name' => 'John Doe',
'email' => $user->email,
'balance' => 1_000_000,
],
]);
});
test('must be authenticated to get account data', function () {
getJson(action(AccountController::class))
->assertUnauthorized();
});

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
use App\Http\Controllers\Api\V1\LoginController;
use App\Models\User;
use function Pest\Laravel\postJson;
use function PHPUnit\Framework\assertCount;
test('login should return token', function () {
$user = User::factory()->create(['email' => 'test@test.com']);
postJson(action(LoginController::class), [
'email' => 'test@test.com',
'password' => 'password',
'device_name' => 'Feature test',
])
->assertCreated()
->assertJsonStructure(['data' => ['token']]);
assertCount(1, $user->refresh()->tokens);
});
test('bad login should return HTTP 400', function () {
postJson(action(LoginController::class), [
'email' => 'test@test.com',
'password' => 'password',
'device_name' => 'Feature test',
])
->assertStatus(400)
->assertJsonPath('message', 'Invalid credentials.')
->assertJsonPath('code', 'BAD_LOGIN');
});

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
use App\Http\Controllers\Api\V1\SendMoneyController;
use App\Models\User;
use App\Models\Wallet;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertDatabaseCount;
use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\postJson;
test('send money to a friend', function () {
$user = User::factory()
->has(Wallet::factory()->richChillGuy())
->create();
$recipient = User::factory()
->has(Wallet::factory())
->create();
actingAs($user);
postJson(action(SendMoneyController::class), [
'recipient_email' => $recipient->email,
'amount' => 100,
'reason' => 'Just a chill guy gift',
])
->assertNoContent(201);
expect($recipient->refresh()->wallet->balance)->toBe(100);
assertDatabaseHas('wallet_transfers', [
'amount' => 100,
'source_id' => $user->wallet->id,
'target_id' => $recipient->wallet->id,
]);
assertDatabaseCount('wallet_transactions', 3);
});
test('cannot send money to a friend with insufficient balance', function () {
$user = User::factory()
->has(Wallet::factory())
->create();
$recipient = User::factory()
->has(Wallet::factory())
->create();
actingAs($user);
postJson(action(SendMoneyController::class), [
'recipient_email' => $recipient->email,
'amount' => 100,
'reason' => 'Just a chill guy gift',
])
->assertBadRequest()
->assertJson([
'code' => 'INSUFFICIENT_BALANCE',
'message' => 'Insufficient balance in wallet.',
]);
expect($recipient->refresh()->wallet->balance)->toBe(0);
});

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
use App\Models\User;
use function Pest\Laravel\actingAs;
use function Pest\Laravel\assertAuthenticated;
use function Pest\Laravel\assertGuest;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
test('login screen can be rendered', function () {
$response = get('/login');
$response->assertStatus(200);
});
test('users can authenticate using the login screen', function () {
$user = User::factory()->create();
$response = post('/login', [
'email' => $user->email,
'password' => 'password',
]);
assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
});
test('users can not authenticate with invalid password', function () {
$user = User::factory()->create();
post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);
assertGuest();
});
test('users can logout', function () {
$user = User::factory()->create();
$response = actingAs($user)->post('/logout');
assertGuest();
$response->assertRedirect('/');
});

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
use function Pest\Laravel\assertAuthenticated;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
test('registration screen can be rendered', function () {
$response = get('/register');
$response->assertStatus(200);
});
test('new users can register', function () {
$response = post('/register', [
'name' => 'Test User',
'email' => 'test@example.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
assertAuthenticated();
$response->assertRedirect(route('dashboard', absolute: false));
});

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
use App\Models\User;
use App\Models\Wallet;
use function Pest\Laravel\actingAs;
test('dashboard page is displayed', function () {
$user = User::factory()->has(Wallet::factory()->richChillGuy())->create();
$wallet = Wallet::factory()->richChillGuy()->for($user)->create();
$response = actingAs($user)->get('/');
$response
->assertOk()
->assertSeeTextInOrder([
__('Balance'),
Number::currencyCents($wallet->balance),
'Transactions history',
'Just a rich chill guy',
]);
});
test('send money to a friend', function () {
$user = User::factory()->has(Wallet::factory()->richChillGuy())->create();
$recipient = User::factory()->has(Wallet::factory())->create();
$response = actingAs($user)->post('/send-money', [
'recipient_email' => $recipient->email,
'amount' => 10, // In euros, not cents
'reason' => 'Just a chill guy gift',
]);
$response
->assertRedirect('/')
->assertSessionHas('money-sent-status', 'success')
->assertSessionHas('money-sent-recipient-name', $recipient->name)
->assertSessionHas('money-sent-amount', 10_00);
actingAs($user)->get('/')
->assertSeeTextInOrder([
__('Balance'),
Number::currencyCents(1_000_000 - 10_00),
'Transactions history',
'Just a chill guy gift',
Number::currencyCents(-10_00),
'Just a rich chill guy',
Number::currencyCents(1_000_000),
]);
});
test('cannot send money to a friend with insufficient balance', function () {
$user = User::factory()->has(Wallet::factory())->create();
$recipient = User::factory()->has(Wallet::factory())->create();
$response = actingAs($user)->post('/send-money', [
'recipient_email' => $recipient->email,
'amount' => 10, // In euros, not cents
'reason' => 'Just a chill guy gift',
]);
$response
->assertRedirect('/')
->assertSessionHas('money-sent-status', 'insufficient-balance')
->assertSessionHas('money-sent-recipient-name', $recipient->name)
->assertSessionHas('money-sent-amount', 10_00);
});

18
tests/Pest.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/
pest()->extend(Tests\TestCase::class)
->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');

12
tests/TestCase.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
//
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
use App\Models\WalletTransaction;
test('that transfers transactions are correctly identified as transfers', function () {
$transaction = WalletTransaction::factory()
->credit()
->amount(100)
->make(['transfer_id' => 1]);
expect($transaction->is_transfer)->toBeTrue();
});
test('that classic transactions aren\'t identified as transfers', function () {
$transaction = WalletTransaction::factory()
->credit()
->amount(100)
->make();
expect($transaction->is_transfer)->toBeFalse();
});