File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/scratch/test_multi_variation_groups.php
Back
<?php /** * Test Multi-Group Product Variations Creation and Management */ require_once __DIR__ . '/../database/Database.php'; require_once __DIR__ . '/../app/Models/User.php'; require_once __DIR__ . '/../app/Models/Store.php'; require_once __DIR__ . '/../app/Models/Product.php'; require_once __DIR__ . '/../app/Services/SellerProductService.php'; $db = Database::getInstance(); $userModel = new User(); $storeModel = new Store(); $productModel = new Product(); $sellerService = new SellerProductService(); echo "=== TEST 1: Retrieve or Create Seller & Store ===\n"; $seller = $db->query("SELECT u.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); if (!$seller) { echo "Creating demo seller and store...\n"; $userId = $userModel->insert([ 'username' => 'multivar_seller_' . time(), 'email' => 'multivar_' . time() . '@seller.test', 'phone' => '09' . rand(10000000, 99999999), 'password' => password_hash('Pass123!', PASSWORD_DEFAULT), 'first_name' => 'MultiVar', 'last_name' => 'Seller', 'status' => 'active' ]); $userModel->assignRole($userId, 'seller'); $storeId = $storeModel->insert([ 'seller_id' => $userId, 'store_name' => 'MultiVar Fashion Store', 'store_slug' => 'multivar-store-' . time(), 'status' => 'active', 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s') ]); } else { $userId = (int)$seller['id']; $storeId = (int)$seller['store_id']; } echo " [PASS] Using Seller #{$userId}, Store #{$storeId}\n"; $cat = $db->query("SELECT id FROM categories WHERE status = 'active' LIMIT 1")->fetch(PDO::FETCH_ASSOC); $catId = (int)($cat['id'] ?? 1); echo "\n=== TEST 2: Create Product with 2 Variation Groups (Color & Size) ===\n"; $postData = [ 'name' => 'เสื้อฮู้ด Unisex Premium Multi-Group ' . time(), 'category_id' => $catId, 'price' => 590.00, 'original_price' => 790.00, 'stock' => 100, 'province' => 'กรุงเทพมหานคร', 'badges' => 'สินค้าใหม่', 'status' => 'active', 'description' => 'เสื้อฮู้ดคุณภาพสูง มีหลายสีและหลายขนาด', 'variations' => [ [ 'name' => 'สี', 'options' => [ ['name' => 'สีดำ', 'price_modifier' => 0.00, 'stock' => 50], ['name' => 'สีขาว', 'price_modifier' => 20.00, 'stock' => 50] ] ], [ 'name' => 'ขนาด', 'options' => [ ['name' => 'M', 'price_modifier' => 0.00, 'stock' => 40], ['name' => 'L', 'price_modifier' => 30.00, 'stock' => 30], ['name' => 'XL', 'price_modifier' => 50.00, 'stock' => 30] ] ] ], // Explicit SKU Matrix combinations (2 colors x 3 sizes = 6 SKUs) 'skus' => [ ['variation_summary' => 'สีดำ, M', 'price' => 590.00, 'stock' => 20], ['variation_summary' => 'สีดำ, L', 'price' => 620.00, 'stock' => 15], ['variation_summary' => 'สีดำ, XL', 'price' => 640.00, 'stock' => 15], ['variation_summary' => 'สีขาว, M', 'price' => 610.00, 'stock' => 20], ['variation_summary' => 'สีขาว, L', 'price' => 640.00, 'stock' => 15], ['variation_summary' => 'สีขาว, XL', 'price' => 660.00, 'stock' => 15] ] ]; $res = $sellerService->createProduct($userId, $postData, []); assert($res['success'] === true, "Create multi-variation product must succeed: " . ($res['message'] ?? '')); $productId = (int)$res['product_id']; echo " [PASS] Product created with ID #{$productId}\n"; echo "\n=== TEST 3: Verify Variations, Options and SKUs in Database ===\n"; $savedVariations = $productModel->getVariations($productId); assert(count($savedVariations) === 2, "Must have exactly 2 variation groups"); assert($savedVariations[0]['name'] === 'สี', "Group 1 name must be 'สี'"); assert(count($savedVariations[0]['options']) === 2, "Group 1 must have 2 options"); assert($savedVariations[1]['name'] === 'ขนาด', "Group 2 name must be 'ขนาด'"); assert(count($savedVariations[1]['options']) === 3, "Group 2 must have 3 options"); echo " [PASS] Variation groups verified: '{$savedVariations[0]['name']}' (2 options), '{$savedVariations[1]['name']}' (3 options)\n"; $savedSkus = $productModel->getSkus($productId); assert(count($savedSkus) === 6, "Must have exactly 6 SKU combinations (2 x 3 = 6)"); $skuSummaries = array_column($savedSkus, 'variation_summary'); assert(in_array('สีดำ, M', $skuSummaries), "Must contain 'สีดำ, M'"); assert(in_array('สีขาว, XL', $skuSummaries), "Must contain 'สีขาว, XL'"); echo " [PASS] 6 SKU combinations stored properly in `product_skus`\n"; echo "\n=== TEST 4: Automatic Cartesian Combinations when SKUs omitted ===\n"; $postData2 = [ 'name' => 'กางเกงคาร์โก้ Auto-Cartesian ' . time(), 'category_id' => $catId, 'price' => 450.00, 'stock' => 60, 'province' => 'กรุงเทพมหานคร', 'status' => 'active', 'variations' => [ [ 'name' => 'สี', 'options' => [ ['name' => 'กากี', 'price_modifier' => 0.00], ['name' => 'เขียวขี้ม้า', 'price_modifier' => 10.00] ] ], [ 'name' => 'ไซส์', 'options' => [ ['name' => '28', 'price_modifier' => 0.00], ['name' => '30', 'price_modifier' => 0.00] ] ] ] ]; $res2 = $sellerService->createProduct($userId, $postData2, []); assert($res2['success'] === true, "Creation without explicit skus matrix must auto-generate Cartesian combinations"); $productId2 = (int)$res2['product_id']; $autoSkus = $productModel->getSkus($productId2); assert(count($autoSkus) === 4, "Auto Cartesian product must produce 4 SKUs (2 x 2)"); echo " [PASS] Auto-generated 4 SKUs via Cartesian product generator\n"; echo "\n=== TEST 5: Update Product with 3 Variation Groups ===\n"; $updateData = [ 'name' => 'เสื้อฮู้ด Unisex Multi-Group Updated ' . time(), 'category_id' => $catId, 'price' => 650.00, 'stock' => 120, 'description' => 'Updated description', 'variations' => [ [ 'name' => 'สี', 'options' => [ ['name' => 'สีดำ', 'price_modifier' => 0.00], ['name' => 'สีกรม', 'price_modifier' => 10.00] ] ], [ 'name' => 'ขนาด', 'options' => [ ['name' => 'L', 'price_modifier' => 0.00], ['name' => 'XL', 'price_modifier' => 20.00] ] ], [ 'name' => 'เนื้อผ้า', 'options' => [ ['name' => 'คอตตอน', 'price_modifier' => 0.00], ['name' => 'ฟลีซกันหนาว', 'price_modifier' => 50.00] ] ] ] ]; $upRes = $sellerService->updateProduct($productId, $userId, $updateData, []); assert($upRes['success'] === true, "Update with 3 variation groups must succeed"); $updatedVars = $productModel->getVariations($productId); assert(count($updatedVars) === 3, "Must now have 3 variation groups"); $updatedSkus = $productModel->getSkus($productId); assert(count($updatedSkus) === 8, "2 x 2 x 2 must produce 8 SKU combinations"); echo " [PASS] Product successfully updated with 3 groups ('{$updatedVars[0]['name']}', '{$updatedVars[1]['name']}', '{$updatedVars[2]['name']}') and 8 SKUs\n"; // Cleanup test products $db->exec("DELETE FROM product_skus WHERE product_id IN ({$productId}, {$productId2})"); $db->exec("DELETE FROM product_variation_options WHERE product_id IN ({$productId}, {$productId2})"); $db->exec("DELETE FROM product_variations WHERE product_id IN ({$productId}, {$productId2})"); $db->exec("DELETE FROM products WHERE id IN ({$productId}, {$productId2})"); echo "\n=== CLEANUP: Removed test products ===\n"; echo "ALL MULTI-VARIATION GROUPS TESTS PASSED SUCCESSFULLY! (5/5)\n";
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings