OutOfOfficeEventDispatcherJobTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Tests\unit\BackgroundJob;
  8. use OCA\DAV\BackgroundJob\OutOfOfficeEventDispatcherJob;
  9. use OCA\DAV\CalDAV\TimezoneService;
  10. use OCA\DAV\Db\Absence;
  11. use OCA\DAV\Db\AbsenceMapper;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\IUser;
  15. use OCP\IUserManager;
  16. use OCP\User\Events\OutOfOfficeEndedEvent;
  17. use OCP\User\Events\OutOfOfficeStartedEvent;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. use Test\TestCase;
  21. class OutOfOfficeEventDispatcherJobTest extends TestCase {
  22. private OutOfOfficeEventDispatcherJob $job;
  23. /** @var MockObject|ITimeFactory */
  24. private $timeFactory;
  25. /** @var MockObject|AbsenceMapper */
  26. private $absenceMapper;
  27. /** @var MockObject|LoggerInterface */
  28. private $logger;
  29. /** @var MockObject|IEventDispatcher */
  30. private $eventDispatcher;
  31. /** @var MockObject|IUserManager */
  32. private $userManager;
  33. private MockObject|TimezoneService $timezoneService;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->timeFactory = $this->createMock(ITimeFactory::class);
  37. $this->absenceMapper = $this->createMock(AbsenceMapper::class);
  38. $this->logger = $this->createMock(LoggerInterface::class);
  39. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  40. $this->userManager = $this->createMock(IUserManager::class);
  41. $this->timezoneService = $this->createMock(TimezoneService::class);
  42. $this->job = new OutOfOfficeEventDispatcherJob(
  43. $this->timeFactory,
  44. $this->absenceMapper,
  45. $this->logger,
  46. $this->eventDispatcher,
  47. $this->userManager,
  48. $this->timezoneService,
  49. );
  50. }
  51. public function testDispatchStartEvent(): void {
  52. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  53. $absence = new Absence();
  54. $absence->setId(200);
  55. $absence->setUserId('user');
  56. $user = $this->createMock(IUser::class);
  57. $user->method('getUID')
  58. ->willReturn('user');
  59. $this->absenceMapper->expects(self::once())
  60. ->method('findById')
  61. ->with(1)
  62. ->willReturn($absence);
  63. $this->userManager->expects(self::once())
  64. ->method('get')
  65. ->with('user')
  66. ->willReturn($user);
  67. $this->eventDispatcher->expects(self::once())
  68. ->method('dispatchTyped')
  69. ->with(self::callback(static function ($event): bool {
  70. self::assertInstanceOf(OutOfOfficeStartedEvent::class, $event);
  71. return true;
  72. }));
  73. $this->job->run([
  74. 'id' => 1,
  75. 'event' => OutOfOfficeEventDispatcherJob::EVENT_START,
  76. ]);
  77. }
  78. public function testDispatchStopEvent(): void {
  79. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  80. $absence = new Absence();
  81. $absence->setId(200);
  82. $absence->setUserId('user');
  83. $user = $this->createMock(IUser::class);
  84. $user->method('getUID')
  85. ->willReturn('user');
  86. $this->absenceMapper->expects(self::once())
  87. ->method('findById')
  88. ->with(1)
  89. ->willReturn($absence);
  90. $this->userManager->expects(self::once())
  91. ->method('get')
  92. ->with('user')
  93. ->willReturn($user);
  94. $this->eventDispatcher->expects(self::once())
  95. ->method('dispatchTyped')
  96. ->with(self::callback(static function ($event): bool {
  97. self::assertInstanceOf(OutOfOfficeEndedEvent::class, $event);
  98. return true;
  99. }));
  100. $this->job->run([
  101. 'id' => 1,
  102. 'event' => OutOfOfficeEventDispatcherJob::EVENT_END,
  103. ]);
  104. }
  105. public function testDoesntDispatchUnknownEvent(): void {
  106. $this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
  107. $absence = new Absence();
  108. $absence->setId(100);
  109. $absence->setUserId('user');
  110. $user = $this->createMock(IUser::class);
  111. $user->method('getUID')
  112. ->willReturn('user');
  113. $this->absenceMapper->expects(self::once())
  114. ->method('findById')
  115. ->with(1)
  116. ->willReturn($absence);
  117. $this->userManager->expects(self::once())
  118. ->method('get')
  119. ->with('user')
  120. ->willReturn($user);
  121. $this->eventDispatcher->expects(self::never())
  122. ->method('dispatchTyped');
  123. $this->logger->expects(self::once())
  124. ->method('error');
  125. $this->job->run([
  126. 'id' => 1,
  127. 'event' => 'foobar',
  128. ]);
  129. }
  130. }