Skip to content

Commit 149d510

Browse files
author
Ibrahim BinAlshikh
committed
feat(health): add getChecks() and afterAll() lifecycle hook
- getChecks() returns registered checks for introspection - afterAll() registers callbacks invoked after runAll() with aggregate results - reset() now also clears afterAll callbacks Closes #392 Closes #391
1 parent 28f77b4 commit 149d510

2 files changed

Lines changed: 90 additions & 1 deletion

File tree

WebFiori/Framework/Health/HealthCheck.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class HealthCheck {
2121
* @var array Registered checks indexed by name.
2222
*/
2323
private static array $checks = [];
24+
/**
25+
* @var array Callbacks to execute after runAll() completes.
26+
*/
27+
private static array $afterAllCallbacks = [];
2428
/**
2529
* Register a health check.
2630
*
@@ -68,17 +72,24 @@ public static function runAll(): array {
6872
}
6973
}
7074

71-
return [
75+
$aggregate = [
7276
'status' => $allOk ? 'ok' : 'fail',
7377
'timestamp' => date('c'),
7478
'checks' => $results,
7579
];
80+
81+
foreach (self::$afterAllCallbacks as $cb) {
82+
$cb($aggregate);
83+
}
84+
85+
return $aggregate;
7686
}
7787
/**
7888
* Remove all registered checks.
7989
*/
8090
public static function reset(): void {
8191
self::$checks = [];
92+
self::$afterAllCallbacks = [];
8293
}
8394
/**
8495
* Returns the number of registered checks.
@@ -88,4 +99,24 @@ public static function reset(): void {
8899
public static function getCheckCount(): int {
89100
return count(self::$checks);
90101
}
102+
/**
103+
* Returns all registered checks.
104+
*
105+
* @return array Associative array keyed by check name. Values are
106+
* HealthCheckInterface instances or callables.
107+
*/
108+
public static function getChecks(): array {
109+
return self::$checks;
110+
}
111+
/**
112+
* Register a callback to execute after all checks complete.
113+
*
114+
* The callback receives the aggregate result array with 'status',
115+
* 'timestamp', and 'checks' keys.
116+
*
117+
* @param callable $callback A function that accepts the aggregate results array.
118+
*/
119+
public static function afterAll(callable $callback): void {
120+
self::$afterAllCallbacks[] = $callback;
121+
}
91122
}

tests/WebFiori/Framework/Tests/Health/HealthCheckTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,62 @@ public function testResultToArray() {
130130
$this->assertEquals('fail', $arr['status']);
131131
$this->assertEquals('down', $arr['reason']);
132132
}
133+
134+
/** @test */
135+
public function testGetChecksReturnsRegisteredChecks() {
136+
HealthCheck::reset();
137+
HealthCheck::register(new PassingCheck());
138+
HealthCheck::register('custom', fn() => HealthCheckResult::ok());
139+
140+
$checks = HealthCheck::getChecks();
141+
$this->assertCount(2, $checks);
142+
$this->assertArrayHasKey('passing', $checks);
143+
$this->assertArrayHasKey('custom', $checks);
144+
$this->assertInstanceOf(HealthCheckInterface::class, $checks['passing']);
145+
$this->assertIsCallable($checks['custom']);
146+
}
147+
148+
/** @test */
149+
public function testAfterAllCallbackReceivesAggregateResults() {
150+
HealthCheck::reset();
151+
HealthCheck::register(new PassingCheck());
152+
HealthCheck::register(new FailingCheck());
153+
154+
$received = null;
155+
HealthCheck::afterAll(function (array $results) use (&$received) {
156+
$received = $results;
157+
});
158+
159+
$result = HealthCheck::runAll();
160+
161+
$this->assertNotNull($received);
162+
$this->assertEquals($result, $received);
163+
$this->assertEquals('fail', $received['status']);
164+
$this->assertArrayHasKey('passing', $received['checks']);
165+
$this->assertArrayHasKey('failing', $received['checks']);
166+
}
167+
168+
/** @test */
169+
public function testAfterAllMultipleCallbacks() {
170+
HealthCheck::reset();
171+
HealthCheck::register(new PassingCheck());
172+
173+
$count = 0;
174+
HealthCheck::afterAll(function () use (&$count) { $count++; });
175+
HealthCheck::afterAll(function () use (&$count) { $count++; });
176+
177+
HealthCheck::runAll();
178+
$this->assertEquals(2, $count);
179+
}
180+
181+
/** @test */
182+
public function testResetClearsAfterAllCallbacks() {
183+
HealthCheck::reset();
184+
$called = false;
185+
HealthCheck::afterAll(function () use (&$called) { $called = true; });
186+
HealthCheck::reset();
187+
HealthCheck::register(new PassingCheck());
188+
HealthCheck::runAll();
189+
$this->assertFalse($called);
190+
}
133191
}

0 commit comments

Comments
 (0)