Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Möller <am@localheinz.com>
Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
localheinz and theseer committed Feb 1, 2021
1 parent fe473cb commit 647aa5a
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace PHPUnit\Event;

use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite as FrameworkTestSuite;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\GlobalState\Snapshot;

final class DispatchingEmitter implements Emitter
Expand Down Expand Up @@ -382,9 +384,14 @@ public function testSuiteLoaded(FrameworkTestSuite $testSuite): void
));
}

public function testSuiteRunFinished(): void
public function testSuiteRunFinished(string $name, TestResult $result, ?CodeCoverage $codeCoverage): void
{
$this->dispatcher->dispatch(new TestSuite\RunFinished($this->telemetryInfo()));
$this->dispatcher->dispatch(new TestSuite\RunFinished(
$this->telemetryInfo(),
$name,
(new TestResultMapper())->map($result),
$codeCoverage
));
}

public function testSuiteSorted(int $executionOrder, int $executionOrderDefects, bool $resolveDependencies): void
Expand Down
4 changes: 3 additions & 1 deletion src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace PHPUnit\Event;

use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\GlobalState\Snapshot;

interface Emitter
Expand Down Expand Up @@ -170,7 +172,7 @@ public function testSuiteAfterClassFinished(): void;

public function testSuiteLoaded(TestSuite $testSuite): void;

public function testSuiteRunFinished(): void;
public function testSuiteRunFinished(string $name, TestResult $result, ?CodeCoverage $codeCoverage): void;

public function testSuiteSorted(int $executionOrder, int $executionOrderDefects, bool $resolveDependencies): void;

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

use ArrayIterator;
use Countable;
use IteratorAggregate;
use PHPUnit\Event\Test\Failure;

final class FailureCollection implements Countable, IteratorAggregate
{
/**
* @psalm-var list<Failure>
*
* @var array<int, Failure>
*/
private array $failures;

public function __construct(Failure ...$failures)
{
$this->failures = $failures;
}

public function count(): int
{
return count($this->failures);
}

/**
* @return ArrayIterator<int, Failure>
*/
public function getIterator(): ArrayIterator
{
return new ArrayIterator($this->failures);
}
}
113 changes: 113 additions & 0 deletions src/Event/TestSuite/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Event\TestSuite;

final class Result
{
private int $count;

private FailureCollection $errors;

private FailureCollection $failures;

private FailureCollection $notImplemented;

private FailureCollection $risky;

private FailureCollection $skipped;

private FailureCollection $warnings;

private array $passed;

/**
* @psalm-var list<class-string>
*
* @var array<int, string>
*/
private array $passedClasses;

/**
* @psalm-param list<class-string> $passedClasses
*
* @param array<int, string> $passedClasses
*/
public function __construct(
int $count,
FailureCollection $errors,
FailureCollection $failures,
FailureCollection $notImplemented,
FailureCollection $risky,
FailureCollection $skipped,
FailureCollection $warnings,
array $passed,
array $passedClasses
) {
$this->count = $count;
$this->errors = $errors;
$this->failures = $failures;
$this->notImplemented = $notImplemented;
$this->risky = $risky;
$this->skipped = $skipped;
$this->warnings = $warnings;
$this->passed = $passed;
$this->passedClasses = $passedClasses;
}

public function count(): int
{
return $this->count;
}

public function errors(): FailureCollection
{
return $this->errors;
}

public function failures(): FailureCollection
{
return $this->failures;
}

public function notImplemented(): FailureCollection
{
return $this->notImplemented;
}

public function risky(): FailureCollection
{
return $this->risky;
}

public function skipped(): FailureCollection
{
return $this->skipped;
}

public function warnings(): FailureCollection
{
return $this->warnings;
}

public function passed(): array
{
return $this->passed;
}

/**
* @psalm-return list<class-string>
*
* @return array<int, string>
*/
public function passedClasses(): array
{
return $this->passedClasses;
}
}
33 changes: 31 additions & 2 deletions src/Event/TestSuite/RunFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,47 @@

use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;
use SebastianBergmann\CodeCoverage\CodeCoverage;

