From 70c7781aa8a1737b4c7ca8e935796b1ebc3d9f34 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 14 Oct 2016 15:36:05 +0200 Subject: [PATCH] apply some polish Signed-off-by: Arthur Schiwon --- apps/comments/lib/EventHandler.php | 47 ++++++------------- apps/comments/lib/Notification/Listener.php | 6 --- apps/comments/tests/Unit/EventHandlerTest.php | 38 +++++---------- .../tests/Unit/Notification/ListenerTest.php | 23 --------- 4 files changed, 28 insertions(+), 86 deletions(-) diff --git a/apps/comments/lib/EventHandler.php b/apps/comments/lib/EventHandler.php index a5f312617e5..8c20c698371 100644 --- a/apps/comments/lib/EventHandler.php +++ b/apps/comments/lib/EventHandler.php @@ -36,12 +36,15 @@ use OCP\Comments\ICommentsEventHandler; * @package OCA\Comments */ class EventHandler implements ICommentsEventHandler { + /** @var ActivityListener */ + private $activityListener; - /** @var Application */ - protected $app; + /** @var NotificationListener */ + private $notificationListener; - public function __construct(Application $app) { - $this->app = $app; + public function __construct(ActivityListener $activityListener, NotificationListener $notificationListener) { + $this->activityListener = $activityListener; + $this->notificationListener = $notificationListener; } /** @@ -55,30 +58,18 @@ class EventHandler implements ICommentsEventHandler { $eventType = $event->getEvent(); if( $eventType === CommentsEvent::EVENT_ADD - && $event instanceof CommentsEvent ) { $this->notificationHandler($event); $this->activityHandler($event); return; } - if( $eventType === CommentsEvent::EVENT_PRE_UPDATE - && $event instanceof CommentsEvent - ) { - $this->notificationHandler($event); - return; - } - - if( $eventType === CommentsEvent::EVENT_UPDATE - && $event instanceof CommentsEvent - ) { - $this->notificationHandler($event); - return; - } - - if( $eventType === CommentsEvent::EVENT_DELETE - && $event instanceof CommentsEvent - ) { + $applicableEvents = [ + CommentsEvent::EVENT_PRE_UPDATE, + CommentsEvent::EVENT_UPDATE, + CommentsEvent::EVENT_DELETE, + ]; + if(in_array($eventType, $applicableEvents)) { $this->notificationHandler($event); return; } @@ -88,21 +79,13 @@ class EventHandler implements ICommentsEventHandler { * @param CommentsEvent $event */ private function activityHandler(CommentsEvent $event) { - $c = $this->app->getContainer(); - - /** @var ActivityListener $listener */ - $activityListener = $c->query(ActivityListener::class); - $activityListener->commentEvent($event); + $this->activityListener->commentEvent($event); } /** * @param CommentsEvent $event */ private function notificationHandler(CommentsEvent $event) { - $c = $this->app->getContainer(); - - /** @var NotificationListener $notificationListener */ - $notificationListener = $c->query(NotificationListener::class); - $notificationListener->evaluate($event); + $this->notificationListener->evaluate($event); } } diff --git a/apps/comments/lib/Notification/Listener.php b/apps/comments/lib/Notification/Listener.php index 68705085023..426e85cac83 100644 --- a/apps/comments/lib/Notification/Listener.php +++ b/apps/comments/lib/Notification/Listener.php @@ -61,12 +61,6 @@ class Listener { public function evaluate(CommentsEvent $event) { $comment = $event->getComment(); - if($comment->getObjectType() !== 'files') { - // comments App serves files only, other object types/apps need to - // register their own ICommentsEventHandler and trigger notifications - return; - } - $mentions = $this->extractMentions($comment->getMessage()); if(empty($mentions)) { // no one to notify diff --git a/apps/comments/tests/Unit/EventHandlerTest.php b/apps/comments/tests/Unit/EventHandlerTest.php index f377c01b3c9..bb714993f7a 100644 --- a/apps/comments/tests/Unit/EventHandlerTest.php +++ b/apps/comments/tests/Unit/EventHandlerTest.php @@ -23,30 +23,35 @@ namespace OCA\Comments\Tests\Unit\Notification; -use OCA\Comments\AppInfo\Application; use OCA\Comments\EventHandler; use OCP\Comments\CommentsEvent; use OCP\Comments\IComment; use OCA\Comments\Activity\Listener as ActivityListener; use OCA\Comments\Notification\Listener as NotificationListener; -use OCP\IContainer; use Test\TestCase; class EventHandlerTest extends TestCase { /** @var EventHandler */ protected $eventHandler; - /** @var Application|\PHPUnit_Framework_MockObject_MockObject */ - protected $app; + /** @var ActivityListener|\PHPUnit_Framework_MockObject_MockObject */ + protected $activityListener; + + /** @var NotificationListener|\PHPUnit_Framework_MockObject_MockObject */ + protected $notificationListener; protected function setUp() { parent::setUp(); - $this->app = $this->getMockBuilder(Application::class) + $this->activityListener = $this->getMockBuilder(ActivityListener::class) ->disableOriginalConstructor() ->getMock(); - $this->eventHandler = new EventHandler($this->app); + $this->notificationListener = $this->getMockBuilder(NotificationListener::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->eventHandler = new EventHandler($this->activityListener, $this->notificationListener); } public function testNotFiles() { @@ -100,31 +105,14 @@ class EventHandlerTest extends TestCase { ->method('getEvent') ->willReturn($eventType); - $notificationListener = $this->getMockBuilder(NotificationListener::class) - ->disableOriginalConstructor() - ->getMock(); - $notificationListener->expects($this->once()) + $this->notificationListener->expects($this->once()) ->method('evaluate') ->with($event); - $activityListener = $this->getMockBuilder(ActivityListener::class) - ->disableOriginalConstructor() - ->getMock(); - $activityListener->expects($this->any()) + $this->activityListener->expects($this->any()) ->method('commentEvent') ->with($event); - /** @var IContainer|\PHPUnit_Framework_MockObject_MockObject $c */ - $c = $this->getMockBuilder(IContainer::class)->getMock(); - $c->expects($this->atLeastOnce()) - ->method('query') - ->withConsecutive([NotificationListener::class], [ActivityListener::class]) - ->willReturnOnConsecutiveCalls($notificationListener, $activityListener); - - $this->app->expects($this->atLeastOnce()) - ->method('getContainer') - ->willReturn($c); - $this->eventHandler->handle($event); } diff --git a/apps/comments/tests/Unit/Notification/ListenerTest.php b/apps/comments/tests/Unit/Notification/ListenerTest.php index 5926264fa08..12f388fcff9 100644 --- a/apps/comments/tests/Unit/Notification/ListenerTest.php +++ b/apps/comments/tests/Unit/Notification/ListenerTest.php @@ -172,29 +172,6 @@ class ListenerTest extends TestCase { $this->listener->evaluate($event); } - public function testUnsupportedCommentObjectType() { - /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */ - $comment = $this->getMockBuilder('\OCP\Comments\IComment')->getMock(); - $comment->expects($this->once()) - ->method('getObjectType') - ->will($this->returnValue('vcards')); - $comment->expects($this->never()) - ->method('getMessage'); - - /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */ - $event = $this->getMockBuilder('\OCP\Comments\CommentsEvent') - ->disableOriginalConstructor() - ->getMock(); - $event->expects($this->once()) - ->method('getComment') - ->will($this->returnValue($comment)); - $event->expects(($this->any())) - ->method(('getEvent')) - ->will($this->returnValue(CommentsEvent::EVENT_ADD)); - - $this->listener->evaluate($event); - } - public function testEvaluateUserDoesNotExist() { $message = '@foobar bla bla bla'; -- 2.39.5