EventHandlerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Comments\Tests\Unit\Notification;
  7. use OCA\Comments\Activity\Listener as ActivityListener;
  8. use OCA\Comments\Listener\CommentsEventListener;
  9. use OCA\Comments\Notification\Listener as NotificationListener;
  10. use OCP\Comments\CommentsEvent;
  11. use OCP\Comments\IComment;
  12. use Test\TestCase;
  13. class EventHandlerTest extends TestCase {
  14. /** @var CommentsEventListener */
  15. protected $eventHandler;
  16. /** @var ActivityListener|\PHPUnit\Framework\MockObject\MockObject */
  17. protected $activityListener;
  18. /** @var NotificationListener|\PHPUnit\Framework\MockObject\MockObject */
  19. protected $notificationListener;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->activityListener = $this->getMockBuilder(ActivityListener::class)
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $this->notificationListener = $this->getMockBuilder(NotificationListener::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener);
  29. }
  30. public function testNotFiles(): void {
  31. /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
  32. $comment = $this->getMockBuilder(IComment::class)->getMock();
  33. $comment->expects($this->once())
  34. ->method('getObjectType')
  35. ->willReturn('smiles');
  36. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  37. $event = $this->getMockBuilder(CommentsEvent::class)
  38. ->disableOriginalConstructor()
  39. ->getMock();
  40. $event->expects($this->once())
  41. ->method('getComment')
  42. ->willReturn($comment);
  43. $event->expects($this->never())
  44. ->method('getEvent');
  45. $this->eventHandler->handle($event);
  46. }
  47. public function handledProvider() {
  48. return [
  49. [CommentsEvent::EVENT_DELETE],
  50. [CommentsEvent::EVENT_UPDATE],
  51. [CommentsEvent::EVENT_PRE_UPDATE],
  52. [CommentsEvent::EVENT_ADD]
  53. ];
  54. }
  55. /**
  56. * @dataProvider handledProvider
  57. * @param string $eventType
  58. */
  59. public function testHandled($eventType): void {
  60. /** @var IComment|\PHPUnit\Framework\MockObject\MockObject $comment */
  61. $comment = $this->getMockBuilder(IComment::class)->getMock();
  62. $comment->expects($this->once())
  63. ->method('getObjectType')
  64. ->willReturn('files');
  65. /** @var CommentsEvent|\PHPUnit\Framework\MockObject\MockObject $event */
  66. $event = $this->getMockBuilder(CommentsEvent::class)
  67. ->disableOriginalConstructor()
  68. ->getMock();
  69. $event->expects($this->atLeastOnce())
  70. ->method('getComment')
  71. ->willReturn($comment);
  72. $event->expects($this->atLeastOnce())
  73. ->method('getEvent')
  74. ->willReturn($eventType);
  75. $this->notificationListener->expects($this->once())
  76. ->method('evaluate')
  77. ->with($event);
  78. $this->activityListener->expects($this->any())
  79. ->method('commentEvent')
  80. ->with($event);
  81. $this->eventHandler->handle($event);
  82. }
  83. }