Files
api/services/nginx/app/tests/Integration/DailyReports/DepartmentDailyReportComplaintsIntegrationTest.php
T

104 lines
4.4 KiB
PHP

<?php
use classes\db;
use classes\department_daily_report_complaints_schema_bootstrap;
use objects\department_daily_report_complaints_o;
function department_daily_report_complaints_integration_db(): db
{
if (!integration_enabled()) {
test()->markTestSkipped('Set RUN_INTEGRATION_TESTS=1 to run DB integration tests.');
}
$host = getenv('CONFIG_DB_HOST') ?: null;
$user = getenv('CONFIG_DB_USER') ?: null;
$password = getenv('CONFIG_DB_PASSWORD') ?: '';
$database = getenv('CONFIG_DB_DATABASE') ?: null;
$port = getenv('CONFIG_DB_PORT');
if (!$host || !$user || !$database) {
test()->markTestSkipped('Missing DB env vars: CONFIG_DB_HOST/CONFIG_DB_USER/CONFIG_DB_DATABASE.');
}
app_require('classes/db.php');
app_require('classes/department_daily_report_complaints_schema_bootstrap.php');
app_require('objects/department_daily_report_complaints_o.php');
$GLOBALS['response'] = new class {
public function error(string $message): void
{
throw new RuntimeException($message);
}
public function internal_server_error(string $message): void
{
throw new RuntimeException($message);
}
};
$db = new db([
'host' => $host,
'user' => $user,
'password' => $password,
'database' => $database,
'port' => $port !== false && $port !== null ? (int)$port : 3306,
]);
$db->connect();
$GLOBALS['db'] = $db;
department_daily_report_complaints_schema_bootstrap::ensureTables();
return $db;
}
it('counts complaint rows by department and created_at reporting range', function (): void {
$db = department_daily_report_complaints_integration_db();
$repository = new department_daily_report_complaints_o();
$suffix = (string)random_int(10000, 99999);
$department_ids = [];
$complaint_ids = [];
try {
$db->query("INSERT INTO departments (name, description) VALUES ('" . $db->escape_string('Complaints A ' . $suffix) . "', 'Integration A')");
$department_a_id = (int)$db->insert_id();
$department_ids[] = $department_a_id;
$db->query("INSERT INTO departments (name, description) VALUES ('" . $db->escape_string('Complaints B ' . $suffix) . "', 'Integration B')");
$department_b_id = (int)$db->insert_id();
$department_ids[] = $department_b_id;
$complaint_one = $repository->addComplaint($department_a_id, null, 'Complaint one', 1);
$complaint_two = (new department_daily_report_complaints_o())->addComplaint($department_a_id, 12345, 'Complaint two', 2);
$complaint_three = (new department_daily_report_complaints_o())->addComplaint($department_b_id, null, 'Complaint three', 3);
$complaint_ids = [
(int)$complaint_one->id,
(int)$complaint_two->id,
(int)$complaint_three->id,
];
$db->query("UPDATE department_daily_report_complaints SET created_at = '2026-03-23 08:00:00' WHERE id = " . (int)$complaint_one->id);
$db->query("UPDATE department_daily_report_complaints SET created_at = '2026-03-24 09:15:00' WHERE id = " . (int)$complaint_two->id);
$db->query("UPDATE department_daily_report_complaints SET created_at = '2026-03-24 10:30:00' WHERE id = " . (int)$complaint_three->id);
$single_department_day_count = $repository->countForDepartmentsInRange([$department_a_id], '2026-03-23', '2026-03-23');
$combined_range_count = $repository->countForDepartmentsInRange([$department_a_id, $department_b_id], '2026-03-23', '2026-03-24');
$second_day_count = $repository->countForDepartmentsInRange([$department_a_id, $department_b_id], '2026-03-24', '2026-03-24');
expect($single_department_day_count)->toBe(1);
expect($combined_range_count)->toBe(3);
expect($second_day_count)->toBe(2);
expect($complaint_two->asArray())->toMatchObject([
'department_id' => $department_a_id,
'customer_number' => 12345,
'description' => 'Complaint two',
'created_by' => 2,
]);
} finally {
if ($complaint_ids !== []) {
$db->query('DELETE FROM department_daily_report_complaints WHERE id IN (' . implode(',', array_map('intval', $complaint_ids)) . ')');
}
if ($department_ids !== []) {
$db->query('DELETE FROM departments WHERE id IN (' . implode(',', array_map('intval', $department_ids)) . ')');
}
$db->close();
}
});