Files
radicall/tests/Feature/Http/Client/InvoiceControllerTest.php

70 lines
2.5 KiB
PHP

<?php
namespace Tests\Feature\Http\Client;
use App\Enums\InvoiceStatusEnum;
use App\Models\Invoice;
use App\Models\User;
use App\Models\Wallet;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Queue;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\TestCase;
class InvoiceControllerTest extends TestCase
{
use RefreshDatabase;
public function test_routes_require_authentication(): void
{
$this->get(route('dashboard.invoices.index'))->assertRedirect(route('login'));
$this->post(route('dashboard.invoices.payWallet', 1))->assertRedirect(route('login'));
}
public function test_index_lists_only_own_invoices(): void
{
$user = User::factory()->create();
Invoice::factory()->count(3)->create(['user_id' => $user->id]);
Invoice::factory()->create();
$this->actingAs($user)->get(route('dashboard.invoices.index'))
->assertOk()
->assertInertia(fn (Assert $page) => $page->component('Dashboard/Invoices/Index')->has('invoices.data', 3));
}
public function test_pay_with_wallet_pays_own_pending_invoice(): void
{
Queue::fake();
$wallet = Wallet::factory()->create(['balance' => 1000000]);
$invoice = Invoice::factory()->create(['user_id' => $wallet->user_id, 'amount' => 400000]);
$this->actingAs($wallet->user)->postJson(route('dashboard.invoices.payWallet', $invoice))
->assertOk()
->assertJsonPath('invoice.id', $invoice->id);
$this->assertSame(InvoiceStatusEnum::Paid, $invoice->fresh()->status);
$this->assertSame(600000, $wallet->fresh()->balance);
}
public function test_pay_with_wallet_forbids_another_users_invoice(): void
{
$invoice = Invoice::factory()->create();
$this->actingAs(User::factory()->create())->postJson(route('dashboard.invoices.payWallet', $invoice))->assertForbidden();
$this->assertSame(InvoiceStatusEnum::Pending, $invoice->fresh()->status);
}
public function test_pay_with_wallet_returns_validation_error_on_insufficient_balance(): void
{
$wallet = Wallet::factory()->create(['balance' => 1000]);
$invoice = Invoice::factory()->create(['user_id' => $wallet->user_id, 'amount' => 400000]);
$this->actingAs($wallet->user)->postJson(route('dashboard.invoices.payWallet', $invoice))
->assertUnprocessable()
->assertJsonValidationErrors('amount');
$this->assertSame(InvoiceStatusEnum::Pending, $invoice->fresh()->status);
}
}