Skip to content

Commit

Permalink
Enhancement: Emit TestCase\BeforeClass event
Browse files Browse the repository at this point in the history
Related to #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 May 21, 2021
1 parent 950776d commit dd1d176
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ public function testCaseAfterClassFinished(): void
$this->dispatcher->dispatch(new TestCase\AfterClassFinished($this->telemetryInfo()));
}

public function testCaseBeforeClassCalled(): void
{
$this->dispatcher->dispatch(new TestCase\BeforeClassCalled($this->telemetryInfo()));
}

public function testCaseBeforeClassFinished(): void
{
$this->dispatcher->dispatch(new TestCase\BeforeClassFinished($this->telemetryInfo()));
Expand Down
2 changes: 2 additions & 0 deletions src/Event/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function testTearDownFinished(): void;

public function testCaseAfterClassFinished(): void;

public function testCaseBeforeClassCalled(): void;

public function testCaseBeforeClassFinished(): void;

public function testCaseSetUpBeforeClassFinished(): 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 @@ -77,6 +77,7 @@ private static function registerDefaultTypes(): void
Test\SetUpFinished::class,
Test\TearDownFinished::class,
TestCase\AfterClassFinished::class,
TestCase\BeforeClassCalled::class,
TestCase\BeforeClassFinished::class,
TestCase\SetUpBeforeClassFinished::class,
TestCase\SetUpFinished::class,
Expand Down
28 changes: 28 additions & 0 deletions src/Event/TestCase/BeforeClassCalled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\TestCase;

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

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

public function __construct(Telemetry\Info $telemetryInfo)
{
$this->telemetryInfo = $telemetryInfo;
}

public function telemetryInfo(): Telemetry\Info
{
return $this->telemetryInfo;
}
}
17 changes: 17 additions & 0 deletions src/Event/TestCase/BeforeClassCalledSubscriber.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\TestCase;

use PHPUnit\Event\Subscriber;

interface BeforeClassCalledSubscriber extends Subscriber
{
public function notify(BeforeClassCalled $event): void;
}
2 changes: 2 additions & 0 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,8 @@ public function runBare(): void
foreach ($hookMethods['beforeClass'] as $method) {
$this->{$method}();
}

Event\Registry::emitter()->testCaseBeforeClassCalled();
}

if (method_exists(static::class, $this->name) &&
Expand Down
4 changes: 4 additions & 0 deletions tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public function testCaseAfterClassFinished(): void
{
}

public function testCaseBeforeClassCalled(): void
{
}

public function testCaseBeforeClassFinished(): void
{
}
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,34 @@ public function notify(TestCase\AfterClassFinished $event): void
$this->assertInstanceOf(TestCase\AfterClassFinished::class, $subscriber->lastRecordedEvent());
}

public function testTestCaseBeforeClassCalledDispatchesTestCaseBeforeClassCalledEvent(): void
{
$subscriber = new class extends RecordingSubscriber implements TestCase\BeforeClassCalledSubscriber {
public function notify(TestCase\BeforeClassCalled $event): void
{
$this->record($event);
}
};

$dispatcher = self::createDispatcherWithRegisteredSubscriber(
TestCase\BeforeClassCalledSubscriber::class,
TestCase\BeforeClassCalled::class,
$subscriber
);

$telemetrySystem = self::createTelemetrySystem();

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

$emitter->testCaseBeforeClassCalled();

$this->assertSame(1, $subscriber->recordedEventCount());
$this->assertInstanceOf(TestCase\BeforeClassCalled::class, $subscriber->lastRecordedEvent());
}

public function testTestCaseBeforeClassFinishedDispatchesTestCaseBeforeClassFinishedEvent(): void
{
$subscriber = new class extends RecordingSubscriber implements TestCase\BeforeClassFinishedSubscriber {
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/Event/TestCase/BeforeClassCalledTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\TestCase;

use PHPUnit\Event\AbstractEventTestCase;

/**
* @covers \PHPUnit\Event\TestCase\BeforeClassCalled
*/
final class BeforeClassCalledTest extends AbstractEventTestCase
{
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();

$event = new BeforeClassCalled($telemetryInfo);

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

0 comments on commit dd1d176

Please sign in to comment.