]> source.dussan.org Git - nextcloud-server.git/commitdiff
apply some polish 1449/head
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Fri, 14 Oct 2016 13:36:05 +0000 (15:36 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Fri, 14 Oct 2016 13:36:05 +0000 (15:36 +0200)
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
apps/comments/lib/EventHandler.php
apps/comments/lib/Notification/Listener.php
apps/comments/tests/Unit/EventHandlerTest.php
apps/comments/tests/Unit/Notification/ListenerTest.php

index a5f312617e5113628e8c312029e889f1b34e24e8..8c20c698371267a33a7c733ab5c1f868fc6887d5 100644 (file)
@@ -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);
        }
 }
index 68705085023846c847a1c526d2939ba05655a2ac..426e85cac83957521eca7820abcfe96a19f96961 100644 (file)
@@ -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
index f377c01b3c9b75dc4917b1ef622a01490bdf04a6..bb714993f7ac358d1c6467377b077a8dd3d2d0ea 100644 (file)
 
 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);
        }
 
index 5926264fa08ae9650356b6071f7e604c0fede88c..12f388fcff927917486c8797d15949f8c2716a50 100644 (file)
@@ -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';