Skip to content

Commit 53c9f33

Browse files
committed
test: implement tests and ensure correct handling of global hook state
Signed-off-by: Tom Carrio <[email protected]>
1 parent 6e6b109 commit 53c9f33

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

hooks/OpenTelemetry/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
"dev:test:unit:teardown": "echo 'Tore down for unit tests...'",
113113
"dev:test:integration": [
114114
"@dev:test:integration:setup",
115-
"echo 'No integration tests to run'",
115+
"phpunit --colors=always --testdox --testsuite=integration",
116116
"@dev:test:integration:teardown"
117117
],
118118
"dev:test:integration:debug": "phpunit --colors=always --testdox -d xdebug.profiler_enable=on",

hooks/OpenTelemetry/src/OpenTelemetryHook.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function register(): void
4242
self::$instance = new OpenTelemetryHook();
4343
}
4444

45-
if (self::$registeredHook) {
45+
if (self::$registeredHook && self::isRegisteredInHooks()) {
4646
return;
4747
}
4848

@@ -88,4 +88,21 @@ public function supportsFlagValueType(string $flagValueType): bool
8888
{
8989
return true;
9090
}
91+
92+
/**
93+
* Hooks can be cleared by other means so we can't simply memoize whether a registration has occurred
94+
*
95+
* However if no registration has yet happened then we can absolutely determine that the hook will
96+
* not be registered yet.
97+
*/
98+
private static function isRegisteredInHooks(): bool
99+
{
100+
foreach (OpenFeatureAPI::getInstance()->getHooks() as $hook) {
101+
if ($hook instanceof OpenTelemetryHook) {
102+
return true;
103+
}
104+
}
105+
106+
return false;
107+
}
91108
}

hooks/OpenTelemetry/src/_autoload.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
// automatically registers the OTel hook for OpenFeature
4-
OpenFeature\Hooks\OpenTelemetry\OpenTelemetryHook::register();
6+
OpenFeature\Hooks\OpenTelemetry\OpenTelemetryHook::register();

hooks/OpenTelemetry/tests/integration/OpenTelemetryHookTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,26 @@
1111

1212
class OpenTelemetryHookTest extends TestCase
1313
{
14+
public function testIsRegisteredAutomatically(): void
15+
{
16+
// Given
17+
$api = OpenFeatureAPI::getInstance();
18+
$api->clearHooks();
19+
20+
// When
21+
// simulates the composer autoload
22+
require_once __DIR__ . '/../../src/_autoload.php';
23+
24+
// Then
25+
$this->assertNotEmpty($api->getHooks());
26+
$this->assertInstanceOf(Hook::class, $api->getHooks()[0]);
27+
}
28+
1429
public function testCanBeRegistered(): void
1530
{
1631
// Given
1732
$api = OpenFeatureAPI::getInstance();
33+
$api->clearHooks();
1834

1935
// When
2036
OpenTelemetryHook::register();

0 commit comments

Comments
 (0)