NotifierTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace App\Tests\Unit\Service;
  3. use App\Entity\Access\Access;
  4. use App\Entity\Core\File;
  5. use App\Entity\Core\Notification;
  6. use App\Entity\Organization\Organization;
  7. use App\Enum\Core\NotificationTypeEnum;
  8. use App\Service\MercureHub;
  9. use App\Service\Notifier;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use PHPUnit\Framework\TestCase;
  12. class TestableNotifier extends Notifier {
  13. public function createNotification(Access $access, string $name, string $type, array $message, string $link = null): Notification {
  14. return parent::createNotification($access, $name, $type, $message, $link);
  15. }
  16. }
  17. class NotifierTest extends TestCase
  18. {
  19. private EntityManagerInterface $em;
  20. private MercureHub $mercureHub;
  21. public function setUp(): void {
  22. $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
  23. $this->mercureHub = $this->getMockBuilder(MercureHub::class)->disableOriginalConstructor()->getMock();
  24. }
  25. /**
  26. * @see Notifier::createNotification()
  27. */
  28. public function testCreateNotification(): void {
  29. $notifier = $this
  30. ->getMockBuilder(TestableNotifier::class)
  31. ->setConstructorArgs([$this->em, $this->mercureHub])
  32. ->setMethodsExcept(['createNotification'])
  33. ->getMock();
  34. $organization = $this->getMockBuilder(Organization::class)->getMock();
  35. $access = $this->getMockBuilder(Access::class)->getMock();
  36. $access->method('getId')->willReturn(1);
  37. $access->method('getOrganization')->willReturn($organization);
  38. $notification = $notifier->createNotification(
  39. $access,
  40. 'test',
  41. NotificationTypeEnum::MESSAGE()->getValue(),
  42. ['message'],
  43. 'link'
  44. );
  45. $this->assertEquals('test', $notification->getName());
  46. $this->assertEquals($access, $notification->getRecipientAccess());
  47. $this->assertEquals($organization, $notification->getRecipientOrganization());
  48. $this->assertEquals(NotificationTypeEnum::MESSAGE()->getValue(), $notification->getType());
  49. $this->assertEquals(['message'], $notification->getMessage());
  50. $this->assertEquals('link', $notification->getLink());
  51. $this->assertInstanceOf(\DateTime::class, $notification->getCreateDate());
  52. $this->assertInstanceOf(\DateTime::class, $notification->getUpdateDate());
  53. }
  54. /**
  55. * @see Notifier::notify()
  56. */
  57. public function testNotify(): void {
  58. $notifier = $this
  59. ->getMockBuilder(TestableNotifier::class)
  60. ->setConstructorArgs([$this->em, $this->mercureHub])
  61. ->setMethodsExcept(['notify'])
  62. ->getMock();
  63. $notification = $this->getMockBuilder(Notification::class)->getMock();
  64. $access = $this->getMockBuilder(Access::class)->getMock();
  65. $access->method('getId')->willReturn(1);
  66. $notifier
  67. ->expects(self::once())
  68. ->method('createNotification')
  69. ->with($access, 'test', NotificationTypeEnum::MESSAGE()->getValue(), ['message'], null)
  70. ->willReturn($notification);
  71. $this->em->expects(self::once())->method('persist')->with($notification);
  72. $this->em->expects(self::once())->method('flush');
  73. $this->mercureHub->expects(self::once())->method('publishCreate')->with(1, $notification);
  74. $returned = $notifier->notify($access, 'test', NotificationTypeEnum::MESSAGE()->getValue(), ['message']);
  75. $this->assertEquals($notification, $returned);
  76. }
  77. /**
  78. * @see Notifier::notifyExport()
  79. */
  80. public function testNotifyExport(): void {
  81. $notifier = $this
  82. ->getMockBuilder(TestableNotifier::class)
  83. ->setConstructorArgs([$this->em, $this->mercureHub])
  84. ->setMethodsExcept(['notifyExport'])
  85. ->getMock();
  86. $access = $this->getMockBuilder(Access::class)->getMock();
  87. $file = $this->getMockBuilder(File::class)->getMock();
  88. $file->method('getId')->willReturn(1);
  89. $file->method('getName')->willReturn('foo.txt');
  90. $notifier
  91. ->expects(self::once())
  92. ->method('notify')
  93. ->with($access, 'export', NotificationTypeEnum::FILE()->getValue(), ['fileName' => 'foo.txt'], '/api/files/1/download');
  94. $notifier->notifyExport($access, $file);
  95. }
  96. /**
  97. * @see Notifier::notifyMessage()
  98. */
  99. public function testNotifyMessage(): void {
  100. $notifier = $this
  101. ->getMockBuilder(TestableNotifier::class)
  102. ->setConstructorArgs([$this->em, $this->mercureHub])
  103. ->setMethodsExcept(['notifyMessage'])
  104. ->getMock();
  105. $access = $this->getMockBuilder(Access::class)->getMock();
  106. $notifier
  107. ->expects(self::once())
  108. ->method('notify')
  109. ->with($access, 'message', NotificationTypeEnum::MESSAGE()->getValue(), ['a message']);
  110. $notifier->notifyMessage($access, ['a message']);
  111. }
  112. /**
  113. * @see Notifier::notifySystem()
  114. */
  115. public function testNotifySystem(): void {
  116. $notifier = $this
  117. ->getMockBuilder(TestableNotifier::class)
  118. ->setConstructorArgs([$this->em, $this->mercureHub])
  119. ->setMethodsExcept(['notifySystem'])
  120. ->getMock();
  121. $access = $this->getMockBuilder(Access::class)->getMock();
  122. $notifier
  123. ->expects(self::once())
  124. ->method('notify')
  125. ->with($access, 'message', NotificationTypeEnum::SYSTEM()->getValue(), ['a message']);
  126. $notifier->notifySystem($access, ['a message']);
  127. }
  128. /**
  129. * @see Notifier::notifyError()
  130. */
  131. public function testNotifyError(): void {
  132. $notifier = $this
  133. ->getMockBuilder(TestableNotifier::class)
  134. ->setConstructorArgs([$this->em, $this->mercureHub])
  135. ->setMethodsExcept(['notifyError'])
  136. ->getMock();
  137. $access = $this->getMockBuilder(Access::class)->getMock();
  138. $notifier
  139. ->expects(self::once())
  140. ->method('notify')
  141. ->with($access, 'test', NotificationTypeEnum::ERROR()->getValue(), ['a message']);
  142. $notifier->notifyError($access, 'test', ['a message']);
  143. }
  144. }