diff --git a/core/Pipeline/Internal/OutputInterceptor.php b/core/Pipeline/Internal/OutputInterceptor.php index 9a293d07..6ada961d 100644 --- a/core/Pipeline/Internal/OutputInterceptor.php +++ b/core/Pipeline/Internal/OutputInterceptor.php @@ -7,12 +7,14 @@ use Testo\Common\Messenger; use Testo\Core\Context\TestInfo; use Testo\Core\Context\TestResult; +use Testo\Core\Log\Message; +use Testo\Core\Log\MessageLog; use Testo\Pipeline\Attribute\InterceptorOptions; use Testo\Pipeline\Middleware\TestRunInterceptor; /** - * Forks the {@see Messenger} per test and attaches the messages recorded during the test to its - * {@see TestResult}. + * Forks the {@see Messenger} per test and adds the messages recorded during the test to its + * {@see TestResult}, next to any messages an inner interceptor attached to the result itself. * * The native output buffer itself is opened once per suite by {@see \Testo\Application\Config\DefaultServicesConfig} * and funnels everything into the `stdout` channel. This interceptor only opens a {@see Messenger::scope()} @@ -54,7 +56,24 @@ public function runTest(TestInfo $info, callable $next): TestResult return $messages->isEmpty() ? $result - : $result->withMessages($messages); + : $result->withMessages(self::merge($result->messages, $messages)); }, $info->identity); } + + /** + * Combines the messages an inner interceptor attached to the result with the ones recorded in + * the scope, in time order. The sort is stable, so on equal timestamps the attached messages + * come first. + */ + private static function merge(MessageLog $attached, MessageLog $recorded): MessageLog + { + if ($attached->isEmpty()) { + return $recorded; + } + + $merged = [...$attached->all(), ...$recorded->all()]; + \usort($merged, static fn(Message $a, Message $b): int => $a->time <=> $b->time); + + return new MessageLog($merged); + } } diff --git a/tests/Core/Pipeline/OutputInterceptorTest.php b/tests/Core/Pipeline/OutputInterceptorTest.php index f3c1d101..e612003b 100644 --- a/tests/Core/Pipeline/OutputInterceptorTest.php +++ b/tests/Core/Pipeline/OutputInterceptorTest.php @@ -124,6 +124,64 @@ public function runTestPreservesResultStatusWhenAttachingMessages(): void Assert::false($result->messages->isEmpty()); } + public function runTestKeepsMessagesAttachedByInnerInterceptorInTimeOrder(): void + { + $testInfo = $this->makeTestInfo(); + $messenger = new TestMessenger(); + $interceptor = new OutputInterceptor($messenger); + + $result = $interceptor->runTest($testInfo, static function () use ($messenger, $testInfo): TestResult { + $messenger->recordMessage(new Message(1.0, 'stdout', Level::Info, 'first output')); + $messenger->recordMessage(new Message(3.0, 'assert-history', Level::Info, 'history')); + return new TestResult($testInfo, Status::Passed, messages: new MessageLog([ + new Message(2.0, 'stderr', Level::Warning, 'attached by inner interceptor'), + ])); + }); + + Assert::same( + \array_map(static fn(Message $message): string => $message->content, $result->messages->all()), + ['first output', 'attached by inner interceptor', 'history'], + ); + } + + public function runTestPutsAttachedMessagesFirstOnEqualTime(): void + { + $testInfo = $this->makeTestInfo(); + $messenger = new TestMessenger(); + $interceptor = new OutputInterceptor($messenger); + + $result = $interceptor->runTest($testInfo, static function () use ($messenger, $testInfo): TestResult { + $messenger->recordMessage(new Message(1.0, 'stdout', Level::Info, 'recorded')); + return new TestResult($testInfo, Status::Passed, messages: new MessageLog([ + new Message(1.0, 'stderr', Level::Warning, 'attached'), + ])); + }); + + Assert::same( + \array_map(static fn(Message $message): string => $message->content, $result->messages->all()), + ['attached', 'recorded'], + ); + } + + public function runTestDoesNotDuplicateMessagesOfNestedScope(): void + { + $testInfo = $this->makeTestInfo(); + $messenger = new TestMessenger(); + $outer = new OutputInterceptor($messenger); + $inner = new OutputInterceptor($messenger); + + $result = $outer->runTest($testInfo, static fn(TestInfo $info): TestResult => $inner->runTest( + $info, + static function () use ($messenger, $info): TestResult { + $messenger->recordMessage(new Message(1.0, 'stdout', Level::Info, 'inner output')); + return new TestResult($info, Status::Passed); + }, + )); + + Assert::count($result->messages, 1); + Assert::same($result->messages->all()[0]->content, 'inner output'); + } + private function makeTestInfo(): TestInfo { $caseDefinition = new CaseDefinition(