# 🧪 CACHE TESTING GUIDE

## ✅ Status: READY FOR TESTING

**Tanggal:** 2026-03-25  
**Setup:** Completed  
**Server:** Running  
**Cache Helper:** Loaded  

---

## 🎯 TUJUAN TESTING

Memastikan bahwa:
1. Helper functions dapat digunakan
2. Cache bekerja dengan benar
3. Cache clear setelah update settings
4. Performance improvement terlihat

---

## 📋 CHECKLIST TESTING

### ✅ Setup Completed
- [x] Composer dump-autoload
- [x] php artisan optimize:clear
- [x] php artisan optimize
- [x] Server running (php artisan serve)

---

## 🧪 TEST 1: Helper Functions Availability

### Test melalui PHP Tinker:
```bash
php artisan tinker
```

### Test Commands:
```php
// Test 1: Check if functions are available
>>> function_exists('getMinWithdrawal')
true

>>> function_exists('getTokenPrice')
true

>>> function_exists('getWithdrawalFee')
true

>>> function_exists('getCommissionPercentage')
true

// Test 2: Get values with cache
>>> getMinWithdrawal()
100000

>>> getTokenPrice()
1000

>>> getWithdrawalFee()
2

>>> getCommissionPercentage()
10

// Test 3: Check cache keys
>>> Cache::get('settings:min_withdrawal')
"100000"

>>> Cache::get('settings:token_price')
"1000"

>>> Cache::get('settings:withdrawal_fee')
"2"

>>> Cache::get('settings:commission_percentage')
"10"

// Exit tinker
>>> exit
```

**Expected Result:** ✅ All functions return correct values

---

## 🧪 TEST 2: Cache Hit Performance

### Test Commands:
```bash
php artisan tinker
```

```php
// Test cache performance (first call - cache miss)
>>> $start = microtime(true);
>>> $minWithdraw = getMinWithdrawal();
>>> $time1 = (microtime(true) - $start) * 1000;
>>> echo "First call: {$time1}ms";

// Test cache performance (second call - cache hit)
>>> $start = microtime(true);
>>> $minWithdraw = getMinWithdrawal();
>>> $time2 = (microtime(true) - $start) * 1000;
>>> echo "Second call: {$time2}ms";

// Compare
>>> echo "Performance gain: " . round(($time1 - $time2) / $time1 * 100, 2) . "%";

// Exit
>>> exit
```

**Expected Result:** ✅ Second call should be significantly faster (50-90% faster)

---

## 🧪 TEST 3: Application Pages (Manual Testing)

### Test 1: Sales Dashboard
1. Login sebagai Sales
2. Buka halaman: `/sales/dashboard`
3. Buka browser DevTools → Network tab
4. Check jumlah queries di debugbar (jika ada)
5. Refresh halaman
6. Bandingkan jumlah queries

**Expected:** ✅ Second load should have fewer queries

### Test 2: Sales Withdraw Page
1. Login sebagai Sales
2. Buka halaman: `/sales/withdraw`
3. Check Network tab
4. Refresh halaman
5. Bandingkan response time

**Expected:** ✅ Second load should be faster

### Test 3: Admin Token Page
1. Login sebagai Admin
2. Buka halaman: `/admin/token`
3. Check Network tab
4. Refresh halaman
5. Bandingkan response time

**Expected:** ✅ Second load should be faster

---

## 🧪 TEST 4: Cache Clear After Update

### Test 1: Update Admin Contact Settings
1. Login sebagai Sales
2. Buka halaman: `/sales/settings`
3. Update admin_phone (contoh: 08123456789)
4. Submit form
5. Check response
6. Refresh halaman
7. Verify phone number diupdate

**Expected:** ✅ Settings updated, cache cleared

### Test 2: Verify Cache Clear
```bash
php artisan tinker
```

```php
// Update setting manually
>>> Setting::where('key_name', 'admin_phone')->update(['value' => '08987654321']);

// Check cache (should still have old value)
>>> Cache::get('settings:admin_phone')
null

// Clear cache manually
>>> clearSettingCache('admin_phone')

// Verify cache cleared
>>> Cache::get('settings:admin_phone')
null

// Get new value (will cache it)
>>> getSetting('admin_phone', 'default')
"08987654321"

// Check cache again (should have new value)
>>> Cache::get('settings:admin_phone')
"08987654321"

// Exit
>>> exit
```

**Expected:** ✅ Cache cleared and updated correctly

---

## 🧪 TEST 5: Database Query Count

### Test dengan DebugBar (jika terinstall):

1. Install Laravel DebugBar (jika belum):
```bash
composer require barryvdh/laravel-debugbar --dev
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
```

2. Refresh halaman aplikasi
3. Lihat jumlah queries di DebugBar

### Test Tanpa DebugBar:

```bash
php artisan tinker
```

```php
// Enable query log
>>> DB::enableQueryLog();

// First call (cache miss)
>>> getMinWithdrawal();
>>> count(DB::getQueryLog());
1  // Should be 1 query

// Clear log
>>> DB::flushQueryLog();

// Second call (cache hit)
>>> getMinWithdrawal();
>>> count(DB::getQueryLog());
0  // Should be 0 queries (cache hit)

// Exit
>>> exit
```

