NotifierTest.php 6.1 KB

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