File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/scratch/test_admin_finance_flow.php
Back
<?php /** * Comprehensive Automated Verification for Admin Finance Modules * 1. Payment Management (/admin/payments) * 2. Refund Requests (/admin/refunds) * 3. Financial Reports (/admin/finance) * 4. Settlements / Payout (/admin/finance/settlements) */ declare(strict_types=1); require_once __DIR__ . '/../database/Database.php'; require_once __DIR__ . '/../app/Models/Payment.php'; require_once __DIR__ . '/../app/Models/PaymentAttempt.php'; require_once __DIR__ . '/../app/Models/PaymentAccount.php'; require_once __DIR__ . '/../app/Models/Refund.php'; require_once __DIR__ . '/../app/Models/Settlement.php'; require_once __DIR__ . '/../app/Models/Order.php'; require_once __DIR__ . '/../app/Models/Store.php'; require_once __DIR__ . '/../app/Models/User.php'; require_once __DIR__ . '/../app/Models/AdminPermission.php'; require_once __DIR__ . '/../app/Services/AdminPermissionService.php'; require_once __DIR__ . '/../app/Services/PaymentService.php'; require_once __DIR__ . '/../app/Services/RefundService.php'; require_once __DIR__ . '/../app/Services/AdminOrderService.php'; $passed = 0; $failed = 0; function it(string $description, callable $test) { global $passed, $failed; try { $test(); echo " [PASS] {$description}\n"; $passed++; } catch (Throwable $e) { echo " [FAIL] {$description}: " . $e->getMessage() . " in " . $e->getFile() . ":" . $e->getLine() . "\n"; $failed++; } } echo "\n=== [1] PAYMENT MANAGEMENT TESTS ===\n"; it("Payment::getAdminPayments returns valid list and joins users & orders without schema error", function() { $model = new Payment(); $list = $model->getAdminPayments([], 10, 0); assert(is_array($list), "Expected array of payments"); if (!empty($list)) { $item = $list[0]; assert(isset($item['order_no']), "Missing order_no"); assert(isset($item['amount']), "Missing amount"); assert(isset($item['payment_method']), "Missing payment_method"); assert(isset($item['payment_status']), "Missing payment_status"); assert(isset($item['customer_name']), "Missing customer_name"); } }); it("Payment::getPaymentStats computes counts and sums correctly", function() { $model = new Payment(); $stats = $model->getPaymentStats(); assert(is_array($stats), "Expected array of stats"); assert(isset($stats['total']), "Missing total"); assert(isset($stats['awaiting']), "Missing awaiting"); assert(isset($stats['confirmed']), "Missing confirmed"); }); it("Payment::findByOrderNo finds existing payment with account details", function() { $model = new Payment(); $list = $model->getAdminPayments([], 1, 0); if (!empty($list)) { $orderNo = $list[0]['order_no']; $pmt = $model->findByOrderNo($orderNo); assert($pmt !== null, "Expected payment object for order {$orderNo}"); assert($pmt['order_no'] === $orderNo, "Order no mismatch"); } }); it("PaymentAccount::getAllAccounts returns configured payment receiving accounts", function() { $accModel = new PaymentAccount(); $accounts = $accModel->getAllAccounts(); assert(is_array($accounts), "Expected accounts list"); }); echo "\n=== [2] REFUND REQUESTS TESTS ===\n"; it("Refund::getAdminRefunds runs cleanly without unknown column errors", function() { $refModel = new Refund(); $refunds = $refModel->getAdminRefunds([], 10, 0); assert(is_array($refunds), "Expected array of refunds"); if (!empty($refunds)) { $r = $refunds[0]; assert(isset($r['id']), "Missing refund id"); assert(isset($r['order_id']), "Missing order_id"); assert(isset($r['amount']), "Missing amount"); assert(isset($r['status']), "Missing status"); assert(isset($r['order_no']), "Missing order_no"); assert(isset($r['requested_by_name']), "Missing requested_by_name"); } }); it("Refund::getRefundStats computes refund metrics", function() { $refModel = new Refund(); $stats = $refModel->getRefundStats(); assert(is_array($stats), "Expected stats array"); assert(isset($stats['total']), "Missing total count"); assert(isset($stats['pending']), "Missing pending count"); }); it("Refund::findById retrieves full refund details with order, customer and store info", function() { $refModel = new Refund(); $list = $refModel->getAdminRefunds([], 1, 0); if (!empty($list)) { $refId = (int)$list[0]['id']; $refund = $refModel->findById($refId); assert($refund !== null, "Refund #{$refId} not found"); assert(isset($refund['order_no']), "Missing order_no in findById"); assert(isset($refund['requested_by_name']), "Missing requested_by_name in findById"); } }); echo "\n=== [3] FINANCIAL REPORTS TESTS ===\n"; it("Order::getPlatformOrderStats calculates platform GMV & commissions", function() { $orderModel = new Order(); $stats = $orderModel->getPlatformOrderStats(); assert(is_array($stats), "Expected array stats"); assert(isset($stats['total_orders']), "Missing total_orders"); assert(isset($stats['total_gmv']), "Missing total_gmv"); assert(isset($stats['total_commission']), "Missing total_commission"); }); it("Financial monthly breakdown query executes properly", function() { $db = Database::getInstance(); $sql = "SELECT DATE_FORMAT(o.created_at, '%Y-%m') as month, COUNT(*) as order_count, SUM(CASE WHEN o.order_status = 'completed' THEN o.total_amount ELSE 0 END) as gmv, SUM(CASE WHEN o.order_status = 'completed' THEN o.commission_amount ELSE 0 END) as commission, SUM(CASE WHEN o.order_status = 'completed' THEN o.net_revenue ELSE 0 END) as net_revenue FROM `orders` o WHERE o.deleted_at IS NULL AND o.created_at >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH) GROUP BY DATE_FORMAT(o.created_at, '%Y-%m') ORDER BY month DESC"; $stmt = $db->query($sql); $monthly = $stmt->fetchAll(); assert(is_array($monthly), "Monthly query returned non-array"); }); echo "\n=== [4] SETTLEMENT / PAYOUT TESTS ===\n"; it("Settlement::getAdminSettlements returns settlements with store and owner info", function() { $setMod = new Settlement(); $list = $setMod->getAdminSettlements([], 10, 0); assert(is_array($list), "Expected settlements array"); if (!empty($list)) { $se = $list[0]; assert(isset($se['id']), "Missing settlement id"); assert(isset($se['store_name']), "Missing store_name"); assert(isset($se['owner_username']), "Missing owner_username"); assert(isset($se['total_sales']), "Missing total_sales"); assert(isset($se['commission_deduction']), "Missing commission_deduction"); assert(isset($se['seller_receives']), "Missing seller_receives"); assert(isset($se['payment_status']), "Missing payment_status"); } }); it("Settlement::getSettlementStats computes payout aggregates", function() { $setMod = new Settlement(); $stats = $setMod->getSettlementStats(); assert(is_array($stats), "Expected stats array"); assert(isset($stats['total']), "Missing total settlements count"); assert(isset($stats['pending_count']), "Missing pending count"); }); it("Settlement::getUnsettledStoreBalances calculates completed order balances per store", function() { $setMod = new Settlement(); $unsettled = $setMod->getUnsettledStoreBalances(); assert(is_array($unsettled), "Expected unsettled stores array"); if (!empty($unsettled)) { $u = $unsettled[0]; assert(isset($u['store_name']), "Missing store_name in unsettled"); assert(isset($u['gross_sales']), "Missing gross_sales in unsettled"); assert(isset($u['net_receivable']), "Missing net_receivable in unsettled"); } }); echo "\n=== [5] ADMIN PERMISSION & CONSISTENCY TESTS ===\n"; it("AdminPermissionService handles can() and has() consistently for superadmin and finance roles", function() { $permService = new AdminPermissionService(); $userModel = new User(); // Find superadmin $admin = $userModel->findByEmail('admin@cargoo.com'); assert($admin !== null, "Superadmin user not found"); $adminId = (int)$admin['id']; assert($permService->can($adminId, 'finance.view') === true, "Superadmin should have finance.view"); assert($permService->has($adminId, 'finance.view') === true, "has() alias should return true"); assert($permService->has($adminId, 'payment.verify') === true, "has() alias should return true for payment.verify"); assert($permService->has($adminId, 'payment.refund') === true, "has() alias should return true for payment.refund"); assert($permService->has($adminId, 'finance.settle') === true, "has() alias should return true for finance.settle"); }); echo "\n============================================\n"; echo "TEST RESULTS: {$passed} Passed, {$failed} Failed\n"; echo "============================================\n"; if ($failed > 0) { exit(1); }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.35 |
proxy
|
phpinfo
|
Settings