120 lines
5.0 KiB
PHP
120 lines
5.0 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 = 'Testmål';
|
|
$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";
|
|
|
|
// Expectations:
|
|
$expect1 = 'Daglige mål: Afdeling 12=3, Afdeling 15=5';
|
|
$expect2 = '*Igår:* 0 ud af 8 (0.00%)'; // one day range, total target = 3 + 5 = 8
|
|
|
|
$ok = true;
|
|
if (strpos($msg, $expect1) === false) {
|
|
echo "✖ Missing daily targets header\n";
|
|
$ok = false;
|
|
} else {
|
|
echo "✔ Daily targets header present\n";
|
|
}
|
|
if (strpos($msg, $expect2) === false) {
|
|
echo "✖ Incorrect total target computation for period (expected 'Igår: 0 ud af 8 (0.00%)')\n";
|
|
$ok = false;
|
|
} else {
|
|
echo "✔ Period total uses sum of department targets\n";
|
|
}
|
|
|
|
if ($ok) {
|
|
echo "DepartmentDailyTargetsRendererTest completed.\n";
|
|
exit(0);
|
|
}
|
|
exit(1);
|
|
}
|