Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 28, 2023
1 parent d493a2a commit e54cd00
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/_files/AnotherDummySubscriber.php
@@ -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\TestFixture;

use PHPUnit\Event\Subscriber;

interface AnotherDummySubscriber extends Subscriber
{
public function notify(DummyEvent $event): void;
}
132 changes: 132 additions & 0 deletions tests/unit/Event/TypeMapTest.php
@@ -0,0 +1,132 @@
<?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;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\AnotherDummySubscriber;
use PHPUnit\TestFixture\DummyEvent;
use PHPUnit\TestFixture\DummySubscriber;
use PHPUnit\TestFixture\MockObject\AnInterface;
use PHPUnit\TestFixture\MockObject\FinalClass;

#[CoversClass(TypeMap::class)]
#[Small]
final class TypeMapTest extends TestCase
{
public function testMapsKnownSubscriberInterfaceToEventClass(): void
{
$subscriber = $this->createStub(DummySubscriber::class);
$subscriberType = DummySubscriber::class;
$event = new DummyEvent;
$eventType = DummyEvent::class;

$map = new TypeMap;

$this->assertFalse($map->isKnownSubscriberType($subscriber));
$this->assertFalse($map->isKnownEventType($event));

$map->addMapping($subscriberType, $eventType);

$this->assertTrue($map->isKnownSubscriberType($subscriber));
$this->assertTrue($map->isKnownEventType($event));

$this->assertSame($eventType, $map->map($subscriber));
}

public function testCannotMapUnknownSubscriberInterface(): void
{
$subscriber = $this->createStub(DummySubscriber::class);

$map = new TypeMap;

$this->expectException(MapError::class);

$map->map($subscriber);
}

public function testCannotAddMappingFromUnknownSubscriberInterface(): void
{
$subscriberType = 'DoesNotExist';
$eventType = DummyEvent::class;

$map = new TypeMap;

$this->expectException(UnknownSubscriberException::class);

$map->addMapping($subscriberType, $eventType);
}

public function testCannotAddMappingFromInvalidSubscriberInterface(): void
{
$subscriberType = AnInterface::class;
$eventType = DummyEvent::class;

$map = new TypeMap;

$this->expectException(InvalidSubscriberException::class);

$map->addMapping($subscriberType, $eventType);
}

public function testCannotAddMappingToUnknownEventClass(): void
{
$subscriberType = DummySubscriber::class;
$eventType = 'DoesNotExist';

$map = new TypeMap;

$this->expectException(UnknownEventException::class);

$map->addMapping($subscriberType, $eventType);
}

public function testCannotAddMappingToInvalidEventClass(): void
{
$subscriberType = DummySubscriber::class;
$eventType = FinalClass::class;

$map = new TypeMap;

$this->expectException(InvalidEventException::class);

$map->addMapping($subscriberType, $eventType);
}

public function testSubscriberInterfaceCanOnlyBeRegisteredOnce(): void
{
$subscriberType = DummySubscriber::class;
$eventType = DummyEvent::class;

$map = new TypeMap;

$map->addMapping($subscriberType, $eventType);

$this->expectException(SubscriberTypeAlreadyRegisteredException::class);

$map->addMapping($subscriberType, $eventType);
}

public function testEventClassCanOnlyBeRegisteredOnce(): void
{
$subscriberType = DummySubscriber::class;
$anotherSubscriberType = AnotherDummySubscriber::class;
$eventType = DummyEvent::class;

$map = new TypeMap;

$map->addMapping($subscriberType, $eventType);

$this->expectException(EventAlreadyAssignedException::class);

$map->addMapping($anotherSubscriberType, $eventType);
}
}

0 comments on commit e54cd00

Please sign in to comment.