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
3 changes: 2 additions & 1 deletion plugin/error-handler/src/ErrorHandlerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Testo\ErrorHandler;

use Internal\Container\Container;
use Testo\Common\Messenger;
use Testo\Common\PluginConfigurator;
use Testo\ErrorHandler\Internal\ErrorHandlerInterceptor;
use Testo\Pipeline\InterceptorCollector;
Expand Down Expand Up @@ -32,6 +33,6 @@
public function configure(Container $container): void
{
$container->get(InterceptorCollector::class)
->addInterceptor(new ErrorHandlerInterceptor($this->failOnError));
->addInterceptor(new ErrorHandlerInterceptor($container->get(Messenger::class), $this->failOnError));

Check warning on line 36 in plugin/error-handler/src/ErrorHandlerPlugin.php

View check run for this annotation

Codecov / codecov/patch

plugin/error-handler/src/ErrorHandlerPlugin.php#L36

Added line #L36 was not covered by tests
}
}
42 changes: 17 additions & 25 deletions plugin/error-handler/src/Internal/ErrorHandlerInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Testo\Core\Context\TestInfo;
use Testo\Core\Context\TestResult;
use Testo\Core\Log\Level;
use Testo\Core\Log\Message;
use Testo\Core\Log\MessageLog;
use Testo\Core\Value\Status;
use Testo\ErrorHandler\CapturedErrors;
use Testo\ErrorHandler\Exception\ErrorHandlerUnchanged;
Expand All @@ -26,8 +24,8 @@
* Installs a capturing error handler for the duration of the test and forwards every error to the
* handler that was active before. Errors silenced with `@` or excluded by `error_reporting()` are
* forwarded but not captured. Captured errors are stored in the returned {@see TestResult} as a
* {@see CapturedErrors} attribute; those the previous handler did not take are also written to the
* `stderr` message channel in place of PHP's own printing. `E_USER_ERROR` is thrown as an
* {@see CapturedErrors} attribute; those the previous handler did not take are also logged to the
* `stderr` channel of the {@see Messenger} in place of PHP's own printing. `E_USER_ERROR` is thrown as an
* {@see \ErrorException}.
*
* A passing test that leaves the handler stack changed is reported {@see Status::Risky} unless it
Expand All @@ -48,10 +46,13 @@
private const CHANNEL = 'error-handler';

/**
* @param Messenger $messenger Receives the unhandled errors and the plugin's verdicts on the
* handler stack, attributed to the running test.
* @param bool $failOnError When true, any captured error upgrades a passing test to
* {@see Status::Failed} with the first error wrapped in an {@see \ErrorException} as the failure.
*/
public function __construct(
private Messenger $messenger,
private bool $failOnError = false,
) {}

Expand All @@ -61,35 +62,27 @@ public function runTest(TestInfo $info, callable $next): TestResult
if (self::declares($info, WithoutErrorHandler::class)) {
$result = $next($info);

return self::declares($info, ExpectErrorHandlerChange::class)
? $result->withMessages(new MessageLog([
...$result->messages->all(),
new Message(
\microtime(true),
Messenger::CHANNEL_STDERR,
Level::Warning,
'#[ExpectErrorHandlerChange] has no effect together with #[WithoutErrorHandler]: no handler is installed, so no stack change is checked.',
),
]))
: $result;
self::declares($info, ExpectErrorHandlerChange::class) and $this->messenger->log(
Messenger::CHANNEL_STDERR,
'#[ExpectErrorHandlerChange] has no effect together with #[WithoutErrorHandler]: no handler is installed, so no stack change is checked.',
Level::Warning,
);

return $result;
}

$scope = new HandlerScope();
$scope = new HandlerScope($this->messenger);
$result = $this->run($info, $next, $scope);

