Skip to content

Commit

Permalink
Enhancement: Compose data into Test\BeforeFirstTestMethodCalled event
Browse files Browse the repository at this point in the history
Fixes #44.

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 7, 2021
1 parent 938cabb commit aae9237
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

final class DispatchingEmitter implements Emitter
Expand Down Expand Up @@ -177,9 +178,13 @@ public function testAfterLastTestMethodFinished(): void
$this->dispatcher->dispatch(new Test\AfterLastTestMethodFinished($this->telemetryInfo()));
}

public function testBeforeFirstTestMethodCalled(): void
public function testBeforeFirstTestMethodCalled(string $testClassName, CodeUnit\ClassMethodUnit $calledMethod): void
{
$this->dispatcher->dispatch(new Test\BeforeFirstTestMethodCalled($this->telemetryInfo()));
$this->dispatcher->dispatch(new Test\BeforeFirstTestMethodCalled(
$this->telemetryInfo(),
$testClassName,
$calledMethod
));
}

public function testBeforeFirstTestMethodFinished(): void
Expand Down
6 changes: 5 additions & 1 deletion src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\TestSuite;
use SebastianBergmann\CodeUnit;
use SebastianBergmann\GlobalState\Snapshot;

interface Emitter
Expand Down Expand Up @@ -67,7 +68,10 @@ public function testAfterTestMethodFinished(): void;

public function testAfterLastTestMethodFinished(): void;

public function testBeforeFirstTestMethodCalled(): void;
/**
* @psalm-param class-string $testClassName
*/
public function testBeforeFirstTestMethodCalled(string $testClassName, CodeUnit\ClassMethodUnit $calledMethod): void;

public function testBeforeFirstTestMethodFinished(): void;

Expand Down
34 changes: 32 additions & 2 deletions src/Event/Test/BeforeFirstTestMethodCalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,48 @@

use PHPUnit\Event\Event;
use PHPUnit\Event\Telemetry;
use SebastianBergmann\CodeUnit;

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

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

private CodeUnit\ClassMethodUnit $calledMethod;

/**
* @psalm-param class-string $testClassName
* @psalm-param class-string $className
*/
public function __construct(
Telemetry\Info $telemetryInfo,
string $testClassName,
CodeUnit\ClassMethodUnit $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(): CodeUnit\ClassMethodUnit
{
return $this->calledMethod;
}
}
11 changes: 9 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
use PHPUnit\Util\Type;
use ReflectionClass;
use ReflectionException;
use SebastianBergmann\CodeUnit;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use SebastianBergmann\Diff\Differ;
Expand Down Expand Up @@ -818,9 +819,15 @@ public function runBare(): void
if ($this->inIsolation) {
foreach ($hookMethods['beforeClass'] as $method) {
$this->{$method}();
}

Event\Registry::emitter()->testBeforeFirstTestMethodCalled();
Event\Registry::emitter()->testBeforeFirstTestMethodCalled(
static::class,
CodeUnit\ClassMethodUnit::forClassMethod(
static::class,
$method
)
);
}
}

$this->setDoesNotPerformAssertionsFromAnnotation();
Expand Down
3 changes: 2 additions & 1 deletion tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\TestSuite;
use SebastianBergmann\CodeUnit;
use SebastianBergmann\GlobalState\Snapshot;

final class NullEmitter implements \PHPUnit\Event\Emitter
Expand Down Expand Up @@ -105,7 +106,7 @@ public function testAfterLastTestMethodFinished(): void
{
}

public function testBeforeFirstTestMethodCalled(): void
public function testBeforeFirstTestMethodCalled(string $testClassName, CodeUnit\ClassMethodUnit $calledMethod): void
{
}

Expand Down
20 changes: 18 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\CodeUnit;
use SebastianBergmann\GlobalState\Snapshot;
use stdClass;

Expand Down Expand Up @@ -730,6 +731,12 @@ public function notify(Test\AfterLastTestMethodFinished $event): void

public function testTestBeforeFirstTestMethodCalledDispatchesTestBeforeFirstTestMethodEvent(): void
{
$testClassName = self::class;
$calledMethod = CodeUnit\ClassMethodUnit::forClassMethod(...array_values(explode(
'::',
__METHOD__
)));

$subscriber = new class extends RecordingSubscriber implements Test\BeforeFirstTestMethodCalledSubscriber {
public function notify(Test\BeforeFirstTestMethodCalled $event): void
{
Expand All @@ -750,10 +757,19 @@ public function notify(Test\BeforeFirstTestMethodCalled $event): void
$telemetrySystem
);

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

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

$event = $subscriber->lastRecordedEvent();

$this->assertInstanceOf(Test\BeforeFirstTestMethodCalled::class, $event);

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

public function testTestBeforeFirstTestMethodFinishedDispatchesTestBeforeFirstTestMethodFinishedEvent(): void
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/Event/Test/BeforeFirstTestMethodCalledTest.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 SebastianBergmann\CodeUnit;

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

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

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

0 comments on commit aae9237

Please sign in to comment.