From 680194bad114f1516d3ca9f8ea52807992ccc507 Mon Sep 17 00:00:00 2001 From: yiiliveext Date: Mon, 6 Apr 2020 16:28:38 +0300 Subject: [PATCH] Add AbstractProviderConfigurator, remove detach() (#21) --- src/Provider/AbstractProviderConfigurator.php | 11 ++++ src/Provider/Provider.php | 14 +---- tests/Provider/ProviderTest.php | 54 ++++++++++++------- 3 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 src/Provider/AbstractProviderConfigurator.php diff --git a/src/Provider/AbstractProviderConfigurator.php b/src/Provider/AbstractProviderConfigurator.php new file mode 100644 index 0000000..200e8cd --- /dev/null +++ b/src/Provider/AbstractProviderConfigurator.php @@ -0,0 +1,11 @@ +getParameterType($listener); @@ -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. * diff --git a/tests/Provider/ProviderTest.php b/tests/Provider/ProviderTest.php index b5b5b27..1089023 100644 --- a/tests/Provider/ProviderTest.php +++ b/tests/Provider/ProviderTest.php @@ -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; @@ -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); @@ -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); @@ -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 }); @@ -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); @@ -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); @@ -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'); }); @@ -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()); @@ -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); + } + }; } }