em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock(); $this->mercureHub = $this->getMockBuilder(MercureHub::class)->disableOriginalConstructor()->getMock(); } /** * @see Notifier::createNotification() */ public function testCreateNotification(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['createNotification']) ->getMock(); $organization = $this->getMockBuilder(Organization::class)->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $access->method('getId')->willReturn(1); $access->method('getOrganization')->willReturn($organization); $notification = $notifier->createNotification( $access, 'test', NotificationTypeEnum::MESSAGE, ['message'], 'link' ); $this->assertEquals('test', $notification->getName()); $this->assertEquals($access, $notification->getRecipientAccess()); $this->assertEquals($organization, $notification->getRecipientOrganization()); $this->assertEquals(NotificationTypeEnum::MESSAGE, $notification->getType()); $this->assertEquals(['message'], $notification->getMessage()); $this->assertEquals('link', $notification->getLink()); $this->assertInstanceOf(\DateTime::class, $notification->getCreateDate()); $this->assertInstanceOf(\DateTime::class, $notification->getUpdateDate()); } /** * @see Notifier::notify() */ public function testNotify(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['notify']) ->getMock(); $notification = $this->getMockBuilder(Notification::class)->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $access->method('getId')->willReturn(1); $notifier ->expects(self::once()) ->method('createNotification') ->with($access, 'test', NotificationTypeEnum::MESSAGE, ['message'], null) ->willReturn($notification); $this->em->expects(self::once())->method('persist')->with($notification); $this->em->expects(self::once())->method('flush'); $this->mercureHub->expects(self::once())->method('publishCreate')->with(1, $notification); $returned = $notifier->notify($access, 'test', NotificationTypeEnum::MESSAGE, ['message']); $this->assertEquals($notification, $returned); } /** * @see Notifier::notifyExport() */ public function testNotifyExport(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['notifyExport']) ->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $file = $this->getMockBuilder(File::class)->getMock(); $file->method('getId')->willReturn(1); $file->method('getName')->willReturn('foo.txt'); $notifier ->expects(self::once()) ->method('notify') ->with($access, 'export', NotificationTypeEnum::FILE, ['fileName' => 'foo.txt'], '/api/files/1/download'); $notifier->notifyExport($access, $file); } /** * @see Notifier::notifyMessage() */ public function testNotifyMessage(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['notifyMessage']) ->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $notifier ->expects(self::once()) ->method('notify') ->with($access, 'message', NotificationTypeEnum::MESSAGE, ['a message']); $notifier->notifyMessage($access, ['a message']); } /** * @see Notifier::notifySystem() */ public function testNotifySystem(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['notifySystem']) ->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $notifier ->expects(self::once()) ->method('notify') ->with($access, 'message', NotificationTypeEnum::SYSTEM, ['a message']); $notifier->notifySystem($access, ['a message']); } /** * @see Notifier::notifyError() */ public function testNotifyError(): void { $notifier = $this ->getMockBuilder(TestableNotifier::class) ->setConstructorArgs([$this->em, $this->mercureHub]) ->setMethodsExcept(['notifyError']) ->getMock(); $access = $this->getMockBuilder(Access::class)->getMock(); $notifier ->expects(self::once()) ->method('notify') ->with($access, 'test', NotificationTypeEnum::ERROR, ['a message']); $notifier->notifyError($access, 'test', ['a message']); } }