1
0

EventHandlerTest.php 3.7 KB

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