Files
portal-api/tests/Feature/Api/Admin/Transaction/AdminManualDepositTest.php

87 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin\Transaction;
use App\Models\Customer;
use App\Models\Transaction;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use PhpOffice\PhpSpreadsheet\IOFactory;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class AdminManualDepositTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->assignRole('Full Administrator');
}
public function test_admin_deposit_credits_customer_and_stores_proof(): void
{
Storage::fake('local');
$customer = Customer::factory()->create(['total' => 100_000, 'credit' => 0, 'total_pending' => 0]);
$this->actingAs($this->admin, 'admin')->post('/api/v1/administrator/transactions', [
'customer_id' => $customer->id,
'amount' => 500_000,
'note' => 'CK Vietcombank',
'proof' => UploadedFile::fake()->image('proof.png'),
])->assertOk();
$customer->refresh();
$this->assertSame(600_000, (int) $customer->total);
$this->assertSame(5_000, (int) $customer->credit);
$this->assertSame(0, (int) $customer->total_pending);
$transaction = Transaction::where('customer_id', $customer->id)->sole();
$this->assertSame('manual', $transaction->type);
$this->assertSame(1, (int) $transaction->status);
$this->assertSame(500_000, (int) $transaction->received_amount);
$this->assertSame($this->admin->id, $transaction->user_id);
$this->assertStringStartsWith('MAN-', $transaction->public_code);
Storage::disk('local')->assertExists($transaction->proof_image);
$this->actingAs($this->admin, 'admin')->getJson('/api/v1/administrator/transactions')
->assertJsonPath('data.items.0.created_by', $this->admin->name ?: $this->admin->username);
}
public function test_export_writes_filtered_rows_into_template(): void
{
$customer = Customer::factory()->create();
$other = Customer::factory()->create();
foreach ([$customer, $customer, $other] as $owner) {
$this->actingAs($this->admin, 'admin')
->post('/api/v1/administrator/transactions', ['customer_id' => $owner->id, 'amount' => 10_000])
->assertOk();
}
$all = $this->exportedSheet('/api/v1/administrator/transactions/export');
$this->assertSame('DANH SÁCH GIAO DỊCH NẠP TIỀN', $all->getCell('A6')->getValue());
$this->assertSame('Mã giao dịch', $all->getCell('A7')->getValue());
$this->assertSame(10, $all->getHighestDataRow());
$this->assertSame(9, $this->exportedSheet("/api/v1/administrator/transactions/export?customer_id={$customer->id}")->getHighestDataRow());
$this->assertSame(7, $this->exportedSheet('/api/v1/administrator/transactions/export?status[]=0')->getHighestDataRow());
}
private function exportedSheet(string $url): \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
{
$response = $this->actingAs($this->admin, 'admin')->get($url)->assertOk();
return IOFactory::load($response->baseResponse->getFile()->getPathname())->getActiveSheet();
}
}