NotifierTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Tests\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. public function testCreateNotification(): void {
  26. $notifier = $this
  27. ->getMockBuilder(TestableNotifier::class)
  28. ->setConstructorArgs([$this->em, $this->mercureHub])
  29. ->setMethodsExcept(['createNotification'])
  30. ->getMock();
  31. $organization = $this->getMockBuilder(Organization::class)->getMock();
  32. $access = $this->getMockBuilder(Access::class)->getMock();
  33. $access->method('getId')->willReturn(1);
  34. $access->method('getOrganization')->willReturn($organization);
  35. $notification = $notifier->createNotification(
  36. $access,
  37. 'test',
  38. NotificationTypeEnum::MESSAGE()->getValue(),
  39. ['message'],
  40. 'link'
  41. );
  42. $this->assertEquals('test', $notification->getName());
  43. $this->assertEquals($access, $notification->getRecipientAccess());
  44. $this->assertEquals($organization, $notification->getRecipientOrganization());
  45. $this->assertEquals(NotificationTypeEnum::MESSAGE()->getValue(), $notification->getType());
  46. $this->assertEquals(['message'], $notification->getMessage());
  47. $this->assertEquals('link', $notification->getLink());
  48. $this->assertInstanceOf(\DateTime::class, $notification->getCreateDate());
  49. $this->assertInstanceOf(\DateTime::class, $notification->getUpdateDate());
  50. }
  51. public function testNotify(): void {
  52. $notifier = $this
  53. ->getMockBuilder(TestableNotifier::class)
  54. ->setConstructorArgs([$this->em, $this->mercureHub])
  55. ->setMethodsExcept(['notify'])
  56. ->getMock();
  57. $notification = $this->getMockBuilder(Notification::class)->getMock();
  58. $access = $this->getMockBuilder(Access::class)->getMock();
  59. $access->method('getId')->willReturn(1);
  60. $notifier
  61. ->expects(self::once())
  62. ->method('createNotification')
  63. ->with($access, 'test', NotificationTypeEnum::MESSAGE()->getValue(), ['message'], null)
  64. ->willReturn($notification);
  65. $this->em->expects(self::once())->method('persist')->with($notification);
  66. $this->em->expects(self::once())->method('flush');
  67. $this->mercureHub->expects(self::once())->method('publishCreate')->with(1, $notification);
  68. $returned = $notifier->notify($access, 'test', NotificationTypeEnum::MESSAGE()->getValue(), ['message']);
  69. $this->assertEquals($notification, $returned);
  70. }
  71. public function testNotifyExport(): void {
  72. $notifier = $this
  73. ->getMockBuilder(TestableNotifier::class)
  74. ->setConstructorArgs([$this->em, $this->mercureHub])
  75. ->setMethodsExcept(['notifyExport'])
  76. ->getMock();
  77. $access = $this->getMockBuilder(Access::class)->getMock();
  78. $file = $this->getMockBuilder(File::class)->getMock();
  79. $file->method('getId')->willReturn(1);
  80. $file->method('getName')->willReturn('foo.txt');
  81. $notifier
  82. ->expects(self::once())
  83. ->method('notify')
  84. ->with($access, 'export', NotificationTypeEnum::FILE()->getValue(), ['fileName' => 'foo.txt'], '/api/files/1/download');
  85. $notifier->notifyExport($access, $file);
  86. }
  87. public function testNotifyMessage(): void {
  88. $notifier = $this
  89. ->getMockBuilder(TestableNotifier::class)
  90. ->setConstructorArgs([$this->em, $this->mercureHub])
  91. ->setMethodsExcept(['notifyMessage'])
  92. ->getMock();
  93. $access = $this->getMockBuilder(Access::class)->getMock();
  94. $notifier
  95. ->expects(self::once())
  96. ->method('notify')
  97. ->with($access, 'message', NotificationTypeEnum::MESSAGE()->getValue(), ['a message']);
  98. $notifier->notifyMessage($access, ['a message']);
  99. }
  100. public function testNotifySystem(): void {
  101. $notifier = $this
  102. ->getMockBuilder(TestableNotifier::class)
  103. ->setConstructorArgs([$this->em, $this->mercureHub])
  104. ->setMethodsExcept(['notifySystem'])
  105. ->getMock();
  106. $access = $this->getMockBuilder(Access::class)->getMock();
  107. $notifier
  108. ->expects(self::once())
  109. ->method('notify')
  110. ->with($access, 'message', NotificationTypeEnum::SYSTEM()->getValue(), ['a message']);
  111. $notifier->notifySystem($access, ['a message']);
  112. }
  113. public function testNotifyError(): void {
  114. $notifier = $this
  115. ->getMockBuilder(TestableNotifier::class)
  116. ->setConstructorArgs([$this->em, $this->mercureHub])
  117. ->setMethodsExcept(['notifyError'])
  118. ->getMock();
  119. $access = $this->getMockBuilder(Access::class)->getMock();
  120. $notifier
  121. ->expects(self::once())
  122. ->method('notify')
  123. ->with($access, 'test', NotificationTypeEnum::ERROR()->getValue(), ['a message']);
  124. $notifier->notifyError($access, 'test', ['a message']);
  125. }
  126. }