Skip to content

Commit

Permalink
Enhancement: Emit Test\PreConditionFinished event
Browse files Browse the repository at this point in the history
Fixes #42.
  • Loading branch information
localheinz committed Oct 15, 2020
1 parent 0471448 commit f17ce58
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 19 deletions.
17 changes: 15 additions & 2 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
</PossiblyUnusedMethod>
</file>
<file src="src/Event/DispatchingEmitter.php">
<MissingThrowsDocblock occurrences="40">
<MissingThrowsDocblock occurrences="41">
<code>dispatch</code>
<code>dispatch</code>
<code>dispatch</code>
<code>dispatch</code>
Expand Down Expand Up @@ -128,7 +129,7 @@
</MissingThrowsDocblock>
</file>
<file src="src/Event/Emitter.php">
<PossiblyUnusedMethod occurrences="17">
<PossiblyUnusedMethod occurrences="18">
<code>extensionLoaded</code>
<code>testAfterLastTestMethodCalled</code>
<code>testAfterLastTestMethodFinished</code>
Expand All @@ -138,6 +139,7 @@
<code>testFinished</code>
<code>testPassed</code>
<code>testPassedButRisky</code>
<code>testPreConditionFinished</code>
<code>testPrepared</code>
<code>testRunConfigured</code>
<code>testSkippedByDataProvider</code>
Expand Down Expand Up @@ -317,6 +319,17 @@
<code>PreConditionCalledSubscriber</code>
</UnusedClass>
</file>
<file src="src/Event/Test/PreConditionFinished.php">
<PossiblyUnusedMethod occurrences="2">
<code>calledMethods</code>
<code>testClassName</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Event/Test/PreConditionFinishedSubscriber.php">
<UnusedClass occurrences="1">
<code>PreConditionFinishedSubscriber</code>
</UnusedClass>
</file>
<file src="src/Event/Test/PreparedSubscriber.php">
<UnusedClass occurrences="1">
<code>PreparedSubscriber</code>
Expand Down
9 changes: 9 additions & 0 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,15 @@ public function testPreConditionCalled(string $testClassName, CodeUnit\ClassMeth
));
}

public function testPreConditionFinished(string $testClassName, CodeUnit\ClassMethodUnit ...$calledMethods): void
{
$this->dispatcher->dispatch(new Test\PreConditionFinished(
$this->telemetryInfo(),
$testClassName,
...$calledMethods
));
}