if ($result->status === Status::Passed) {
$declared = self::declares($info, ExpectErrorHandlerChange::class);
if ($declared && !$scope->changed()) {
$result = $result->with(status: Status::Failed)->withFailure(new ErrorHandlerUnchanged());
} elseif (!$declared && $scope->changed()) {
$result = $result
->with(status: Status::Risky)
->withMessages(new MessageLog([
...$result->messages->all(),
new Message(\microtime(true), self::CHANNEL, Level::Warning, $scope->removed()
? 'Test code or tested code removed error handlers other than its own.'
: 'Test code or tested code did not remove its own error handlers.'),
]));
$result = $result->with(status: Status::Risky);
$this->messenger->log(self::CHANNEL, $scope->removed()
? 'Test code or tested code removed error handlers other than its own.'
: 'Test code or tested code did not remove its own error handlers.', Level::Warning);
}
}

Expand All @@ -98,7 +91,6 @@ public function runTest(TestInfo $info, callable $next): TestResult
}

$result = $result->withAttribute(CapturedErrors::class, new CapturedErrors($scope->errors));
$scope->stderr === [] or $result = $result->withMessages(new MessageLog([...$result->messages->all(), ...$scope->stderr]));

if ($this->failOnError && $result->status === Status::Passed) {
$first = $scope->errors[0];
Expand Down
16 changes: 7 additions & 9 deletions plugin/error-handler/src/Internal/HandlerScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Testo\Common\Messenger;
use Testo\Core\Log\Level;
use Testo\Core\Log\Message;
use Testo\ErrorHandler\CapturedError;

/**
Expand All @@ -25,9 +24,6 @@ final class HandlerScope
/** @var list<CapturedError> */
public array $errors = [];

/** @var list<Message> What PHP would have printed for the errors nobody handled. */
public array $stderr = [];

private readonly \Closure $handler;

/** @var list<callable> Stack as it was before {@see install()}, bottom first. */
Expand All @@ -40,9 +36,12 @@ final class HandlerScope
private bool $removed = false;
private bool $left = false;

public function __construct()
/**
* @param Messenger $messenger Receives what PHP would have printed for the errors nobody handled.
*/
public function __construct(Messenger $messenger)
{
$this->handler = function (int $severity, string $message, string $file, int $line): bool {
$this->handler = function (int $severity, string $message, string $file, int $line) use ($messenger): bool {
// Forwarded first: a previous handler that throws turns the error into control flow of
// the test, and then there is nothing to capture.
$handled = $this->previous !== null && (bool) ($this->previous)($severity, $message, $file, $line);
Expand All @@ -57,11 +56,10 @@ public function __construct()

$error = new CapturedError($severity, $message, $file, $line, $handled);
$this->errors[] = $error;
$handled or $this->stderr[] = new Message(
\microtime(true),
$handled or $messenger->log(
Messenger::CHANNEL_STDERR,
self::level($severity),
(string) $error,
self::level($severity),
['severity' => $severity, 'file' => $file, 'line' => $line],
);

Expand Down
41 changes: 41 additions & 0 deletions plugin/error-handler/tests/Feature/StderrFeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Tests\ErrorHandler\Feature;

use Testo\Assert;
use Testo\Codecov\Covers;
use Testo\Common\Messenger;
use Testo\Core\Value\Status;
use Testo\ErrorHandler\Internal\ErrorHandlerInterceptor;
use Testo\ErrorHandler\Internal\HandlerScope;
use Testo\ErrorHandler\WithoutErrorHandler;
use Testo\Test;
use Testo\Testing\Attribute\TestingSuite;
use Testo\Testing\Helper\TestRunner;
use Tests\ErrorHandler\Stub\Warning\RaisesWarningStub;

/**
* An unhandled error reaches the test's output next to whatever else the test wrote, such as the
* assertion history.
*
* The outer run stays out of the plugin: its handler would take the stub's error as handled.
*/
#[Test]
#[TestingSuite(path: __DIR__ . '/../Stub/Warning')]
#[Covers(ErrorHandlerInterceptor::class)]
#[Covers(HandlerScope::class)]
#[WithoutErrorHandler]
final class StderrFeatureTest
{
public function unhandledErrorReachesTheTestOutput(): void
{
$result = TestRunner::runTest([RaisesWarningStub::class, 'raisesWarning']);

Assert::same($result->status, Status::Passed);
$stderr = $result->messages->channel(Messenger::CHANNEL_STDERR);
Assert::count($stderr, 1);
Assert::string($stderr[0]->content)->startsWith('Warning: Stub warning in ');
}
}
Loading
Loading