Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions core/Pipeline/Internal/OutputInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down Expand Up @@ -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);
}
}
58 changes: 58 additions & 0 deletions tests/Core/Pipeline/OutputInterceptorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading