apply some polish

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2016-10-14 15:36:05 +02:00
parent 1bcd2ca8e3
commit 70c7781aa8
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
4 changed files with 28 additions and 86 deletions

View File

@ -36,12 +36,15 @@ use OCP\Comments\ICommentsEventHandler;
* @package OCA\Comments * @package OCA\Comments
*/ */
class EventHandler implements ICommentsEventHandler { class EventHandler implements ICommentsEventHandler {
/** @var ActivityListener */
private $activityListener;
/** @var Application */ /** @var NotificationListener */
protected $app; private $notificationListener;
public function __construct(Application $app) { public function __construct(ActivityListener $activityListener, NotificationListener $notificationListener) {
$this->app = $app; $this->activityListener = $activityListener;
$this->notificationListener = $notificationListener;
} }
/** /**
@ -55,30 +58,18 @@ class EventHandler implements ICommentsEventHandler {
$eventType = $event->getEvent(); $eventType = $event->getEvent();
if( $eventType === CommentsEvent::EVENT_ADD if( $eventType === CommentsEvent::EVENT_ADD
&& $event instanceof CommentsEvent
) { ) {
$this->notificationHandler($event); $this->notificationHandler($event);
$this->activityHandler($event); $this->activityHandler($event);
return; return;
} }
if( $eventType === CommentsEvent::EVENT_PRE_UPDATE $applicableEvents = [
&& $event instanceof CommentsEvent CommentsEvent::EVENT_PRE_UPDATE,
) { CommentsEvent::EVENT_UPDATE,
$this->notificationHandler($event); CommentsEvent::EVENT_DELETE,
return; ];
} if(in_array($eventType, $applicableEvents)) {
if( $eventType === CommentsEvent::EVENT_UPDATE
&& $event instanceof CommentsEvent
) {
$this->notificationHandler($event);
return;
}
if( $eventType === CommentsEvent::EVENT_DELETE
&& $event instanceof CommentsEvent
) {
$this->notificationHandler($event); $this->notificationHandler($event);
return; return;
} }
@ -88,21 +79,13 @@ class EventHandler implements ICommentsEventHandler {
* @param CommentsEvent $event * @param CommentsEvent $event
*/ */
private function activityHandler(CommentsEvent $event) { private function activityHandler(CommentsEvent $event) {
$c = $this->app->getContainer(); $this->activityListener->commentEvent($event);
/** @var ActivityListener $listener */
$activityListener = $c->query(ActivityListener::class);
$activityListener->commentEvent($event);
} }
/** /**
* @param CommentsEvent $event * @param CommentsEvent $event
*/ */
private function notificationHandler(CommentsEvent $event) { private function notificationHandler(CommentsEvent $event) {
$c = $this->app->getContainer(); $this->notificationListener->evaluate($event);
/** @var NotificationListener $notificationListener */
$notificationListener = $c->query(NotificationListener::class);
$notificationListener->evaluate($event);
} }
} }

View File

@ -61,12 +61,6 @@ class Listener {
public function evaluate(CommentsEvent $event) { public function evaluate(CommentsEvent $event) {
$comment = $event->getComment(); $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()); $mentions = $this->extractMentions($comment->getMessage());
if(empty($mentions)) { if(empty($mentions)) {
// no one to notify // no one to notify

View File

@ -23,30 +23,35 @@
namespace OCA\Comments\Tests\Unit\Notification; namespace OCA\Comments\Tests\Unit\Notification;
use OCA\Comments\AppInfo\Application;
use OCA\Comments\EventHandler; use OCA\Comments\EventHandler;
use OCP\Comments\CommentsEvent; use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment; use OCP\Comments\IComment;
use OCA\Comments\Activity\Listener as ActivityListener; use OCA\Comments\Activity\Listener as ActivityListener;
use OCA\Comments\Notification\Listener as NotificationListener; use OCA\Comments\Notification\Listener as NotificationListener;
use OCP\IContainer;
use Test\TestCase; use Test\TestCase;
class EventHandlerTest extends TestCase { class EventHandlerTest extends TestCase {
/** @var EventHandler */ /** @var EventHandler */
protected $eventHandler; protected $eventHandler;
/** @var Application|\PHPUnit_Framework_MockObject_MockObject */ /** @var ActivityListener|\PHPUnit_Framework_MockObject_MockObject */
protected $app; protected $activityListener;
/** @var NotificationListener|\PHPUnit_Framework_MockObject_MockObject */
protected $notificationListener;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->app = $this->getMockBuilder(Application::class) $this->activityListener = $this->getMockBuilder(ActivityListener::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->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() { public function testNotFiles() {
@ -100,31 +105,14 @@ class EventHandlerTest extends TestCase {
->method('getEvent') ->method('getEvent')
->willReturn($eventType); ->willReturn($eventType);
$notificationListener = $this->getMockBuilder(NotificationListener::class) $this->notificationListener->expects($this->once())
->disableOriginalConstructor()
->getMock();
$notificationListener->expects($this->once())
->method('evaluate') ->method('evaluate')
->with($event); ->with($event);
$activityListener = $this->getMockBuilder(ActivityListener::class) $this->activityListener->expects($this->any())
->disableOriginalConstructor()
->getMock();
$activityListener->expects($this->any())
->method('commentEvent') ->method('commentEvent')
->with($event); ->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); $this->eventHandler->handle($event);
} }

View File

@ -172,29 +172,6 @@ class ListenerTest extends TestCase {
$this->listener->evaluate($event); $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() { public function testEvaluateUserDoesNotExist() {
$message = '@foobar bla bla bla'; $message = '@foobar bla bla bla';