Skip to content

Commit

Permalink
Add event that is emitted when a test printed output (but does not pe…
Browse files Browse the repository at this point in the history
…rform assertions on it)
  • Loading branch information
sebastianbergmann committed Mar 11, 2023
1 parent f7adfb5 commit c168d82
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@
</PropertyTypeCoercion>
</file>
<file src="src/Framework/TestRunner.php">
<ArgumentTypeCoercion>
<code><![CDATA[$test->output()]]></code>
</ArgumentTypeCoercion>
<InvalidArgument>
<code>$var</code>
</InvalidArgument>
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog-10.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi

## [10.0.16] - 2023-MM-DD

### Added

* `PHPUnit\Event\Test\PrintedOutput` and `PHPUnit\Event\Test\PrintedOutputSubscriber` (required for the fixing [#5278](https://github.com/sebastianbergmann/phpunit/issues/5278))

### Changed

* [#5276](https://github.com/sebastianbergmann/phpunit/issues/5276): Restore display of summary and warnings when `--teamcity` is used
Expand Down
16 changes: 16 additions & 0 deletions src/Event/Emitter/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,22 @@ public function testTriggeredPhpunitWarning(Code\Test $test, string $message): v
);
}

/**
* @psalm-param non-empty-string $output
*
* @throws InvalidArgumentException
* @throws UnknownEventTypeException
*/
public function testPrintedOutput(string $output): void
{
$this->dispatcher->dispatch(
new Test\PrintedOutput(
$this->telemetryInfo(),
$output
)
);
}

/**
* @throws InvalidArgumentException
* @throws UnknownEventTypeException
Expand Down
5 changes: 5 additions & 0 deletions src/Event/Emitter/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public function testTriggeredPhpunitError(Code\Test $test, string $message): voi

public function testTriggeredPhpunitWarning(Code\Test $test, string $message): void;

/**
* @psalm-param non-empty-string $output
*/
public function testPrintedOutput(string $output): void;

public function testFinished(Code\Test $test, int $numberOfAssertionsPerformed): void;

/**
Expand Down
60 changes: 60 additions & 0 deletions src/Event/Events/Test/PrintedOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Event\Test;

use function sprintf;
use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;

/**
* @psalm-immutable
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
final class PrintedOutput implements Event
{
private readonly Telemetry\Info $telemetryInfo;

/**
* @psalm-var non-empty-string
*/
private readonly string $output;

/**
* @psalm-param non-empty-string $output
*/
public function __construct(Telemetry\Info $telemetryInfo, string $output)
{
$this->telemetryInfo = $telemetryInfo;
$this->output = $output;
}

public function telemetryInfo(): Telemetry\Info
{
return $this->telemetryInfo;
}

/**
* @psalm-return non-empty-string
*/
public function output(): string
{
return $this->output;
}

public function asString(): string
{
return sprintf(
'Test Printed Output%s%s',
PHP_EOL,
$this->output
);
}
}
20 changes: 20 additions & 0 deletions src/Event/Events/Test/PrintedOutputSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Event\Test;

use PHPUnit\Event\Subscriber;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*/
interface PrintedOutputSubscriber extends Subscriber
{
public function notify(PrintedOutput $event): void;
}
1 change: 1 addition & 0 deletions src/Event/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private static function registerDefaultTypes(TypeMap $typeMap): void
Test\PreConditionFinished::class,
Test\PreparationStarted::class,
Test\Prepared::class,
Test\PrintedOutput::class,
Test\Skipped::class,
Test\WarningTriggered::class,

Expand Down
4 changes: 4 additions & 0 deletions src/Framework/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ public function run(TestCase $test): void
);
}

if ($test->hasOutput()) {
Event\Facade::emitter()->testPrintedOutput($test->output());
}

if ($this->configuration->disallowTestOutput() && $test->hasOutput()) {
Event\Facade::emitter()->testConsideredRisky(
$test->valueObjectForEvents(),
Expand Down
2 changes: 2 additions & 0 deletions tests/end-to-end/event/test-risky-output.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Test Preparation Started (PHPUnit\TestFixture\Event\RiskyBecauseOutputTest::test
Test Prepared (PHPUnit\TestFixture\Event\RiskyBecauseOutputTest::testOne)
Assertion Succeeded (Constraint: is true, Value: true)
Test Passed (PHPUnit\TestFixture\Event\RiskyBecauseOutputTest::testOne)
Test Printed Output
*
Test Considered Risky (PHPUnit\TestFixture\Event\RiskyBecauseOutputTest::testOne)
This test printed output: *
Test Finished (PHPUnit\TestFixture\Event\RiskyBecauseOutputTest::testOne)
Expand Down

0 comments on commit c168d82

Please sign in to comment.