Skip to content

Commit

Permalink
Fix: Allow all event dispatchers (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Jun 12, 2022
1 parent 379a97c commit e0f9a57
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 19 deletions.
10 changes: 2 additions & 8 deletions src/Helper/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Unleash\Client\Helper;

use InvalidArgumentException;
use JetBrains\PhpStorm\ExpectedValues;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Unleash\Client\Event\UnleashEvents;
Expand All @@ -21,15 +19,11 @@
final class EventDispatcher implements EventDispatcherInterface
{
/**
* @param SymfonyEventDispatcher|null $eventDispatcher
* @noinspection PhpDocSignatureInspection
* @param EventDispatcherInterface|null $eventDispatcher
*/
public function __construct(
private readonly ?object $eventDispatcher,
private readonly ?EventDispatcherInterface $eventDispatcher,
) {
if ($this->eventDispatcher !== null && !$this->eventDispatcher instanceof SymfonyEventDispatcher) { // @phpstan-ignore-line
throw new InvalidArgumentException('The dispatcher must either be null or an instance of ' . SymfonyEventDispatcher::class);
}
}

public function addListener(string $eventName, callable $listener, int $priority = 0): void
Expand Down
5 changes: 3 additions & 2 deletions src/UnleashBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\SimpleCache\CacheInterface;
use SplFileInfo;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Traversable;
use Unleash\Client\Bootstrap\BootstrapHandler;
Expand Down Expand Up @@ -93,7 +94,7 @@ final class UnleashBuilder
private array $strategies;

/**
* @var SymfonyEventDispatcher|null
* @var EventDispatcherInterface|null
* @noinspection PhpDocFieldTypeMismatchInspection
*/
private ?object $eventDispatcher = null;
Expand Down Expand Up @@ -306,7 +307,7 @@ public function withFetchingEnabled(bool $enabled): self
}

#[Pure]
public function withEventDispatcher(?SymfonyEventDispatcher $eventDispatcher): self
public function withEventDispatcher(?EventDispatcherInterface $eventDispatcher): self
{
return $this->with('eventDispatcher', $eventDispatcher);
}
Expand Down
9 changes: 0 additions & 9 deletions tests/Helper/EventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Unleash\Client\Tests\Helper;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use stdClass;
use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyEventDispatcher;
Expand Down Expand Up @@ -97,12 +96,4 @@ public function onTest(stdClass $event): void
self::assertTrue($instance->hasListeners('test2'));
self::assertFalse($instance->hasListeners('test3'));
}

public function testInvalidClass()
{
$this->expectException(InvalidArgumentException::class);
// it's typehinted as any object due to event dispatcher being non-mandatory,
// but it shouldn't really accept any class
new EventDispatcher(new stdClass());
}
}
1 change: 1 addition & 0 deletions tests/UnleashBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public function testWithoutDefaultClients()
->withAppUrl('http://example.com')
->withInstanceId('test')
->withAppName('test')
->withAutomaticRegistrationEnabled(false)
;
$unleash = $instance->build();

Expand Down

0 comments on commit e0f9a57

Please sign in to comment.