From fe05f17cd73869565d42e1ba7f3e84be1d890972 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Thu, 19 Nov 2026 10:27:40 +0400 Subject: [PATCH] fix(core): keep messages an interceptor attached to the test result OutputInterceptor replaced the result's message log with the messages recorded in the test's messenger scope, so anything an inner interceptor attached via `withMessages()` was lost whenever the scope recorded output (the assert plugin writes assertion history for nearly every test). Merge both logs in time order instead, attached messages first on equal time. Assisted-By: Claude Opus 5.5 --- core/Pipeline/Internal/OutputInterceptor.php | 25 +++++++- tests/Core/Pipeline/OutputInterceptorTest.php | 58 +++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) 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(