diff --git a/DIRECTORY.md b/DIRECTORY.md index 6a5139c..995e7a4 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -188,6 +188,7 @@ * [Stringstest](./tests/Strings/StringsTest.php) * Utils * [Arrayhelperstest](./tests/Utils/ArrayHelpersTest.php) + * [Executiontimetest](./tests/Utils/ExecutionTimeTest.php) ## Utils * [Arrayhelpers](./Utils/ArrayHelpers.php) diff --git a/tests/Utils/ExecutionTimeTest.php b/tests/Utils/ExecutionTimeTest.php new file mode 100644 index 0000000..14bc0df --- /dev/null +++ b/tests/Utils/ExecutionTimeTest.php @@ -0,0 +1,46 @@ +assertSame('', ob_get_contents()); + + unset($timer); // ...the report is emitted by __destruct() + + $output = ob_get_clean(); + + $this->assertMatchesRegularExpression('/^Executed in \d+\.\d+ seconds$/', $output); + } + + public function testMeasuredIntervalExceedsSleepDuration(): void + { + $sleepMicroseconds = 20000; // 20 ms + + ob_start(); + $timer = new ExecutionTime(); + usleep($sleepMicroseconds); + unset($timer); + ob_end_clean(); + + // Re-run with a capture of the measured value to assert ordering. + ob_start(); + $timer = new ExecutionTime(); + usleep($sleepMicroseconds); + unset($timer); + $output = ob_get_clean(); + + $this->assertSame(1, preg_match('/Executed in (\d+\.\d+) seconds/', $output, $matches)); + $this->assertGreaterThanOrEqual($sleepMicroseconds / 1_000_000, (float) $matches[1]); + } +}