diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2021-02-06 13:01:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-06 13:01:23 +0100 |
commit | cd77b1f2383b0296f7a631353d39178813f18ce2 (patch) | |
tree | 1d68380805e352481919a19adfe187a6f4fff26d | |
parent | 6945bd8cc388a7a697ef0342d406423f2e545a78 (diff) | |
parent | f6ddda8dadeed923a213f66d96d4e9f0a7d6b9fa (diff) | |
download | nextcloud-server-cd77b1f2383b0296f7a631353d39178813f18ce2.tar.gz nextcloud-server-cd77b1f2383b0296f7a631353d39178813f18ce2.zip |
Merge pull request #25499 from nextcloud/test/symfony-adapter
Test the symfony adapter / fix missing return object
-rw-r--r-- | lib/private/EventDispatcher/EventDispatcher.php | 3 | ||||
-rw-r--r-- | lib/private/EventDispatcher/SymfonyAdapter.php | 3 | ||||
-rw-r--r-- | tests/lib/EventDispatcher/SymfonyAdapterTest.php | 155 |
3 files changed, 160 insertions, 1 deletions
diff --git a/lib/private/EventDispatcher/EventDispatcher.php b/lib/private/EventDispatcher/EventDispatcher.php index 8fe4bcbb942..4e1fba67663 100644 --- a/lib/private/EventDispatcher/EventDispatcher.php +++ b/lib/private/EventDispatcher/EventDispatcher.php @@ -81,6 +81,9 @@ class EventDispatcher implements IEventDispatcher { $this->addListener($eventName, $listener, $priority); } + /** + * @deprecated + */ public function dispatch(string $eventName, Event $event): void { $this->dispatcher->dispatch($event, $eventName); diff --git a/lib/private/EventDispatcher/SymfonyAdapter.php b/lib/private/EventDispatcher/SymfonyAdapter.php index b1e5aa86049..3bd74e719fb 100644 --- a/lib/private/EventDispatcher/SymfonyAdapter.php +++ b/lib/private/EventDispatcher/SymfonyAdapter.php @@ -87,7 +87,8 @@ class SymfonyAdapter implements EventDispatcherInterface { // Event with no payload (object) need special handling if ($newEvent === null) { - return $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName); + $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName); + return new Event(); } // Flip the argument order for Symfony to prevent a trigger_error diff --git a/tests/lib/EventDispatcher/SymfonyAdapterTest.php b/tests/lib/EventDispatcher/SymfonyAdapterTest.php new file mode 100644 index 00000000000..b1d43bf2661 --- /dev/null +++ b/tests/lib/EventDispatcher/SymfonyAdapterTest.php @@ -0,0 +1,155 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace lib\EventDispatcher; + +use OC\EventDispatcher\EventDispatcher; +use OC\EventDispatcher\GenericEventWrapper; +use OC\EventDispatcher\SymfonyAdapter; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\GenericEvent; +use OCP\ILogger; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\EventDispatcher\Event as SymfonyEvent; +use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent as SymfonyGenericEvent; +use Test\TestCase; + +class SymfonyAdapterTest extends TestCase { + + /** @var EventDispatcher|MockObject */ + private $eventDispatcher; + + /** @var ILogger|MockObject */ + private $logger; + + /** @var EventDispatcherInterface */ + private $adapter; + + protected function setUp(): void { + parent::setUp(); + + $this->eventDispatcher = $this->createMock(EventDispatcher::class); + $this->logger = $this->createMock(ILogger::class); + + $this->adapter = new SymfonyAdapter( + $this->eventDispatcher, + $this->logger + ); + } + + public function testDispatchTypedEvent(): void { + $event = new Event(); + $eventName = 'symfony'; + $this->eventDispatcher->expects(self::once()) + ->method('dispatch') + ->with( + $eventName, + $event + ) + ->willReturnArgument(0); + + $this->adapter->dispatch($eventName, $event); + } + + public function testDispatchSymfonyGenericEvent(): void { + $eventName = 'symfony'; + $event = new SymfonyGenericEvent(); + $wrapped = new GenericEventWrapper( + $this->logger, + $eventName, + $event + ); + $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class); + $this->eventDispatcher->expects(self::once()) + ->method('getSymfonyDispatcher') + ->willReturn($symfonyDispatcher); + $symfonyDispatcher->expects(self::once()) + ->method('dispatch') + ->with( + self::equalTo($wrapped), + $eventName + ) + ->willReturnArgument(0); + + $result = $this->adapter->dispatch($eventName, $event); + + self::assertEquals($result, $wrapped); + } + + public function testDispatchOldSymfonyEvent(): void { + $event = new SymfonyEvent(); + $eventName = 'symfony'; + $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class); + $this->eventDispatcher->expects(self::once()) + ->method('getSymfonyDispatcher') + ->willReturn($symfonyDispatcher); + $symfonyDispatcher->expects(self::once()) + ->method('dispatch') + ->with( + $event, + $eventName + ) + ->willReturnArgument(0); + + $result = $this->adapter->dispatch($eventName, $event); + + self::assertSame($result, $event); + } + + public function testDispatchCustomGenericEvent(): void { + $event = new GenericEvent(); + $eventName = 'symfony'; + $this->eventDispatcher->expects(self::once()) + ->method('dispatch') + ->with( + $eventName, + $event + ); + + $result = $this->adapter->dispatch($eventName, $event); + + self::assertSame($result, $event); + } + + public function testDispatchEventWithoutPayload(): void { + $eventName = 'symfony'; + $symfonyDispatcher = $this->createMock(SymfonyDispatcher::class); + $this->eventDispatcher->expects(self::once()) + ->method('getSymfonyDispatcher') + ->willReturn($symfonyDispatcher); + $symfonyDispatcher->expects(self::once()) + ->method('dispatch') + ->with( + $eventName + ) + ->willReturnArgument(0); + + $result = $this->adapter->dispatch($eventName); + + self::assertNotNull($result); + } +} |