153 lines
5.2 KiB
PHP
153 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Smoke test: PolicyTransactionController::updateRelatedPolicyTransactionPolicyNo
|
|
* Run: php tests/smoke_inception_policy_no_sync.php [inception_pt_id]
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
ob_start();
|
|
|
|
define('FCPATH', __DIR__ . '/../public/');
|
|
chdir(FCPATH);
|
|
|
|
require FCPATH . '../app/Config/Paths.php';
|
|
$paths = new Config\Paths();
|
|
require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
|
|
require_once SYSTEMPATH . 'Config/DotEnv.php';
|
|
(new CodeIgniter\Config\DotEnv(ROOTPATH))->load();
|
|
|
|
defined('ENVIRONMENT') || define('ENVIRONMENT', env('CI_ENVIRONMENT', 'development'));
|
|
|
|
$boot = APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php';
|
|
if (is_file($boot)) {
|
|
require_once $boot;
|
|
}
|
|
|
|
use App\Controllers\PolicyTransactionController;
|
|
use App\Models\PolicyTransactionModel;
|
|
|
|
$pass = 0;
|
|
$fail = 0;
|
|
$results = [];
|
|
|
|
function ok(string $label, bool $cond, string $detail = ''): void
|
|
{
|
|
global $pass, $fail, $results;
|
|
if ($cond) {
|
|
$pass++;
|
|
$results[] = '[PASS] ' . $label . ($detail ? " — {$detail}" : '');
|
|
} else {
|
|
$fail++;
|
|
$results[] = '[FAIL] ' . $label . ($detail ? " — {$detail}" : '');
|
|
}
|
|
}
|
|
|
|
function invokePolicyNoSync(PolicyTransactionController $controller, int $inceptionPtId, string $oldPolicyNo, string $newPolicyNo): array
|
|
{
|
|
$method = new ReflectionMethod($controller, 'updateRelatedPolicyTransactionPolicyNo');
|
|
$method->setAccessible(true);
|
|
|
|
return $method->invoke($controller, $inceptionPtId, $oldPolicyNo, $newPolicyNo);
|
|
}
|
|
|
|
$db = db_connect('default');
|
|
$ptModel = new PolicyTransactionModel();
|
|
$controller = new PolicyTransactionController();
|
|
|
|
$inceptionPtId = isset($argv[1]) ? (int) $argv[1] : 0;
|
|
|
|
if ($inceptionPtId <= 0) {
|
|
$seed = $db->query(
|
|
"SELECT pt.id
|
|
FROM policy_transaction pt
|
|
WHERE pt.is_active = 1
|
|
AND pt.action_type = 'inception'
|
|
AND pt.policy_no IS NOT NULL
|
|
AND pt.policy_no <> ''
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM policy_transaction other
|
|
WHERE other.is_active = 1
|
|
AND other.policy_no = pt.policy_no
|
|
AND other.id <> pt.id
|
|
)
|
|
ORDER BY pt.id ASC
|
|
LIMIT 1"
|
|
)->getRowArray();
|
|
|
|
$inceptionPtId = (int) ($seed['id'] ?? 0);
|
|
}
|
|
|
|
ok('seed inception row found', $inceptionPtId > 0, "pt_id={$inceptionPtId}");
|
|
|
|
$inception = $ptModel->where('is_active', 1)->where('id', $inceptionPtId)->first();
|
|
ok('inception row loaded', !empty($inception), 'id=' . ($inception['id'] ?? 'n/a'));
|
|
|
|
$oldPolicyNo = trim((string) ($inception['policy_no'] ?? ''));
|
|
ok('inception has policy_no', $oldPolicyNo !== '', $oldPolicyNo);
|
|
|
|
$relatedBefore = $ptModel
|
|
->where('is_active', 1)
|
|
->where('id !=', $inceptionPtId)
|
|
->where('policy_no', $oldPolicyNo)
|
|
->findAll();
|
|
|
|
ok('related rows exist before sync', count($relatedBefore) > 0, 'count=' . count($relatedBefore));
|
|
|
|
$newPolicyNo = $oldPolicyNo . '_SMOKE_' . time();
|
|
$relatedIds = array_map(static fn(array $row): int => (int) $row['id'], $relatedBefore);
|
|
|
|
// No-op cases
|
|
$noChange = invokePolicyNoSync($controller, $inceptionPtId, $oldPolicyNo, $oldPolicyNo);
|
|
ok('no-op when policy_no unchanged', ($noChange['status'] ?? null) === false);
|
|
|
|
$emptyOld = invokePolicyNoSync($controller, $inceptionPtId, '', $newPolicyNo);
|
|
ok('no-op when old policy_no empty', ($emptyOld['status'] ?? null) === false);
|
|
|
|
// Apply sync on related rows only (inception row still has old policy_no for this call)
|
|
$result = invokePolicyNoSync($controller, $inceptionPtId, $oldPolicyNo, $newPolicyNo);
|
|
ok('sync returns success', ($result['status'] ?? null) === true, json_encode($result));
|
|
ok('sync updated expected row count', count($result['updated_ids'] ?? []) === count($relatedIds), 'updated=' . count($result['updated_ids'] ?? []));
|
|
|
|
foreach ($relatedIds as $relatedId) {
|
|
$row = $ptModel->where('id', $relatedId)->first();
|
|
ok("related row {$relatedId} has new policy_no", ($row['policy_no'] ?? '') === $newPolicyNo, $row['policy_no'] ?? 'missing');
|
|
}
|
|
|
|
$inceptionAfter = $ptModel->where('id', $inceptionPtId)->first();
|
|
ok('inception row unchanged by sync helper', ($inceptionAfter['policy_no'] ?? '') === $oldPolicyNo, $inceptionAfter['policy_no'] ?? 'missing');
|
|
|
|
$stillOld = $ptModel
|
|
->where('is_active', 1)
|
|
->where('id !=', $inceptionPtId)
|
|
->where('policy_no', $oldPolicyNo)
|
|
->countAllResults();
|
|
ok('no related rows left with old policy_no', $stillOld === 0, "remaining={$stillOld}");
|
|
|
|
// Restore test data
|
|
foreach ($relatedIds as $relatedId) {
|
|
$ptModel->where('id', $relatedId)->set(['policy_no' => $oldPolicyNo])->update();
|
|
}
|
|
|
|
$restored = $ptModel
|
|
->where('is_active', 1)
|
|
->where('id !=', $inceptionPtId)
|
|
->where('policy_no', $oldPolicyNo)
|
|
->countAllResults();
|
|
ok('related rows restored', $restored === count($relatedIds), "restored={$restored}");
|
|
|
|
echo PHP_EOL . '=== smoke_inception_policy_no_sync ===' . PHP_EOL;
|
|
echo 'inception_pt_id: ' . $inceptionPtId . PHP_EOL;
|
|
echo 'policy_no: ' . $oldPolicyNo . PHP_EOL;
|
|
echo 'related_rows: ' . count($relatedIds) . PHP_EOL;
|
|
echo PHP_EOL;
|
|
|
|
foreach ($results as $line) {
|
|
echo $line . PHP_EOL;
|
|
}
|
|
|
|
echo PHP_EOL . "Summary: {$pass} passed, {$fail} failed" . PHP_EOL;
|
|
|
|
exit($fail > 0 ? 1 : 0);
|