Skip to content

Commit

Permalink
Add AbstractProviderConfigurator, remove detach() (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiiliveext committed Apr 6, 2020
1 parent 47e4fdd commit 680194b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 32 deletions.
11 changes: 11 additions & 0 deletions src/Provider/AbstractProviderConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Yiisoft\EventDispatcher\Provider;

abstract class AbstractProviderConfigurator
{
protected function attach(callable $listener, string $eventClassName = ''): void
{
throw new \RuntimeException("Method 'attach' does not exist.");
}
}
14 changes: 2 additions & 12 deletions src/Provider/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* });
* ```
*/
final class Provider implements ListenerProviderInterface
final class Provider extends AbstractProviderConfigurator implements ListenerProviderInterface
{
/**
* @var callable[]
Expand Down Expand Up @@ -46,7 +46,7 @@ public function getListenersForEvent(object $event): iterable
* @param callable $listener
* @param string $eventClassName
*/
public function attach(callable $listener, string $eventClassName = ''): void
protected function attach(callable $listener, string $eventClassName = ''): void

This comment has been minimized.

Copy link
@lubobill1990

lubobill1990 May 2, 2020

Why make this attach function protected?
How can I call it if it's protected?

{
if ($eventClassName === '') {
$eventClassName = $this->getParameterType($listener);
Expand All @@ -55,16 +55,6 @@ public function attach(callable $listener, string $eventClassName = ''): void
$this->listeners[$eventClassName][] = $listener;
}

/**
* Detach all event handlers registered for an event
*
* @param string $eventClassName
*/
public function detach(string $eventClassName): void
{
unset($this->listeners[$eventClassName]);
}

/**
* Derives the interface type of the first argument of a callable.
*
Expand Down
54 changes: 34 additions & 20 deletions tests/Provider/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yiisoft\EventDispatcher\Tests\Provider;

use PHPUnit\Framework\TestCase;
use Yiisoft\EventDispatcher\Provider\AbstractProviderConfigurator;
use Yiisoft\EventDispatcher\Provider\Provider;
use Yiisoft\EventDispatcher\Tests\Event\ClassItself;
use Yiisoft\EventDispatcher\Tests\Event\Event;
Expand All @@ -18,7 +19,8 @@ final class ProviderTest extends TestCase
public function testAttachCallableArray(): void
{
$provider = new Provider();
$provider->attach([WithStaticMethod::class, 'handle']);
$providerConfigurator = $this->getProviderConfigurator($provider);
$providerConfigurator->attach([WithStaticMethod::class, 'handle']);

$listeners = $provider->getListenersForEvent(new Event());
$this->assertCount(1, $listeners);
Expand All @@ -27,7 +29,8 @@ public function testAttachCallableArray(): void
public function testAttachCallableFunction(): void
{
$provider = new Provider();
$provider->attach('Yiisoft\EventDispatcher\Tests\Provider\handle');
$providerConfigurator = $this->getProviderConfigurator($provider);
$providerConfigurator->attach('Yiisoft\EventDispatcher\Tests\Provider\handle');

$listeners = $provider->getListenersForEvent(new Event());
$this->assertCount(1, $listeners);
Expand All @@ -36,7 +39,8 @@ public function testAttachCallableFunction(): void
public function testAttachClosure(): void
{
$provider = new Provider();
$provider->attach(function (Event $event) {
$providerConfigurator = $this->getProviderConfigurator($provider);
$providerConfigurator->attach(function (Event $event) {
// do nothing
});

Expand All @@ -47,7 +51,8 @@ public function testAttachClosure(): void
public function testAttachCallableObject(): void
{
$provider = new Provider();
$provider->attach([new NonStatic(), 'handle']);
$providerConfigurator = $this->getProviderConfigurator($provider);
$providerConfigurator->attach([new NonStatic(), 'handle']);

$listeners = $provider->getListenersForEvent(new Event());
$this->assertCount(1, $listeners);
Expand All @@ -56,7 +61,8 @@ public function testAttachCallableObject(): void
public function testInvokable(): void
{
$provider = new Provider();
$provider->attach(new Invokable());
$providerConfigurator = $this->getProviderConfigurator($provider);
$providerConfigurator->attach(new Invokable());

$listeners = $provider->getListenersForEvent(new Event());
$this->assertCount(1, $listeners);
Expand All @@ -65,17 +71,18 @@ public function testInvokable(): void
public function testListenersForClassHierarchyAreReturned(): void
{
$provider = new Provider();
$providerConfigurator = $this->getProviderConfigurator($provider);

$provider->attach(function (ParentInterface $parentInterface) {
$providerConfigurator->attach(function (ParentInterface $parentInterface) {
$parentInterface->register('parent interface');
});
$provider->attach(function (ParentClass $parentClass) {
$providerConfigurator->attach(function (ParentClass $parentClass) {
$parentClass->register('parent class');
});
$provider->attach(function (ClassInterface $classInterface) {
$providerConfigurator->attach(function (ClassInterface $classInterface) {
$classInterface->register('class interface');
});
$provider->attach(function (ClassItself $classItself) {
$providerConfigurator->attach(function (ClassItself $classItself) {
$classItself->register('class itself');
});

Expand All @@ -102,20 +109,22 @@ public function testListenersForClassHierarchyAreReturned(): void
public function testListenerWithNoParameterThrowsException(): void
{
$provider = new Provider();
$providerConfigurator = $this->getProviderConfigurator($provider);

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Listeners must declare an object type they can accept.');

$provider->attach(fn () => null);
$providerConfigurator->attach(fn () => null);
}

public function testListenerForEventIsReturned(): void
{
$provider = new Provider();
$providerConfigurator = $this->getProviderConfigurator($provider);

$listener = fn () => null;

$provider->attach($listener, Event::class);
$providerConfigurator->attach($listener, Event::class);

$listeners = $provider->getListenersForEvent(new Event());

Expand All @@ -124,16 +133,21 @@ public function testListenerForEventIsReturned(): void
$this->assertContains($listener, $listeners);
}

public function testDetachListenersForEventAreDetached(): void
private function getProviderConfigurator(Provider $provider)
{
$provider = new Provider();

$provider->attach(fn () => null, Event::class);
$provider->detach(Event::class);

$listeners = $provider->getListenersForEvent(new Event());

$this->assertCount(0, $listeners);
return new class($provider) extends AbstractProviderConfigurator {
private Provider $provider;

public function __construct(Provider $provider)
{
$this->provider = $provider;
}

public function attach(callable $listener, string $eventClassName = ''): void
{
$this->provider->attach($listener, $eventClassName);
}
};
}
}

Expand Down

0 comments on commit 680194b

Please sign in to comment.