Files
api/services/nginx/app/tests/goals/DepartmentDailyTargetsRendererTest.php
T
Jeppe Bundgaard ff2d9788bf Remove dependency on the ws module.
Deleted the entire `ws` library and its associated files. This likely reflects a move away from the WebSocket library, possibly signaling an alternate implementation or unused/legacy code cleanup.
2026-04-14 13:45:17 +02:00

128 lines
5.3 KiB
PHP

<?php
/**
* DepartmentDailyTargetsRendererTest.php
*
* Minimal, env-agnostic test to validate that custom per-department daily targets
* are taken into account by the Slack renderer and are shown in the message.
*/
namespace {
if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
}
// --- Lightweight stubs to avoid DB access ---
namespace objects {
class departments_o {
public int $id = 0;
public $name;
public function select(int $id): self {
$o = new self();
$o->id = $id;
$o->name = new class($id) {
private int $id;
public function __construct(int $id) { $this->id = $id; }
public function value(): string { return 'Afdeling ' . $this->id; }
};
return $o;
}
public function exists(): bool { return true; }
}
}
namespace {
// --- Includes: enums, traits, interfaces, classes used by goals_criteria ---
require_once WD . '/modules/goals/helpers/goals_criteria_type.php';
require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_frequency.php';
require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_destination.php';
require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_progress_type.php';
require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_style.php';
require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_weekday.php';
require_once WD . '/modules/goals/traits/goals_timeframe_t.php';
require_once WD . '/modules/goals/traits/goals_target_t.php';
require_once WD . '/modules/goals/traits/goals_result_parser_t.php';
require_once WD . '/modules/goals/traits/goals_label_t.php';
require_once WD . '/modules/goals/interfaces/goals_criteria_i.php';
require_once WD . '/modules/goals/interfaces/goals_criteria_users_i.php';
require_once WD . '/modules/goals/interfaces/goals_criteria_departments_i.php';
require_once WD . '/modules/goals/interfaces/goals_criteria_products_i.php';
require_once WD . '/modules/goals/traits/goals_criteria_users_t.php';
require_once WD . '/modules/goals/traits/goals_criteria_departments_t.php';
require_once WD . '/modules/goals/traits/goals_criteria_products_t.php';
require_once WD . '/modules/goals/classes/goals_criteria_users.php';
require_once WD . '/modules/goals/classes/goals_criteria_departments.php';
require_once WD . '/modules/goals/classes/goals_criteria_products.php';
require_once WD . '/modules/goals/classes/goals_criteria.php';
require_once WD . '/modules/goals/traits/goals_criteria_progress_alert_format_t.php';
require_once WD . '/modules/goals/formats/slack_progress_alert_format.php';
require_once WD . '/modules/goals/formats/email_progress_alert_format.php';
require_once WD . '/modules/goals/formats/sms_progress_alert_format.php';
require_once WD . '/modules/goals/services/goals_progress_alert_renderer.php';
use goals\classes\goals_criteria;
use goals\helpers\goals_criteria_type as Type;
use goals\helpers\goals_criteria_progress_alert_destination as Dest;
use goals\helpers\goals_criteria_progress_alert_progress_type as PType;
// Build a criteria with two departments and custom per-department daily targets
$criteria = new goals_criteria();
$criteria->type = Type::NONE; // avoid DB in progress calc
$criteria->target = 100; // overall target (not used when overrides provided, but kept for completeness)
$criteria->label = 'Testmal';
$criteria->start = new \DateTime('yesterday 00:00:00');
$criteria->end = new \DateTime('yesterday 23:59:59');
$criteria->progress_alert_destination = Dest::SLACK;
$criteria->progress_alert_progress_type = PType::ALL;
// Departments (ids only)
$criteria->departments->set([
(new \objects\departments_o())->select(12),
(new \objects\departments_o())->select(15)
]);
// Override daily targets: dept 12 => 3, dept 15 => 5
$criteria->department_daily_targets = [12 => 3, 15 => 5];
// Render the Slack message
$msg = \goals\services\goals_progress_alert_renderer::render($criteria);
// Debug output
echo "--- Message ---\n" . $msg . "\n---------------\n";
$ok = true;
if (!preg_match('/^Daglige .+: Afdeling 12=3, Afdeling 15=5$/mu', $msg)) {
echo "Missing daily targets header\n";
$ok = false;
} else {
echo "Daily targets header present\n";
}
$operatingDays = $criteria->operating_days_of_week ?? [1, 2, 3, 4, 5];
$yesterdayDow = (int)(new \DateTimeImmutable('yesterday'))->format('N');
$expectedYesterdayTarget = in_array($yesterdayDow, $operatingDays, true)
? (int)array_sum(array_map(static fn($target): int => (int)$target, $criteria->department_daily_targets ?? []))
: 0;
$expectedYesterdayLinePattern = '/^\*Ig.+:\* 0 ud af ' . preg_quote((string)$expectedYesterdayTarget, '/') . ' \(0\.00%\)$/mu';
if (!preg_match($expectedYesterdayLinePattern, $msg)) {
echo "Incorrect total target computation for period (expected Igar line with 0 ud af {$expectedYesterdayTarget})\n";
$ok = false;
} else {
echo "Period total uses current renderer output\n";
}
if ($ok) {
echo "DepartmentDailyTargetsRendererTest completed.\n";
exit(0);
}
exit(1);
}