The v2 rewrite of processLogEntries() calls consoleLog(...) at two
skip branches (no id, and no deposited/withdrawn). Commit 3ebc5e2 had
removed the project helper from includes/utilities.php, leaving a
production runtime fatal ("Call to undefined function consoleLog()")
the first time a non-vault entry flowed through fetchAndStoreLogPage
-> processLogEntries in real use.
The previous test appeared to pass only because it defined a local
no-op consoleLog stub via function_exists() guard — masking the defect
behind a test fixture that never reached either skip branch.
Fix:
- Restore the original consoleLog() helper to
includes/utilities.php (unchanged from the pre-3ebc5e2 codebase;
spec lists it as unchanged).
- Remove the local consoleLog stub from
tests/process_log_entries_test.php so the test now exercises
the real production code path.
98 lines
3.3 KiB
PHP
98 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* Golden-file test for processLogEntries() v2 behavior.
|
|
*
|
|
* Loads backup/api-sample-new.json, runs each entry through the v2
|
|
* processor against an in-memory SQLite database, and asserts the
|
|
* resulting rows match the expected (id, user, timestamp, description,
|
|
* amount) tuples derived from the fixture.
|
|
*
|
|
* Run: php tests/process_log_entries_test.php
|
|
* Exit code 0 = pass.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Make USER_KEYS available without including config.php (it would force a
|
|
// real DB connection).
|
|
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");
|
|
}
|
|
}
|
|
|
|
// In-memory DB so we don't touch the user's vault.db.
|
|
$pdo = new PDO('sqlite::memory:');
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$pdo->exec(
|
|
'CREATE TABLE vault (
|
|
id TEXT PRIMARY KEY,
|
|
user TEXT NOT NULL,
|
|
timestamp INTEGER NOT NULL,
|
|
description TEXT NOT NULL,
|
|
amount REAL NOT NULL
|
|
)'
|
|
);
|
|
|
|
$insertStmt = $pdo->prepare(
|
|
'INSERT INTO vault (id, user, timestamp, description, amount) '
|
|
. 'VALUES (:id, :user, :timestamp, :description, :amount) '
|
|
. 'ON CONFLICT(id) DO NOTHING'
|
|
);
|
|
|
|
// Load the golden file.
|
|
$fixturePath = __DIR__ . '/../backup/api-sample-new.json';
|
|
if (!is_readable($fixturePath)) {
|
|
fail("Fixture not readable at $fixturePath");
|
|
}
|
|
$fixture = json_decode(file_get_contents($fixturePath), true);
|
|
if (!is_array($fixture) || !isset($fixture['log'])) {
|
|
fail('Fixture is missing the log array.');
|
|
}
|
|
|
|
processLogEntries($fixture['log'], 'zarathos', $insertStmt);
|
|
|
|
// 100 entries in the fixture.
|
|
$count = (int)$pdo->query('SELECT COUNT(*) FROM vault')->fetchColumn();
|
|
assertSame(100, $count, 'row count');
|
|
|
|
// Spot-check first entry (newest, withdraw) and last (oldest, deposit).
|
|
$rows = $pdo->query('SELECT id, user, timestamp, description, amount FROM vault ORDER BY timestamp DESC')->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
$first = $rows[0];
|
|
assertSame('c1pEDQ7jl3kuBHV4NqDI', $first['id'], 'first.id');
|
|
assertSame('zarathos', $first['user'], 'first.user');
|
|
assertSame(1785325489, (int)$first['timestamp'], 'first.timestamp');
|
|
assertSame('Vault withdraw', $first['description'], 'first.description');
|
|
assertSame(-498870964, (int)$first['amount'], 'first.amount');
|
|
|
|
$last = $rows[count($rows) - 1];
|
|
assertSame('8m5jrUKTiKk0SB4MxzIy', $last['id'], 'last.id');
|
|
assertSame(1714350415, (int)$last['timestamp'], 'last.timestamp');
|
|
assertSame('Vault deposit', $last['description'], 'last.description');
|
|
assertSame(231492, (int)$last['amount'], 'last.amount');
|
|
|
|
// Re-running with the same fixture inserts zero new rows (idempotency).
|
|
processLogEntries($fixture['log'], 'zarathos', $insertStmt);
|
|
$count2 = (int)$pdo->query('SELECT COUNT(*) FROM vault')->fetchColumn();
|
|
assertSame(100, $count2, 'row count after re-run');
|
|
|
|
echo "OK: processLogEntries v2 golden-file test passed (100 entries).\n";
|