Files
portal-api/tests/Feature/Payment/AdminTransactionApproveTest.php
T
2026-09-23 15:17:18 +07:00

104 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Payment;
use App\Enums\TransactionStatus;
use App\Http\Controllers\Api\Admin\Transaction\AdminTransactionController;
use App\Models\Customer;
use App\Models\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
class AdminTransactionApproveTest extends TestCase
{
use RefreshDatabase;
public function test_approving_the_same_manual_deposit_twice_credits_the_customer_only_once(): void
{
Mail::fake();
$customer = Customer::factory()->create(['total' => 0, 'credit' => 0, 'total_pending' => 100_000]);
$transaction = Transaction::create([
'uuid' => (string) \Illuminate\Support\Str::uuid(),
'public_code' => 'DEP-TEST-0001',
'trans' => 'DEP-TEST-0001',
'amount' => 100_000,
'customer_id' => $customer->id,
'type' => 'manual',
'status' => TransactionStatus::PENDING->value,
]);
$controller = app(AdminTransactionController::class);
$first = $controller->approve(new Request(), $transaction->uuid);
$second = $controller->approve(new Request(), $transaction->uuid);
$this->assertSame(200, $first->getStatusCode());
// Second call finds the transaction no longer PENDING → treated as not-approvable.
$this->assertSame(404, $second->getStatusCode());
$this->assertSame(100_000, (int) $customer->fresh()->total);
}
/**
* Khách khai 5tr nhưng sao kê chỉ về 4tr: số dư phải cộng theo số THỰC NHẬN, phần chênh
* được ghi lại để kế toán đối chiếu. Trước đây luôn cộng theo số khách khai.
*/
public function test_approving_credits_the_received_amount_not_the_declared_one(): void
{
Mail::fake();
$customer = Customer::factory()->create(['total' => 0, 'credit' => 0, 'total_pending' => 5_000_000]);
$transaction = Transaction::create([
'uuid' => (string) \Illuminate\Support\Str::uuid(),
'public_code' => 'DEP-TEST-0002',
'trans' => 'DEP-TEST-0002',
'amount' => 5_000_000,
'customer_id' => $customer->id,
'type' => 'manual',
'status' => TransactionStatus::PENDING->value,
]);
$request = Request::create('/', 'POST', ['received_amount' => 4_000_000]);
$response = app(AdminTransactionController::class)->approve($request, $transaction->uuid);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame(4_000_000, (int) $customer->fresh()->total);
$this->assertSame(40_000, (int) $customer->fresh()->credit);
$this->assertSame(0, (int) $customer->fresh()->total_pending);
$transaction->refresh();
$this->assertSame(5_000_000, (int) $transaction->amount);
$this->assertSame(4_000_000, (int) $transaction->received_amount);
$this->assertStringContainsString('Lệch ' . number_format(1_000_000), (string) $transaction->note);
}
/** Bỏ trống received_amount = tin số khách khai, giữ nguyên hành vi cũ. */
public function test_approving_without_received_amount_credits_the_declared_amount(): void
{
Mail::fake();
$customer = Customer::factory()->create(['total' => 0, 'credit' => 0, 'total_pending' => 300_000]);
$transaction = Transaction::create([
'uuid' => (string) \Illuminate\Support\Str::uuid(),
'public_code' => 'DEP-TEST-0003',
'trans' => 'DEP-TEST-0003',
'amount' => 300_000,
'customer_id' => $customer->id,
'type' => 'manual',
'status' => TransactionStatus::PENDING->value,
]);
app(AdminTransactionController::class)->approve(new Request(), $transaction->uuid);
$this->assertSame(300_000, (int) $customer->fresh()->total);
$this->assertSame(300_000, (int) $transaction->fresh()->received_amount);
$this->assertStringNotContainsString('Lệch', (string) $transaction->fresh()->note);
}
}