**Expected:** ✅ First call: 1 query, Second call: 0 queries

---

## 🧪 TEST 6: Cache Expiration (TTL)

### Test:
```bash
php artisan tinker
```

```php
// Get setting (will cache it)
>>> getMinWithdrawal();
>>> Cache::get('settings:min_withdrawal');
"100000"

// Wait 1 hour (or manually clear cache)
>>> Cache::forget('settings:min_withdrawal');
true

// Try to get again (should fetch from DB)
>>> DB::enableQueryLog();
>>> getMinWithdrawal();
>>> count(DB::getQueryLog());
1  // Should be 1 query again

// Exit
>>> exit
```

**Expected:** ✅ Cache expires correctly after TTL

---

## 🧪 TEST 7: Multiple Settings

### Test:
```bash
php artisan tinker
```

```php
// Get multiple settings
>>> $tokenPrice = getTokenPrice();
>>> $minWithdraw = getMinWithdrawal();
>>> $withdrawFee = getWithdrawalFee();
>>> $commission = getCommissionPercentage();

// Check all caches
>>> Cache::get('settings:token_price');
"1000"

>>> Cache::get('settings:min_withdrawal');
"100000"

>>> Cache::get('settings:withdrawal_fee');
"2"

>>> Cache::get('settings:commission_percentage');
"10"

// Exit
>>> exit
```

**Expected:** ✅ All settings cached correctly

---

## 🧪 TEST 8: Error Handling

### Test with Non-Existent Setting:
```bash
php artisan tinker
```

```php
// Get non-existent setting with default
>>> getSetting('non_existent_key', 'default_value');
"default_value"

// Check cache
>>> Cache::get('settings:non_existent_key');
"default_value"

// Clear cache
>>> clearSettingCache('non_existent_key');
true

// Exit
>>> exit
```

**Expected:** ✅ Returns default value, caches it correctly

---

## 📊 PERFORMANCE COMPARISON

### Before Cache (Estimated):
- **Sales Withdraw Page:** ~50-100ms, 2 queries
- **Sales Dashboard:** ~30-60ms, 1 query
- **Admin Token Page:** ~30-60ms, 1 query

### After Cache (Expected):
- **Sales Withdraw Page:** ~10-30ms, 0-1 queries (cache hit)
- **Sales Dashboard:** ~5-15ms, 0 queries (cache hit)
- **Admin Token Page:** ~5-15ms, 0 queries (cache hit)

### Performance Gain:
- **Response Time:** 50-70% faster
- **Database Load:** 60-80% reduction

---

## 🐛 TROUBLESHOOTING

### Problem 1: Helper functions tidak dikenali

**Symptoms:**
```
Call to undefined function getMinWithdrawal()
```

**Solution:**
```bash
# Regenerate autoload
composer dump-autoload

# Clear cache
php artisan cache:clear
php artisan config:clear
```

---

### Problem 2: Cache tidak bekerja

**Symptoms:**
- Setiap request tetap query database
- Performance tidak meningkat

**Solution:**
```bash
# Check cache driver
php artisan tinker
>>> env('CACHE_DRIVER')

# Test cache manually
>>> Cache::put('test', 'value', 60);
>>> Cache::get('test');

# If null, cache driver not working
# Configure in .env:
# CACHE_DRIVER=redis
# Or use file cache (default)
```

---

### Problem 3: Settings tidak terupdate

**Symptoms:**
- Settings diupdate tapi masih tampil lama
- Cache tidak clear

**Solution:**
```bash
# Clear cache manually
php artisan cache:clear

# Or clear specific cache
php artisan tinker
>>> clearSettingCache('admin_phone');
```

---

## 📝 TESTING CHECKLIST

### Manual Testing:
- [ ] Login sebagai Sales
- [ ] Buka Sales Dashboard → Refresh → Check performance
- [ ] Buka Sales Withdraw → Refresh → Check performance
- [ ] Update Sales Settings → Verify update
- [ ] Login sebagai Admin
- [ ] Buka Admin Token → Refresh → Check performance
- [ ] Update Admin Settings → Verify update

### Automated Testing (Tinker):
- [ ] Test helper functions availability
- [ ] Test cache hit performance
- [ ] Test cache clear after update
- [ ] Test database query count
- [ ] Test cache expiration
- [ ] Test multiple settings
- [ ] Test error handling

---

## ✅ SUCCESS CRITERIA

Cache implementation considered successful when:

1. ✅ Helper functions available and working
2. ✅ First call: 1 query per setting
3. ✅ Second call: 0 queries (cache hit)
4. ✅ Performance gain: 50-70% faster
5. ✅ Settings update correctly
6. ✅ Cache clears after update
7. ✅ No PHP errors or warnings

---

## 🎉 CONCLUSION

**If all tests pass:** ✅ Cache implementation is working correctly!

**If some tests fail:** Review troubleshooting section above

**Next steps after successful testing:**
- Deploy to staging environment
- Test with real user traffic
- Monitor cache hit rate
- Adjust TTL if needed

---

**Happy Testing! 🚀**