Files
Verde-Web/tests/Feature/Api/V1/Store/ResidentSaleScanTest.php

96 lines
3.2 KiB
PHP

<?php
namespace Tests\Feature\Api\V1\Store;
use App\Models\DropOffPoint;
use App\Models\PartnerStore;
use App\Models\QrCode;
use App\Models\User;
use App\Services\Store\StoreOperations;
use Database\Seeders\RoleSeeder;
use Database\Seeders\SampleDropOffPointsSeeder;
use Database\Seeders\SamplePsgcSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;
class ResidentSaleScanTest extends TestCase
{
use RefreshDatabase;
public function test_end_to_end_resident_sale_and_scan(): void
{
// 1. Setup Environment
$this->seed([RoleSeeder::class, SamplePsgcSeeder::class, SampleDropOffPointsSeeder::class]);
$admin = User::factory()->create(['role' => User::ROLE_ADMIN, 'status' => 'active']);
$storeOwner = User::factory()->create(['role' => User::ROLE_STORE_PARTNER, 'status' => 'active']);
$resident = User::factory()->create(['role' => User::ROLE_RESIDENT, 'status' => 'active']);
$scannerUser = User::factory()->create(['role' => User::ROLE_SCANNER, 'status' => 'active']);
$dop = DropOffPoint::first();
// 2. Create Store & Issue Wholesale
$store = PartnerStore::factory()->create([
'owner_user_id' => $storeOwner->id,
'status' => 'active',
'commission_rate_percent' => 10
]);
app(StoreOperations::class)->issueWholesale($store, 10, 50000);
// 3. Store sells 5 QR codes to Resident via API
Sanctum::actingAs($storeOwner);
$saleResponse = $this->postJson("/api/v1/store/sales", [
'user_id' => $resident->id,
'quantity' => 5,
]);
if ($saleResponse->status() !== 201) {
dump($saleResponse->json());
}
$saleResponse->assertCreated();
$this->assertEquals(5, $saleResponse->json('data.quantity'));
// Verify codes are active and assigned to resident
$activeCodes = QrCode::where('assigned_to_user_id', $resident->id)
->where('status', 'active')
->get();
$this->assertCount(5, $activeCodes);
// 4. Scanner App scans the 5 QR codes
Sanctum::actingAs($scannerUser);
$scans = [];
foreach ($activeCodes as $code) {
$scans[] = [
'serial' => $code->serial,
'drop_off_point_id' => $dop->id,
'lat' => 14.6539,
'lng' => 121.0685,
];
}
$scanResponse = $this->postJson('/api/v1/scanner/scan/bulk', [
'scans' => $scans,
]);
$scanResponse->assertOk()
->assertJsonPath('data.accepted_count', 5)
->assertJsonPath('data.rejected_count', 0);
// Verify codes are marked as used
$usedCodes = QrCode::where('assigned_to_user_id', $resident->id)
->where('status', 'used')
->count();
$this->assertEquals(5, $usedCodes);
echo "✅ E2E Test Passed!\n";
echo "1. Issued wholesale to store.\n";
echo "2. Store sold 5 codes to resident (user_id: {$resident->id}).\n";
echo "3. Scanner app successfully scanned all 5 codes.\n";
}
}