final class RunFinished implements Event
{
private Telemetry\Info $telemetryInfo;

public function __construct(Telemetry\Info $telemetryInfo)
{
private string $name;

private Result $result;

private ?CodeCoverage $codeCoverage;

public function __construct(
Telemetry\Info $telemetryInfo,
string $name,
Result $result,
?CodeCoverage $codeCoverage
) {
$this->telemetryInfo = $telemetryInfo;
$this->name = $name;
$this->result = $result;
$this->codeCoverage = $codeCoverage;
}

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

public function name(): string
{
return $this->name;
}

public function result(): Result
{
return $this->result;
}

public function codeCoverage(): ?CodeCoverage
{
return $this->codeCoverage;
}
}
44 changes: 44 additions & 0 deletions src/Event/TestSuite/TestResultMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Event;

use PHPUnit\Event\Test\Failure;
use PHPUnit\Event\TestSuite\FailureCollection;
use PHPUnit\Event\TestSuite\Result;
use PHPUnit\Framework\TestFailure;
use PHPUnit\Framework\TestResult;

final class TestResultMapper
{
public function map(TestResult $result): Result
{
return new Result(
$result->count(),
self::toFailureCollection(...$result->errors()),
self::toFailureCollection(...$result->failures()),
self::toFailureCollection(...$result->notImplemented()),
self::toFailureCollection(...$result->risky()),
self::toFailureCollection(...$result->skipped()),
self::toFailureCollection(...$result->warnings()),
$result->passed(),
$result->passedClasses()
);
}

private static function toFailureCollection(TestFailure ...$testFailures): FailureCollection
{
return new FailureCollection(...array_map(static function (TestFailure $testFailure): Failure {
return new Failure(
$testFailure->getTestName(),
$testFailure->thrownException()
);
}, $testFailures));
}
}
6 changes: 5 additions & 1 deletion src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ public function run(TestSuite $suite, array $arguments = [], array $warnings = [

$suite->run($result);

Event\Registry::emitter()->testSuiteRunFinished();
Event\Registry::emitter()->testSuiteRunFinished(
$suite->getName(),
$result,
CodeCoverage::isActive() ? CodeCoverage::instance() : null
);

foreach ($this->extensions as $extension) {
if ($extension instanceof AfterLastTestHook) {
Expand Down
4 changes: 3 additions & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPUnit\Event\Code;
use PHPUnit\Event\Emitter;
use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\TestResult;
use PHPUnit\Framework\TestSuite;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\GlobalState\Snapshot;

final class NullEmitter implements Emitter
Expand Down Expand Up @@ -189,7 +191,7 @@ public function testSuiteLoaded(TestSuite $testSuite): void
{
}

public function testSuiteRunFinished(): void
public function testSuiteRunFinished(string $name, TestResult $result, ?CodeCoverage $codeCoverage): void
{
}

Expand Down
25 changes: 23 additions & 2 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework;
use PHPUnit\TestFixture;
use RecordingSubscriber;
use SebastianBergmann\CodeCoverage;
use SebastianBergmann\GlobalState\Snapshot;
use stdClass;

Expand Down Expand Up @@ -1621,6 +1622,13 @@ public function notify(TestSuite\Loaded $event): void

public function testTestSuiteRunFinishedDispatchesTestSuiteRunFinishedEvent(): void
{
$name = 'foo';
$result = new Framework\TestResult();
$codeCoverage = new CodeCoverage\CodeCoverage(
$this->createMock(CodeCoverage\Driver\Driver::class),
new CodeCoverage\Filter()
);

$subscriber = new class extends RecordingSubscriber implements TestSuite\RunFinishedSubscriber {
public function notify(TestSuite\RunFinished $event): void
{
Expand All @@ -1641,10 +1649,23 @@ public function notify(TestSuite\RunFinished $event): void
$telemetrySystem
);

$emitter->testSuiteRunFinished();
$emitter->testSuiteRunFinished(
$name,
$result,
$codeCoverage
);

$this->assertSame(1, $subscriber->recordedEventCount());
$this->assertInstanceOf(TestSuite\RunFinished::class, $subscriber->lastRecordedEvent());

$event = $subscriber->lastRecordedEvent();

$this->assertInstanceOf(TestSuite\RunFinished::class, $event);
$this->assertSame($name, $event->name());

$testResultMapper = new TestResultMapper();

$this->assertEquals($testResultMapper->map($result), $event->result());
$this->assertSame($codeCoverage, $event->codeCoverage());
}

public function testTestSuiteSortedDispatchesTestSuiteSortedEvent(): void
Expand Down
Loading

0 comments on commit 647aa5a

Please sign in to comment.