* @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;
}
/**
$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;
}
* @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);
}
}
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
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() {
->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);
}
$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';