File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/scratch/test_variation_price_switching.php
Back
<?php /** * Test Dynamic Variation Price Switching Across Different Product Configurations */ require_once __DIR__ . '/../database/Database.php'; require_once __DIR__ . '/../app/Models/Product.php'; require_once __DIR__ . '/../app/Services/ProductService.php'; require_once __DIR__ . '/../app/Services/CartService.php'; $db = Database::getInstance(); $productModel = new Product(); $productService = new ProductService(); $cartService = new CartService(); echo "=== TEST 1: Inspect Product #30 (iPhone 13 with custom SKU matrix) ===\n"; $p30 = $productService->getProductDetail(30); assert($p30 !== null, "Product 30 must exist"); assert(count($p30['variations']) === 2, "Product 30 must have 2 variation groups"); assert(count($p30['skus']) === 18, "Product 30 must have 18 SKUs"); echo " [PASS] Product 30 has 2 groups ('{$p30['variations'][0]['name']}', '{$p30['variations'][1]['name']}') and 18 SKUs\n"; echo "\n=== TEST 2: Inspect Product #35 (iPhone 15 with storage modifiers & matrix) ===\n"; $p35 = $productService->getProductDetail(35); assert($p35 !== null, "Product 35 must exist"); assert(count($p35['variations']) === 2, "Product 35 must have 2 variation groups"); assert(count($p35['skus']) === 15, "Product 35 must have 15 SKUs"); // Verify SKU price increments: 128GB=15900, 256GB=20900, 512GB=24900 $sku256 = array_filter($p35['skus'], fn($s) => strpos($s['variation_summary'], '256 GB') !== false); $first256 = reset($sku256); assert((float)$first256['price'] === 20900.00, "256 GB SKU must be priced at 20900.00"); $sku512 = array_filter($p35['skus'], fn($s) => strpos($s['variation_summary'], '512 GB') !== false); $first512 = reset($sku512); assert((float)$first512['price'] === 24900.00, "512 GB SKU must be priced at 24900.00"); echo " [PASS] Product 35 SKUs verified (128GB: 15,900฿, 256GB: 20,900฿, 512GB: 24,900฿)\n"; echo "\n=== TEST 3: Create Single-Variation Group Product with Modifiers Only (No explicit SKU matrix) ===\n"; $seller = $db->query("SELECT u.id as user_id, s.id as store_id FROM users u JOIN stores s ON s.user_id = u.id WHERE s.status = 'active' LIMIT 1")->fetch(PDO::FETCH_ASSOC); $sellerId = (int)$seller['user_id']; require_once __DIR__ . '/../app/Services/SellerProductService.php'; $sellerService = new SellerProductService(); $cat = $db->query("SELECT id FROM categories WHERE status = 'active' LIMIT 1")->fetch(PDO::FETCH_ASSOC); $catId = (int)($cat['id'] ?? 1); $singleVarPost = [ 'name' => 'หูฟังไร้สาย Wireless Mod Test ' . time(), 'category_id' => $catId, 'price' => 1200.00, 'stock' => 30, 'province' => 'กรุงเทพมหานคร', 'status' => 'active', 'variations' => [ [ 'name' => 'รุ่น (Model)', 'options' => [ ['name' => 'รุ่นธรรมดา (Standard)', 'price_modifier' => 0.00, 'stock' => 10], ['name' => 'รุ่นโปรพร้อมเคส (Pro + Case)', 'price_modifier' => 350.00, 'stock' => 10], ['name' => 'รุ่นอัลตร้า (Ultra ANC)', 'price_modifier' => 600.00, 'stock' => 10] ] ] ] ]; $resSingle = $sellerService->createProduct($sellerId, $singleVarPost, []); assert($resSingle['success'] === true, "Creation must succeed"); $singlePid = (int)$resSingle['product_id']; $singleProd = $productService->getProductDetail($singlePid); assert(count($singleProd['skus']) === 3, "Must auto-generate 3 SKUs from 3 options"); $skuStandard = array_values(array_filter($singleProd['skus'], fn($s) => strpos($s['variation_summary'], 'Standard') !== false))[0]; $skuPro = array_values(array_filter($singleProd['skus'], fn($s) => strpos($s['variation_summary'], 'Pro') !== false))[0]; $skuUltra = array_values(array_filter($singleProd['skus'], fn($s) => strpos($s['variation_summary'], 'Ultra') !== false))[0]; assert((float)$skuStandard['price'] === 1200.00, "Standard price must be 1200.00"); assert((float)$skuPro['price'] === 1550.00, "Pro price must be 1550.00 (1200 + 350)"); assert((float)$skuUltra['price'] === 1800.00, "Ultra price must be 1800.00 (1200 + 600)"); echo " [PASS] Auto-generated SKU prices correctly reflect price modifiers (1200฿, 1550฿, 1800฿)\n"; // Cleanup test product $db->exec("DELETE FROM product_skus WHERE product_id = {$singlePid}"); $db->exec("DELETE FROM product_variation_options WHERE product_id = {$singlePid}"); $db->exec("DELETE FROM product_variations WHERE product_id = {$singlePid}"); $db->exec("DELETE FROM products WHERE id = {$singlePid}"); echo "\nALL VARIATION PRICE SWITCHING TESTS PASSED SUCCESSFULLY! (3/3)\n";
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.23 |
proxy
|
phpinfo
|
Settings