nhance/tests/smoke_enrollment_collection_v1.php
2026-06-05 11:32:52 +05:30

106 lines
4.4 KiB
PHP

<?php
/**
* Smoke test: Enrollment Collection V1 dashboard.
* Run: php tests/smoke_enrollment_collection_v1.php [policy_id]
*/
declare(strict_types=1);
use App\Controllers\EnrollmentCollectionV1DashboardController;
use App\Models\EnrollmentCollectionV1DashboardModel;
use Config\Services;
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;
}
helper('url');
$policyId = isset($argv[1]) ? (int) $argv[1] : 4687;
$pass = 0;
$fail = 0;
$results = [];
function ok_enrollment(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}" : '');
}
}
$model = new EnrollmentCollectionV1DashboardModel();
$kpiMap = EnrollmentCollectionV1DashboardModel::KPI_MAP;
$expectedCount = 10;
ok_enrollment('KPI_MAP count', count($kpiMap) === $expectedCount, (string) count($kpiMap));
$uniqueMethods = array_unique(array_values($kpiMap));
ok_enrollment('unique KPI method names', count($uniqueMethods) === $expectedCount, count($uniqueMethods) . ' methods');
ok_enrollment('id 115 maps to originally_enrolled', ($kpiMap[115] ?? '') === 'originally_enrolled');
ok_enrollment('id 119 maps to enrollment_relationship', ($kpiMap[119] ?? '') === 'enrollment_relationship');
ok_enrollment('id 124 maps to overall_active', ($kpiMap[124] ?? '') === 'overall_active');
ok_enrollment('id 125 maps to enrollment_age_group', ($kpiMap[125] ?? '') === 'enrollment_age_group');
ok_enrollment('id 126 maps to additions_premium', ($kpiMap[126] ?? '') === 'additions_premium');
ok_enrollment('id 127 maps to net_premium', ($kpiMap[127] ?? '') === 'net_premium');
try {
$rows = $model->originally_enrolled($policyId);
ok_enrollment('model originally_enrolled', is_array($rows), 'rows=' . count($rows));
} catch (Throwable $e) {
ok_enrollment('model originally_enrolled', false, $e->getMessage());
}
$request = Services::request(null, false);
$response = Services::response();
$request->setGlobal('get', ['client_policy' => (string) $policyId]);
$controller = new EnrollmentCollectionV1DashboardController();
$controller->initController($request, $response, service('logger'));
$slugResp = json_decode($controller->kpi('gender_split')->getJSON(), true);
ok_enrollment('controller kpi by slug', ($slugResp['status'] ?? false) === true && ($slugResp['kpi'] ?? '') === 'gender_split');
$idResp = json_decode($controller->kpi('115')->getJSON(), true);
ok_enrollment('controller kpi by id 115', ($idResp['status'] ?? false) === true && ($idResp['kpi_id'] ?? 0) === 115);
$allResp = json_decode($controller->all()->getJSON(), true);
ok_enrollment('controller all KPIs', ($allResp['status'] ?? false) === true && count($allResp['data'] ?? []) === $expectedCount);
$previewOut = $controller->preview($policyId);
$previewHtml = is_string($previewOut) ? $previewOut : $previewOut->getBody();
ok_enrollment('controller preview HTML', str_contains($previewHtml, 'Enrollment Collection V1') && str_contains($previewHtml, 'kpi-grid'));
$sparkRoutes = (string) shell_exec('php ' . escapeshellarg(ROOTPATH . 'spark') . ' routes 2>&1');
ok_enrollment('spark lists util preview route', str_contains($sparkRoutes, 'util/enrollment-collection-v1/preview'));
ok_enrollment('spark lists employeeRest preview route', str_contains($sparkRoutes, 'employeeRest/enrollment-collection-v1/preview'));
$previewUrl = rtrim((string) base_url(), '/') . '/util/enrollment-collection-v1/preview?client_policy=' . $policyId;
ob_end_clean();
echo '=== Enrollment Collection V1 smoke test (policy_id=' . $policyId . ') ===' . PHP_EOL . PHP_EOL;
echo implode(PHP_EOL, $results) . PHP_EOL;
echo PHP_EOL . '=== Admin preview URL (note util/ prefix) ===' . PHP_EOL;
echo $previewUrl . PHP_EOL;
echo PHP_EOL . "=== Summary: {$pass} passed, {$fail} failed ===" . PHP_EOL;
exit($fail > 0 ? 1 : 0);