Remove unused fetchLiveVaultBalance and its test

This commit is contained in:
Keith Solomon
2026-08-07 17:50:45 -05:00
parent dd820f6c8a
commit a7f35e79e2
2 changed files with 0 additions and 151 deletions
-68
View File
@@ -14,11 +14,6 @@
* @link https://github.com/ksolomon/Torn-Vault-Tracker
*/
// Test hook for fetchLiveVaultBalance(). When set, this closure is called
// instead of executeApiCall. Set $GLOBALS['liveBalanceTestHook'] to a
// closure($url, $apiKey): array in test code. Production code leaves it null.
$GLOBALS['liveBalanceTestHook'] = $GLOBALS['liveBalanceTestHook'] ?? null;
/**
* Checks if the database is new and empty.
*
@@ -343,66 +338,3 @@ function refetchRunningBalances($pdo, $user) {
$url = $responseData['_metadata']['links']['next'] ?? null;
} while ($url !== null);
}
/**
* Fetch the live vault balance for a user from the v2 /money endpoint.
*
* Returns the `money.vault` value as an int, or null if the API call
* fails, returns invalid JSON, or doesn't include `money.vault`. The
* result is cached for the duration of the PHP request so multiple
* callers (e.g., `vaultLoop` for one user, then the all-users sum)
* don't re-fetch.
*
* @param string $user The user whose vault balance to fetch.
*
* @return int|null The vault amount in pennies, or null on failure.
*/
function fetchLiveVaultBalance($user) {
static $prodCache = []; // production: hook is null, plain per-request array
static $testCache = null; // test: keyed by hook closure (weakly held)
if (!array_key_exists($user, USER_KEYS)) {
return null;
}
$hook = $GLOBALS['liveBalanceTestHook'] ?? null;
if ($hook === null) {
// Production path: plain array cache.
if (array_key_exists($user, $prodCache)) {
return $prodCache[$user];
}
try {
$responseData = executeApiCall('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
if (!isset($responseData['money']['vault'])) {
$prodCache[$user] = null;
return null;
}
$prodCache[$user] = (int)$responseData['money']['vault'];
return $prodCache[$user];
} catch (Exception $e) {
$prodCache[$user] = null;
return null;
}
}
// Test path: key by hook identity so test scenarios get fresh fetches.
if ($testCache === null) {
$testCache = new \WeakMap();
}
if (isset($testCache[$hook]) && array_key_exists($user, $testCache[$hook])) {
return $testCache[$hook][$user];
}
try {
$responseData = $hook('https://api.torn.com/v2/user?selections=money', USER_KEYS[$user]);
if (!isset($responseData['money']['vault'])) {
$testCache[$hook] = [$user => null];
return null;
}
$testCache[$hook] = [$user => (int)$responseData['money']['vault']];
return $testCache[$hook][$user];
} catch (Exception $e) {
$testCache[$hook] = [$user => null];
return null;
}
}
-83
View File
@@ -1,83 +0,0 @@
<?php
/**
* Test for fetchLiveVaultBalance() — the live vault balance helper.
*
* Uses a swappable test hook so we don't make real API calls.
*
* Run: php tests/live_balance_test.php
* Exit code 0 = pass.
*/
declare(strict_types=1);
if (!defined('USER_KEYS')) {
define('USER_KEYS', [
'zarathos' => 'test-key-not-used',
'symos' => 'test-key-not-used',
]);
}
require_once __DIR__ . '/../includes/exceptions.php';
require_once __DIR__ . '/../includes/utilities.php';
function fail(string $message): void {
fwrite(STDERR, "FAIL: $message\n");
exit(1);
}
function assertSame($expected, $actual, string $label): void {
if ($expected !== $actual) {
$exp = var_export($expected, true);
$act = var_export($actual, true);
fail("$label: expected $exp, got $act");
}
}
// Test hook: when set, fetchLiveVaultBalance calls this closure instead of
// executeApiCall. The closure receives ($url, $apiKey) and returns the
// decoded response array (or throws).
$GLOBALS['liveBalanceTestHook'] = null;
function liveBalanceHook($url, $apiKey) {
$hook = $GLOBALS['liveBalanceTestHook'] ?? null;
if ($hook === null) {
fail('Test hook not set — fetchLiveVaultBalance called executeApiCall');
}
return $hook($url, $apiKey);
}
// --- Test 1: success path returns the vault amount as int ---
$GLOBALS['liveBalanceTestHook'] = function ($url, $apiKey) {
assertSame('zarathos', $apiKey === 'test-key-not-used' ? 'zarathos' : 'symos', 'api key routed');
// (just verify the hook got called)
return ['money' => ['vault' => 160627669, 'wallet' => 4000]];
};
$balance = fetchLiveVaultBalance('zarathos');
assertSame(160627669, $balance, 'success returns vault amount');
// --- Test 2: failure path returns null ---
$GLOBALS['liveBalanceTestHook'] = function () {
throw new CurlErrorException('connection refused');
};
$balance = fetchLiveVaultBalance('zarathos');
assertSame(null, $balance, 'failure returns null');
// --- Test 3: missing money.vault returns null ---
$GLOBALS['liveBalanceTestHook'] = function () {
return ['error' => ['code' => 4]];
};
$balance = fetchLiveVaultBalance('zarathos');
assertSame(null, $balance, 'missing field returns null');
// --- Test 4: cache: second call does not invoke the hook twice ---
$invocations = 0;
$GLOBALS['liveBalanceTestHook'] = function () use (&$invocations) {
$invocations++;
return ['money' => ['vault' => 100]];
};
fetchLiveVaultBalance('symos');
fetchLiveVaultBalance('symos');
fetchLiveVaultBalance('symos');
assertSame(1, $invocations, 'cache prevents repeated fetches');
echo "OK: fetchLiveVaultBalance tests passed.\n";