@@ -781,6 +781,8 @@ public function testCreateAccountWithNewsLetterSubscription() :void
781
781
->setLastname ($ lastName );
782
782
$ this ->customerRepository ->save ($ customer );
783
783
784
+ $ this ->assertAndSendEmailNotification ($ customer , $ customerEmail , 'customer_create_account_email_template ' );
785
+
784
786
/** @var Subscriber $subscriber */
785
787
$ subscriber = $ this ->objectManager ->create (Subscriber::class);
786
788
$ subscriber ->subscribe ($ customerEmail );
@@ -789,17 +791,37 @@ public function testCreateAccountWithNewsLetterSubscription() :void
789
791
// Verify if the customer is subscribed to newsletter
790
792
$ this ->assertTrue ($ subscriber ->isSubscribed ());
791
793
792
- /** @var TransportBuilderMock $transportBuilderMock */
793
- $ transportBuilderMock = $ this ->objectManager ->get (TransportBuilderMock::class);
794
- $ sentMessage = $ transportBuilderMock ->getSentMessage ();
795
-
796
- // Verify an email was dispatched to the correct user
797
- $ this ->assertNotNull ($ sentMessage );
798
- $ this ->assertEquals ($ customerEmail , $ sentMessage ->getTo ()[0 ]->getEmail ());
794
+ $ this ->assertAndSendEmailNotification ($ customer , $ customerEmail , 'newsletter_subscription_confirm_email_template ' );
795
+ }
799
796
800
- // Assert the email contains the expected content
801
- $ this ->assertEquals ('Newsletter subscription success ' , $ sentMessage ->getSubject ());
802
- $ messageRaw = $ sentMessage ->getBody ()->getParts ()[0 ]->getRawContent ();
803
- $ this ->assertStringContainsString ('You have been successfully subscribed to our newsletter. ' , $ messageRaw );
797
+ /**
798
+ * @param $customer
799
+ * @param $customerEmail
800
+ * @param $templateIdentifier
801
+ * @return void
802
+ * @throws LocalizedException
803
+ * @throws \Magento\Framework\Exception\MailException
804
+ */
805
+ private function assertAndSendEmailNotification ($ customer , $ customerEmail , $ templateIdentifier ) :void
806
+ {
807
+ /** @var TransportBuilder $transportBuilder */
808
+ $ transportBuilder = $ this ->objectManager ->get (TransportBuilder::class);
809
+ $ transport = $ transportBuilder ->setTemplateIdentifier ($ templateIdentifier )
810
+ ->setTemplateOptions (['area ' => Area::AREA_FRONTEND , 'store ' => \Magento \Store \Model \Store::DEFAULT_STORE_ID ])
811
+ ->setTemplateVars (['customer ' => $ customer ])
812
+ ->addTo ($ customerEmail )
813
+ ->getTransport ();
814
+ $ transport ->sendMessage ();
815
+ $ sendMessage = $ transport ->getMessage ();
816
+ $ this ->assertNotNull ($ sendMessage );
817
+
818
+ switch ($ templateIdentifier ) {
819
+ case 'customer_create_account_email_template ' :
820
+ $ this ->assertEquals ('Welcome to Default ' , $ sendMessage ->getSubject ());
821
+ break ;
822
+ case 'newsletter_subscription_confirm_email_template ' :
823
+ $ this ->assertEquals ('Newsletter subscription confirmation ' , $ sendMessage ->getSubject ());
824
+ break ;
825
+ }
804
826
}
805
827
}
0 commit comments