public function testAfterLastTestMethodCalled(): void
{
$this->dispatcher->dispatch(new Test\AfterLastTestMethodCalled($this->telemetryInfo()));
Expand Down
5 changes: 5 additions & 0 deletions src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public function testBeforeTestMethodFinished(string $testClassName, CodeUnit\Cla
*/
public function testPreConditionCalled(string $testClassName, CodeUnit\ClassMethodUnit $calledMethod): void;

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

public function testAfterLastTestMethodCalled(): void;

/**
Expand Down
1 change: 1 addition & 0 deletions src/Event/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private static function registerDefaultTypes(TypeMap $typeMap): void
Test\Passed::class,
Test\PassedButRisky::class,
Test\PreConditionCalled::class,
Test\PreConditionFinished::class,
Test\Prepared::class,
Test\RunConfigured::class,
Test\SetUpFinished::class,
Expand Down
68 changes: 68 additions & 0 deletions src/Event/Test/PreConditionFinished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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\Test;

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

final class PreConditionFinished implements Event
{
/**
* @var Telemetry\Info
*/
private $telemetryInfo;

/**
* @psalm-var class-string
*
* @var string
*/
private $testClassName;

/**
* @var array<CodeUnit\ClassMethodUnit>
*/
private $calledMethods;

/**
* @psalm-param class-string $testClassName
*/
public function __construct(
Telemetry\Info $telemetryInfo,
string $testClassName,
CodeUnit\ClassMethodUnit ...$calledMethods
) {
$this->telemetryInfo = $telemetryInfo;
$this->testClassName = $testClassName;
$this->calledMethods = $calledMethods;
}

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

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

/**
* @return array<CodeUnit\ClassMethodUnit>
*/
public function calledMethods(): array
{
return $this->calledMethods;
}
}
17 changes: 17 additions & 0 deletions src/Event/Test/PreConditionFinishedSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Test;

use PHPUnit\Event\Subscriber;

interface PreConditionFinishedSubscriber extends Subscriber
{
public function notify(PreConditionFinished $event): void;
}
19 changes: 15 additions & 4 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1179,18 +1179,29 @@ public function runBare(): void

Event\Registry::emitter()->testSetUpFinished();

$methodsCalledPreCondition = [];

foreach ($hookMethods['preCondition'] as $method) {
$this->{$method}();

$methodCalledPreCondition = CodeUnit\ClassMethodUnit::forClassMethod(
static::class,
$method
);

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

$methodsCalledPreCondition[] = $methodCalledPreCondition;
}

Event\Registry::emitter()->testBeforeTestMethodFinished(
static::class,
...$methodsCalledPreCondition
);

$this->assertPreConditions();
$this->testResult = $this->runTest();
$this->verifyMockObjects();
Expand Down
4 changes: 4 additions & 0 deletions tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function testPreConditionCalled(string $testClassName, CodeUnit\ClassMeth
{
}

public function testPreConditionFinished(string $testClassName, CodeUnit\ClassMethodUnit ...$calledMethods): void
{
}

public function testAfterLastTestMethodCalled(): void
{
}
Expand Down
71 changes: 58 additions & 13 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,49 @@ public function notify(Test\BeforeTestMethodCalled $event): void
self::assertSame($calledMethod, $event->calledMethod());
}

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

$subscriber = new class extends RecordingSubscriber implements Test\PreConditionCalledSubscriber {
public function notify(Test\PreConditionCalled $event): void
{
$this->record($event);
}
};

$dispatcher = self::createDispatcherWithRegisteredSubscriber(
Test\PreConditionCalledSubscriber::class,
Test\PreConditionCalled::class,
$subscriber
);

$telemetrySystem = self::createTelemetrySystem();

$emitter = new DispatchingEmitter(
$dispatcher,
$telemetrySystem
);

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

self::assertSame(1, $subscriber->recordedEventCount());

$event = $subscriber->lastRecordedEvent();

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

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

public function testTestBeforeTestMethodFinishedDispatchesTestBeforeTestMethodFinishedEvent(): void
{
$testClassName = self::class;
Expand Down Expand Up @@ -905,24 +948,26 @@ public function notify(Test\BeforeTestMethodFinished $event): void
self::assertSame($calledMethods, $event->calledMethods());
}

public function testTestPreConditionCalledDispatchesTestBeforeTestMethodEvent(): void
public function testTestPreConditionFinishedDispatchesTestPreConditionFinishedEvent(): void
{
$testClassName = self::class;
$calledMethod = CodeUnit\ClassMethodUnit::forClassMethod(...array_values(explode(
'::',
__METHOD__
)));
$calledMethods = array_map(static function (string $methodName): CodeUnit\ClassMethodUnit {
return CodeUnit\ClassMethodUnit::forClassMethod(
self::class,
$methodName
);
}, get_class_methods($this));

$subscriber = new class extends RecordingSubscriber implements Test\PreConditionCalledSubscriber {
public function notify(Test\PreConditionCalled $event): void
$subscriber = new class extends RecordingSubscriber implements Test\PreConditionFinishedSubscriber {
public function notify(Test\PreConditionFinished $event): void
{
$this->record($event);
}
};

$dispatcher = self::createDispatcherWithRegisteredSubscriber(
Test\PreConditionCalledSubscriber::class,
Test\PreConditionCalled::class,
Test\PreConditionFinishedSubscriber::class,
Test\PreConditionFinished::class,
$subscriber
);

Expand All @@ -933,19 +978,19 @@ public function notify(Test\PreConditionCalled $event): void
$telemetrySystem
);

$emitter->testPreConditionCalled(
$emitter->testPreConditionFinished(
$testClassName,
$calledMethod
...$calledMethods
);

self::assertSame(1, $subscriber->recordedEventCount());

$event = $subscriber->lastRecordedEvent();

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

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

public function testAfterLastTestMethodCalledDispatchesAfterLastTestMethodCalledEvent(): void
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/Event/Test/PreConditionFinishedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\Test;

use PHPUnit\Event\AbstractEventTestCase;
use SebastianBergmann\CodeUnit;

/**
* @covers \PHPUnit\Event\Test\PreConditionFinished
*/
final class PreConditionFinishedTest extends AbstractEventTestCase
{
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$testClassName = self::class;
$calledMethods = array_map(static function (string $methodName): CodeUnit\ClassMethodUnit {
return CodeUnit\ClassMethodUnit::forClassMethod(
self::class,
$methodName
);
}, get_class_methods($this));

$event = new PreConditionFinished(
$telemetryInfo,
$testClassName,
...$calledMethods
);

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

0 comments on commit f17ce58

Please sign in to comment.