17
17
use PHPUnit \Framework \MockObject \MockObject ;
18
18
use PHPUnit \Framework \TestCase ;
19
19
use Sonata \UserBundle \Mailer \Mailer ;
20
+ use Symfony \Component \Mailer \MailerInterface as SymfonyMailerInterface ;
21
+ use Symfony \Component \Mime \Email ;
20
22
use Symfony \Component \Routing \RouterInterface ;
21
23
use Twig \Environment ;
22
24
@@ -33,7 +35,7 @@ class MailerTest extends TestCase
33
35
private $ templating ;
34
36
35
37
/**
36
- * @var \Swift_Mailer |MockObject
38
+ * @var SymfonyMailerInterface |MockObject
37
39
*/
38
40
private $ mailer ;
39
41
@@ -51,8 +53,10 @@ protected function setUp(): void
51
53
{
52
54
$ this ->router = $ this ->createMock (RouterInterface::class);
53
55
$ this ->templating = $ this ->createMock (Environment::class);
54
- $ this ->mailer = $ this ->createMock (\Swift_Mailer::class);
55
- $ this ->emailFrom = ['noreply@sonata-project.org ' ];
56
+ $ this ->mailer = $ this ->createMock (SymfonyMailerInterface::class);
57
+ $ this ->emailFrom = [
58
+ 'noreply@sonata-project.org ' => 'Sonata Project ' ,
59
+ ];
56
60
$ this ->template = 'foo ' ;
57
61
}
58
62
@@ -71,7 +75,7 @@ public function testSendConfirmationEmailMessage(): void
71
75
*/
72
76
public function testSendResettingEmailMessage (string $ template , string $ subject , string $ body ): void
73
77
{
74
- $ user = $ this ->createMock (UserInterface::class);
78
+ $ user = $ this ->createStub (UserInterface::class);
75
79
$ user
76
80
->method ('getConfirmationToken ' )
77
81
->willReturn ('user-token ' );
@@ -89,18 +93,63 @@ public function testSendResettingEmailMessage(string $template, string $subject,
89
93
->with ('foo ' , ['user ' => $ user , 'confirmationUrl ' => '/foo ' ])
90
94
->willReturn ($ template );
91
95
96
+ $ fromName = current ($ this ->emailFrom );
97
+ $ fromAddress = current (array_keys ($ this ->emailFrom ));
98
+
99
+ $ email = (new Email ())
100
+ ->from (sprintf ('%s <%s> ' , $ fromName , $ fromAddress ))
101
+ ->to ((string ) $ user ->getEmail ())
102
+ ->subject ($ subject )
103
+ ->html ($ body );
104
+
92
105
$ this ->mailer ->expects ($ this ->once ())
93
106
->method ('send ' )
94
- ->willReturnCallback (function (\Swift_Message $ message ) use ($ subject , $ body ): void {
95
- $ this ->assertSame ($ subject , $ message ->getSubject ());
96
- $ this ->assertSame ($ body , $ message ->getBody ());
97
- $ this ->assertArrayHasKey ($ this ->emailFrom [0 ], $ message ->getFrom ());
98
- $ this ->assertArrayHasKey ('user@sonata-project.org ' , $ message ->getTo ());
99
- });
107
+ ->with ($ this ->equalTo ($ email ));
100
108
101
109
$ this ->getMailer ()->sendResettingEmailMessage ($ user );
102
110
}
103
111
112
+ /**
113
+ * NEXT_MAJOR: Remove this method.
114
+ *
115
+ * @group legacy
116
+ *
117
+ * @dataProvider emailTemplateData
118
+ */
119
+ public function testSendResettingEmailMessageWithSwiftMailer (string $ template , string $ subject , string $ body ): void
120
+ {
121
+ $ user = $ this ->createStub (UserInterface::class);
122
+ $ user
123
+ ->method ('getConfirmationToken ' )
124
+ ->willReturn ('user-token ' );
125
+ $ user
126
+ ->method ('getEmail ' )
127
+ ->willReturn ('user@sonata-project.org ' );
128
+
129
+ $ this ->router ->expects ($ this ->once ())
130
+ ->method ('generate ' )
131
+ ->with ('sonata_user_admin_resetting_reset ' , ['token ' => 'user-token ' ])
132
+ ->willReturn ('/foo ' );
133
+
134
+ $ this ->templating ->expects ($ this ->once ())
135
+ ->method ('render ' )
136
+ ->with ('foo ' , ['user ' => $ user , 'confirmationUrl ' => '/foo ' ])
137
+ ->willReturn ($ template );
138
+
139
+ $ swiftMailer = $ this ->createMock (\Swift_Mailer::class);
140
+
141
+ $ swiftMailer ->expects ($ this ->once ())
142
+ ->method ('send ' )
143
+ ->with ($ this ->callback (function (\Swift_Message $ message ) use ($ subject , $ body , $ user ): bool {
144
+ return $ subject === $ message ->getSubject ()
145
+ && $ body === $ message ->getBody ()
146
+ && $ this ->emailFrom === $ message ->getFrom ()
147
+ && \array_key_exists ((string ) $ user ->getEmail (), $ message ->getTo ());
148
+ }));
149
+
150
+ (new Mailer ($ this ->router , $ this ->templating , $ swiftMailer , $ this ->emailFrom , $ this ->template ))->sendResettingEmailMessage ($ user );
151
+ }
152
+
104
153
public function emailTemplateData (): array
105
154
{
106
155
return [
0 commit comments