MailHubTest.php 4.7 KB

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