translatorMock = $this->createMock(TranslatorInterface::class); $this->routerMock = $this->createMock(Router::class); $this->notificationModelMock = $this->createMock(NotificationModel::class); $this->entityManagerMock = $this->createMock(EntityManager::class); $this->mailHelperMock = $this->createMock(MailHelper::class); $this->coreParamHelperMock = $this->createMock(CoreParametersHelper::class); $this->webhook = $this->createMock(Webhook::class); } public function testSendToOwner(): void { $this->mockCommonMethods(1); $this->webhook ->expects($this->once()) ->method('getCreatedBy') ->willReturn($this->createdBy); $this->webhook ->expects($this->once()) ->method('getModifiedBy') ->willReturn($this->modifiedBy); $this->entityManagerMock ->expects($this->once()) ->method('getReference') ->with(User::class, $this->createdBy) ->willReturn($this->owner); $this->notificationModelMock ->expects($this->once()) ->method('addNotification') ->with( $this->details, 'error', false, $this->subject, null, false, $this->owner ); $this->mailHelperMock ->expects($this->once()) ->method('setTo') ->with($this->ownerEmail); $this->webhookKillNotificator->send($this->webhook, $this->reason); } public function testSendToModifier(): void { $this->ownerEmail = 'ownerEmail'; $this->modifiedBy = 'modifiedBy'; $modifier = $this->createMock(User::class); $modifierEmail = 'modifierEmail'; $this->mockCommonMethods(1); $this->webhook ->expects($this->exactly(2)) ->method('getCreatedBy') ->willReturn($this->createdBy); $this->webhook ->expects($this->exactly(3)) ->method('getModifiedBy') ->willReturn($this->modifiedBy); $this->entityManagerMock ->method('getReference') ->withConsecutive([User::class, $this->createdBy], [User::class, $this->modifiedBy]) ->willReturn($this->owner, $modifier); $this->notificationModelMock ->expects($this->once()) ->method('addNotification') ->with( $this->details, 'error', false, $this->subject, null, false, $modifier ); $modifier ->expects($this->once()) ->method('getEmail') ->willReturn($modifierEmail); $this->mailHelperMock ->expects($this->once()) ->method('setTo') ->with($modifierEmail); $this->mailHelperMock ->expects($this->once()) ->method('setCc') ->with([$this->ownerEmail => null]); $this->webhookKillNotificator->send($this->webhook, $this->reason); } private function mockCommonMethods(int $sentToAuther, string $emailToSend = null): void { $this->coreParamHelperMock ->method('get') ->withConsecutive(['webhook_send_notification_to_author'], ['webhook_notification_email_addresses']) ->willReturnOnConsecutiveCalls($sentToAuther, $emailToSend); $this->webhookKillNotificator = new WebhookKillNotificator($this->translatorMock, $this->routerMock, $this->notificationModelMock, $this->entityManagerMock, $this->mailHelperMock, $this->coreParamHelperMock); $this->owner = $this->createMock(User::class); $htmlUrl = ''.$this->webhookName.''; $this->translatorMock ->method('trans') ->withConsecutive(['mautic.webhook.stopped'], [$this->reason], ['mautic.webhook.stopped.details', ['%reason%' => $this->reason, '%webhook%' => $htmlUrl]]) ->willReturnOnConsecutiveCalls($this->subject, $this->reason, $this->details); $this->webhook->expects($this->once()) ->method('getId') ->willReturn($this->webhookId); $this->webhook->expects($this->once()) ->method('getName') ->willReturn($this->webhookName); $this->routerMock ->expects($this->once()) ->method('generate') ->with( 'mautic_webhook_action', ['objectAction' => 'view', 'objectId' => $this->webhookId], UrlGeneratorInterface::ABSOLUTE_URL ) ->willReturn($this->generatedRoute); if ($sentToAuther) { $this->owner ->expects($this->once()) ->method('getEmail') ->willReturn($this->ownerEmail); } $this->mailHelperMock ->expects($this->once()) ->method('setSubject') ->with($this->subject); $this->mailHelperMock ->expects($this->once()) ->method('setBody') ->with($this->details); } }