Skip to content

Commit

Permalink
Enhancement: Emit Test\AfterLastTestMethodCalled event
Browse files Browse the repository at this point in the history
Fixes #45.

Co-authored-by: Arne Blankerts <Arne@Blankerts.de>
  • Loading branch information
localheinz and theseer committed Nov 6, 2020
1 parent a038771 commit 0af80ac
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@
<code>AbortedWithMessageSubscriber</code>
</UnusedClass>
</file>
<file src="src/Event/Test/AfterLastTestMethodCalled.php">
<PossiblyUnusedMethod occurrences="2">
<code>calledMethod</code>
<code>testClassName</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Event/Test/AfterLastTestMethodCalledSubscriber.php">
<UnusedClass occurrences="1">
<code>AfterLastTestMethodCalledSubscriber</code>
Expand Down
8 changes: 6 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,13 @@ public function testAfterTestMethodCalled(string $testClassName, Code\ClassMetho
));
}

public function testAfterLastTestMethodCalled(): void
public function testAfterLastTestMethodCalled(string $testClassName, Code\ClassMethod $calledMethod): void
{
$this->dispatcher->dispatch(new Test\AfterLastTestMethodCalled($this->telemetryInfo()));
$this->dispatcher->dispatch(new Test\AfterLastTestMethodCalled(
$this->telemetryInfo(),
$testClassName,
$calledMethod
));
}

public function testDoubleMockObjectCreated(string $className): void
Expand Down
5 changes: 4 additions & 1 deletion src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public function testPostConditionFinished(string $testClassName, Code\ClassMetho
*/
public function testAfterTestMethodCalled(string $testClassName, Code\ClassMethod $calledMethod): void;

public function testAfterLastTestMethodCalled(): void;
/**
* @psalm-param class-string $testClassName
*/
public function testAfterLastTestMethodCalled(string $testClassName, Code\ClassMethod $calledMethod): void;

/**
* @psalm-param class-string $className
Expand Down
38 changes: 36 additions & 2 deletions src/Event/Test/AfterLastTestMethodCalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace PHPUnit\Event\Test;

use PHPUnit\Event\Code;
use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;

Expand All @@ -19,13 +20,46 @@ final class AfterLastTestMethodCalled implements Event
*/
private $telemetryInfo;

public function __construct(Telemetry\Info $telemetryInfo)
{
/**
* @psalm-var class-string
*
* @var string
*/
private $testClassName;

/**
* @var Code\ClassMethod
*/
private $calledMethod;

/**
* @psalm-param class-string $testClassName
*/
public function __construct(
Telemetry\Info $telemetryInfo,
string $testClassName,
Code\ClassMethod $calledMethod
) {
$this->telemetryInfo = $telemetryInfo;
$this->testClassName = $testClassName;
$this->calledMethod = $calledMethod;
}

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

/**
* @psalm-return class-string
*/
public function testClassName(): string
{
return $this->testClassName;
}

public function calledMethod(): Code\ClassMethod
{
return $this->calledMethod;
}
}
8 changes: 8 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,14 @@ public function runBare(): void
if ($this->inIsolation) {
foreach ($hookMethods['afterClass'] as $method) {
$this->{$method}();

$emitter->testAfterLastTestMethodCalled(
static::class,
new Event\Code\ClassMethod(
static::class,
$method
)
);
}

$emitter->testAfterLastTestMethodFinished();
Expand Down
2 changes: 1 addition & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function testAfterTestMethodCalled(string $testClassName, Code\ClassMetho
{
}

public function testAfterLastTestMethodCalled(): void
public function testAfterLastTestMethodCalled(string $testClassName, Code\ClassMethod $calledMethod): void
{
}

Expand Down
21 changes: 18 additions & 3 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1214,8 +1214,14 @@ public function notify(Test\PreConditionFinished $event): void
self::assertSame($calledMethods, $event->calledMethods());
}

public function testAfterLastTestMethodCalledDispatchesAfterLastTestMethodCalledEvent(): void
public function testTestAfterLastTestMethodCalledDispatchesTestAfterLastTestMethodCalledEvent(): void
{
$testClassName = self::class;
$calledMethod = new Code\ClassMethod(...array_values(explode(
'::',
__METHOD__
)));

$subscriber = new class extends RecordingSubscriber implements Test\AfterLastTestMethodCalledSubscriber {
public function notify(Test\AfterLastTestMethodCalled $event): void
{
Expand All @@ -1236,10 +1242,19 @@ public function notify(Test\AfterLastTestMethodCalled $event): void
$telemetrySystem
);

$emitter->testAfterLastTestMethodCalled();
$emitter->testAfterLastTestMethodCalled(
$testClassName,
$calledMethod
);

self::assertSame(1, $subscriber->recordedEventCount());
self::assertInstanceOf(Test\AfterLastTestMethodCalled::class, $subscriber->lastRecordedEvent());

$event = $subscriber->lastRecordedEvent();

self::assertInstanceOf(Test\AfterLastTestMethodCalled::class, $event);

self::assertSame($testClassName, $event->testClassName());
self::assertSame($calledMethod, $event->calledMethod());
}

public function testTestDoubleMockObjectCreatedDispatchesTestDoubleMockObjectCreatedEvent(): void
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/Event/Test/AfterLastTestMethodCalledTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace PHPUnit\Event\Test;

use PHPUnit\Event\AbstractEventTestCase;
use PHPUnit\Event\Code;

/**
* @covers \PHPUnit\Event\Test\AfterLastTestMethodCalled
Expand All @@ -19,9 +20,20 @@ final class AfterLastTestMethodCalledTest extends AbstractEventTestCase
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$testClassName = self::class;
$calledMethod = new Code\ClassMethod(...array_values(explode(
'::',
__METHOD__
)));

$event = new AfterLastTestMethodCalled($telemetryInfo);
$event = new AfterLastTestMethodCalled(
$telemetryInfo,
$testClassName,
$calledMethod
);

self::assertSame($telemetryInfo, $event->telemetryInfo());
self::assertSame($testClassName, $event->testClassName());
self::assertSame($calledMethod, $event->calledMethod());
}
}

0 comments on commit 0af80ac

Please sign in to comment.