You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventHandlerTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Comments\Tests\Unit\Notification;
  25. use OCA\Comments\Activity\Listener as ActivityListener;
  26. use OCA\Comments\EventHandler;
  27. use OCA\Comments\Notification\Listener as NotificationListener;
  28. use OCP\Comments\CommentsEvent;
  29. use OCP\Comments\IComment;
  30. use Test\TestCase;
  31. class EventHandlerTest extends TestCase {
  32. /** @var EventHandler */
  33. protected $eventHandler;
  34. /** @var ActivityListener|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $activityListener;
  36. /** @var NotificationListener|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $notificationListener;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->activityListener = $this->getMockBuilder(ActivityListener::class)
  41. ->disableOriginalConstructor()
  42. ->getMock();
  43. $this->notificationListener = $this->getMockBuilder(NotificationListener::class)
  44. ->disableOriginalConstructor()
  45. ->getMock();
  46. $this->eventHandler = new EventHandler($this->activityListener, $this->notificationListener);
  47. }
  48. public function testNotFiles() {
  49. /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
  50. $comment = $this->getMockBuilder(IComment::class)->getMock();
  51. $comment->expects($this->once())
  52. ->method('getObjectType')
  53. ->willReturn('smiles');
  54. /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
  55. $event = $this->getMockBuilder(CommentsEvent::class)
  56. ->disableOriginalConstructor()
  57. ->getMock();
  58. $event->expects($this->once())
  59. ->method('getComment')
  60. ->willReturn($comment);
  61. $event->expects($this->never())
  62. ->method('getEvent');
  63. $this->eventHandler->handle($event);
  64. }
  65. public function handledProvider() {
  66. return [
  67. [CommentsEvent::EVENT_DELETE],
  68. [CommentsEvent::EVENT_UPDATE],
  69. [CommentsEvent::EVENT_PRE_UPDATE],
  70. [CommentsEvent::EVENT_ADD]
  71. ];
  72. }
  73. /**
  74. * @dataProvider handledProvider
  75. * @param string $eventType
  76. */
  77. public function testHandled($eventType) {
  78. /** @var IComment|\PHPUnit_Framework_MockObject_MockObject $comment */
  79. $comment = $this->getMockBuilder(IComment::class)->getMock();
  80. $comment->expects($this->once())
  81. ->method('getObjectType')
  82. ->willReturn('files');
  83. /** @var CommentsEvent|\PHPUnit_Framework_MockObject_MockObject $event */
  84. $event = $this->getMockBuilder(CommentsEvent::class)
  85. ->disableOriginalConstructor()
  86. ->getMock();
  87. $event->expects($this->atLeastOnce())
  88. ->method('getComment')
  89. ->willReturn($comment);
  90. $event->expects($this->atLeastOnce())
  91. ->method('getEvent')
  92. ->willReturn($eventType);
  93. $this->notificationListener->expects($this->once())
  94. ->method('evaluate')
  95. ->with($event);
  96. $this->activityListener->expects($this->any())
  97. ->method('commentEvent')
  98. ->with($event);
  99. $this->eventHandler->handle($event);
  100. }
  101. }