Skip to content

Commit

Permalink
Enhancement: Implement and emit TestDouble\TestStubCreated event
Browse files Browse the repository at this point in the history
Fixes #51.

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 97700f8 commit ac468f2
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
</UndefinedInterfaceMethod>
</file>
<file src="src/Event/DispatchingEmitter.php">
<MissingThrowsDocblock occurrences="42">
<MissingThrowsDocblock occurrences="43">
<code>dispatch</code>
<code>dispatch</code>
<code>dispatch</code>
<code>dispatch</code>
Expand Down
11 changes: 11 additions & 0 deletions src/Event/DispatchingEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,17 @@ public function testDoubleTestProxyCreated(string $className, array $constructor
));
}

/**
* @psalm-param class-string $className
*/
public function testDoubleTestStubCreated(string $className): void
{
$this->dispatcher->dispatch(new TestDouble\TestStubCreated(
$this->telemetryInfo(),
$className
));
}

public function testSuiteAfterClassFinished(): void
{
$this->dispatcher->dispatch(new TestSuite\AfterClassFinished($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 @@ -124,6 +124,11 @@ public function testDoublePartialMockObjectCreated(string $className, string ...
*/
public function testDoubleTestProxyCreated(string $className, array $constructorArguments): void;

/**
* @psalm-param class-string $className
*/
public function testDoubleTestStubCreated(string $className): void;

public function testSuiteAfterClassFinished(): void;

public function testSuiteLoaded(TestSuite $testSuite): 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 @@ -92,6 +92,7 @@ private static function registerDefaultTypes(TypeMap $typeMap): void
TestDouble\MockObjectCreatedForTrait::class,
TestDouble\PartialMockObjectCreated::class,
TestDouble\TestProxyCreated::class,
TestDouble\TestStubCreated::class,
TestSuite\AfterClassFinished::class,
TestSuite\Loaded::class,
TestSuite\RunFinished::class,
Expand Down
45 changes: 45 additions & 0 deletions src/Event/TestDouble/TestStubCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\TestDouble;

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

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

/**
* @var class-string
*/
private string $className;

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

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

/**
* @return class-string
*/
public function className(): string
{
return $this->className;
}
}
17 changes: 17 additions & 0 deletions src/Event/TestDouble/TestStubCreatedSubscriber.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\TestDouble;

use PHPUnit\Event\Subscriber;

interface TestStubCreatedSubscriber extends Subscriber
{
public function notify(TestStubCreated $event): void;
}
6 changes: 5 additions & 1 deletion src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,11 @@ protected function setLocale(...$args): void
*/
protected function createStub(string $originalClassName): Stub
{
return $this->createMockObject($originalClassName);
$stub = $this->createMockObject($originalClassName);

Event\Registry::emitter()->testDoubleTestStubCreated($originalClassName);

return $stub;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/_files/NullEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function testDoubleTestProxyCreated(string $className, array $constructor
{
}

public function testDoubleTestStubCreated(string $className): void
{
}

public function testSuiteAfterClassFinished(): void
{
}
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/Event/DispatchingEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,41 @@ public function notify(TestDouble\TestProxyCreated $event): void
$this->assertSame($constructorArguments, $event->constructorArguments());
}

public function testTestDoubleTestStubCreatedDispatchesTestDoubleTestStubCreatedEvent(): void
{
$className = self::class;

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

$dispatcher = self::createDispatcherWithRegisteredSubscriber(
TestDouble\TestStubCreatedSubscriber::class,
TestDouble\TestStubCreated::class,
$subscriber
);

$telemetrySystem = self::createTelemetrySystem();

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

$emitter->testDoubleTestStubCreated($className);

$this->assertSame(1, $subscriber->recordedEventCount());

$event = $subscriber->lastRecordedEvent();

$this->assertInstanceOf(TestDouble\TestStubCreated::class, $event);

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

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

use PHPUnit\Event\AbstractEventTestCase;

/**
* @covers \PHPUnit\Event\TestDouble\TestStubCreated
*/
final class TestStubCreatedTest extends AbstractEventTestCase
{
public function testConstructorSetsValues(): void
{
$telemetryInfo = self::createTelemetryInfo();
$className = self::class;

$event = new TestStubCreated(
$telemetryInfo,
$className
);

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

0 comments on commit ac468f2

Please sign in to comment.