MailHubTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. use App\Service\Core\ContactPointUtils;
  3. use App\Service\MailHub;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  6. use Symfony\Component\Mailer\MailerInterface;
  7. class MailHubTest extends TestCase
  8. {
  9. private MailerInterface $mailer;
  10. private string $opentalentNoReplyEmailAddress;
  11. private ContactPointUtils $contactPointUtils;
  12. private \App\Service\Access\Utils $accessUtils;
  13. public function setUp(): void {
  14. $this->mailer = $this->getMockBuilder(MailerInterface::class)->disableOriginalConstructor()->getMock();
  15. $this->opentalentNoReplyEmailAddress = 'noreply@opentalent.fr';
  16. $this->contactPointUtils = $this->getMockBuilder(ContactPointUtils::class)->disableOriginalConstructor()->getMock();
  17. $this->accessUtils = $this->getMockBuilder(\App\Service\Access\Utils::class)->disableOriginalConstructor()->getMock();
  18. }
  19. public function testSendAutomaticEmailTo(): void
  20. {
  21. $contactPoint = $this->getMockBuilder(\App\Entity\Core\ContactPoint::class)->disableOriginalConstructor()->getMock();
  22. $contactPoint->method('getEmail')->willReturn('mail@domain.net');
  23. $person = $this->getMockBuilder(\App\Entity\Person\Person::class)->disableOriginalConstructor()->getMock();
  24. $person->method('getFullName')->willReturn('Don Diego de la Vega');
  25. $person->method('getUsername')->willReturn('zorro2000');
  26. $access = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
  27. $access->method('getPerson')->willReturn($person);
  28. $this->contactPointUtils->expects(self::once())->method('getPersonContactPointPrincipal')->willReturn($contactPoint);
  29. $this->mailer->expects(self::once())
  30. ->method('send')
  31. ->with(self::isInstanceOf(TemplatedEmail::class));
  32. $mailerHub = new MailHub(
  33. $this->mailer,
  34. $this->opentalentNoReplyEmailAddress,
  35. $this->contactPointUtils,
  36. $this->accessUtils
  37. );
  38. $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
  39. }
  40. public function testSendAutomaticEmailToButNoAddress(): void
  41. {
  42. $access = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
  43. $this->contactPointUtils->expects(self::once())->method('getPersonContactPointPrincipal')->willReturn(null);
  44. $mailerHub = new MailHub(
  45. $this->mailer,
  46. $this->opentalentNoReplyEmailAddress,
  47. $this->contactPointUtils,
  48. $this->accessUtils
  49. );
  50. try {
  51. $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
  52. throw new AssertionError('A RuntimeException should have been thrown but has not');
  53. } catch (\RuntimeException $e) {}
  54. }
  55. public function testSendAutomaticEmailToAdmin() {
  56. $organization = $this->getMockBuilder(\App\Entity\Organization\Organization::class)->disableOriginalConstructor()->getMock();
  57. $admin = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
  58. $this->accessUtils->expects(self::once())->method('findAdminFor')->with($organization)->willReturn($admin);
  59. $mailerHub = $this->getMockBuilder(MailHub::class)
  60. ->onlyMethods(['sendAutomaticEmailTo'])
  61. ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
  62. ->getMock();
  63. $mailerHub->expects(self::once())->method('sendAutomaticEmailTo')->with($admin, 'subject', 'template', []);
  64. $mailerHub->sendAutomaticEmailToAdmin($organization, 'subject', 'template', []);
  65. }
  66. public function testSendAutomaticEmailToAdminButNoAdmin() {
  67. $organization = $this->getMockBuilder(\App\Entity\Organization\Organization::class)->disableOriginalConstructor()->getMock();
  68. $this->accessUtils->expects(self::once())->method('findAdminFor')->with($organization)->willReturn(null);
  69. $mailerHub = new MailHub(
  70. $this->mailer,
  71. $this->opentalentNoReplyEmailAddress,
  72. $this->contactPointUtils,
  73. $this->accessUtils
  74. );
  75. try {
  76. $mailerHub->sendAutomaticEmailToAdmin($organization, 'subject', 'template', []);
  77. throw new AssertionError('A RuntimeException should have been thrown but has not');
  78. } catch (\RuntimeException $e) {}
  79. }
  80. }