|
12 | 12 | namespace Symfony\Component\Mailer\Tests\Transport\Smtp;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\Mailer\Exception\TransportException; |
| 16 | +use Symfony\Component\Mailer\Transport\Smtp\Auth\CramMd5Authenticator; |
| 17 | +use Symfony\Component\Mailer\Transport\Smtp\Auth\LoginAuthenticator; |
| 18 | +use Symfony\Component\Mailer\Transport\Smtp\Auth\XOAuth2Authenticator; |
15 | 19 | use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
16 | 20 | use Symfony\Component\Mime\Email;
|
17 | 21 |
|
@@ -57,6 +61,137 @@ public function testExtensibility()
|
57 | 61 | $this-> assertContains( "MAIL FROM:<[email protected]> RET=HDRS\r\n", $stream-> getCommands());
|
58 | 62 | $this-> assertContains( "RCPT TO:<[email protected]> NOTIFY=FAILURE\r\n", $stream-> getCommands());
|
59 | 63 | }
|
| 64 | + |
| 65 | + public function testConstructorWithDefaultAuthenticators() |
| 66 | + { |
| 67 | + $stream = new DummyStream(); |
| 68 | + $transport = new EsmtpTransport(stream: $stream); |
| 69 | + $transport->setUsername('testuser'); |
| 70 | + $transport->setPassword('p4ssw0rd'); |
| 71 | + |
| 72 | + $message = new Email(); |
| 73 | + $message-> from( '[email protected]'); |
| 74 | + $message-> addTo( '[email protected]'); |
| 75 | + $message->text('.'); |
| 76 | + |
| 77 | + try { |
| 78 | + $transport->send($message); |
| 79 | + $this->fail('Symfony\Component\Mailer\Exception\TransportException to be thrown'); |
| 80 | + } catch (TransportException $e) { |
| 81 | + $this->assertStringStartsWith('Failed to authenticate on SMTP server with username "testuser" using the following authenticators: "CRAM-MD5", "LOGIN", "PLAIN", "XOAUTH2".', $e->getMessage()); |
| 82 | + } |
| 83 | + |
| 84 | + $this->assertEquals( |
| 85 | + [ |
| 86 | + "EHLO [127.0.0.1]\r\n", |
| 87 | + // S: 250 localhost |
| 88 | + // S: 250-AUTH PLAIN LOGIN CRAM-MD5 XOAUTH2 |
| 89 | + "AUTH CRAM-MD5\r\n", |
| 90 | + // S: 334 PDAxMjM0NTY3ODkuMDEyMzQ1NjdAc3ltZm9ueT4= |
| 91 | + "dGVzdHVzZXIgNTdlYzg2ODM5OWZhZThjY2M5OWFhZGVjZjhiZTAwNmY=\r\n", |
| 92 | + // S: 535 5.7.139 Authentication unsuccessful |
| 93 | + "RSET\r\n", |
| 94 | + // S: 250 2.0.0 Resetting |
| 95 | + "AUTH LOGIN\r\n", |
| 96 | + // S: 334 VXNlcm5hbWU6 |
| 97 | + "dGVzdHVzZXI=\r\n", |
| 98 | + // S: 334 UGFzc3dvcmQ6 |
| 99 | + "cDRzc3cwcmQ=\r\n", |
| 100 | + // S: 535 5.7.139 Authentication unsuccessful |
| 101 | + "RSET\r\n", |
| 102 | + // S: 250 2.0.0 Resetting |
| 103 | + "AUTH PLAIN dGVzdHVzZXIAdGVzdHVzZXIAcDRzc3cwcmQ=\r\n", |
| 104 | + // S: 535 5.7.139 Authentication unsuccessful |
| 105 | + "RSET\r\n", |
| 106 | + // S: 250 2.0.0 Resetting |
| 107 | + "AUTH XOAUTH2 dXNlcj10ZXN0dXNlcgFhdXRoPUJlYXJlciBwNHNzdzByZAEB\r\n", |
| 108 | + // S: 535 5.7.139 Authentication unsuccessful |
| 109 | + "RSET\r\n", |
| 110 | + // S: 250 2.0.0 Resetting |
| 111 | + ], |
| 112 | + $stream->getCommands() |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public function testConstructorWithRedefinedAuthenticators() |
| 117 | + { |
| 118 | + $stream = new DummyStream(); |
| 119 | + $transport = new EsmtpTransport( |
| 120 | + stream: $stream, |
| 121 | + authenticators: [new CramMd5Authenticator(), new LoginAuthenticator()] |
| 122 | + ); |
| 123 | + $transport->setUsername('testuser'); |
| 124 | + $transport->setPassword('p4ssw0rd'); |
| 125 | + |
| 126 | + $message = new Email(); |
| 127 | + $message-> from( '[email protected]'); |
| 128 | + $message-> addTo( '[email protected]'); |
| 129 | + $message->text('.'); |
| 130 | + |
| 131 | + try { |
| 132 | + $transport->send($message); |
| 133 | + $this->fail('Symfony\Component\Mailer\Exception\TransportException to be thrown'); |
| 134 | + } catch (TransportException $e) { |
| 135 | + $this->assertStringStartsWith('Failed to authenticate on SMTP server with username "testuser" using the following authenticators: "CRAM-MD5", "LOGIN".', $e->getMessage()); |
| 136 | + } |
| 137 | + |
| 138 | + $this->assertEquals( |
| 139 | + [ |
| 140 | + "EHLO [127.0.0.1]\r\n", |
| 141 | + // S: 250 localhost |
| 142 | + // S: 250-AUTH PLAIN LOGIN CRAM-MD5 XOAUTH2 |
| 143 | + "AUTH CRAM-MD5\r\n", |
| 144 | + // S: 334 PDAxMjM0NTY3ODkuMDEyMzQ1NjdAc3ltZm9ueT4= |
| 145 | + "dGVzdHVzZXIgNTdlYzg2ODM5OWZhZThjY2M5OWFhZGVjZjhiZTAwNmY=\r\n", |
| 146 | + // S: 535 5.7.139 Authentication unsuccessful |
| 147 | + "RSET\r\n", |
| 148 | + // S: 250 2.0.0 Resetting |
| 149 | + "AUTH LOGIN\r\n", |
| 150 | + // S: 334 VXNlcm5hbWU6 |
| 151 | + "dGVzdHVzZXI=\r\n", |
| 152 | + // S: 334 UGFzc3dvcmQ6 |
| 153 | + "cDRzc3cwcmQ=\r\n", |
| 154 | + // S: 535 5.7.139 Authentication unsuccessful |
| 155 | + "RSET\r\n", |
| 156 | + // S: 250 2.0.0 Resetting |
| 157 | + ], |
| 158 | + $stream->getCommands() |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + public function testSetAuthenticators() |
| 163 | + { |
| 164 | + $stream = new DummyStream(); |
| 165 | + $transport = new EsmtpTransport(stream: $stream); |
| 166 | + $transport->setUsername('testuser'); |
| 167 | + $transport->setPassword('p4ssw0rd'); |
| 168 | + $transport->setAuthenticators([new XOAuth2Authenticator()]); |
| 169 | + |
| 170 | + $message = new Email(); |
| 171 | + $message-> from( '[email protected]'); |
| 172 | + $message-> addTo( '[email protected]'); |
| 173 | + $message->text('.'); |
| 174 | + |
| 175 | + try { |
| 176 | + $transport->send($message); |
| 177 | + $this->fail('Symfony\Component\Mailer\Exception\TransportException to be thrown'); |
| 178 | + } catch (TransportException $e) { |
| 179 | + $this->assertStringStartsWith('Failed to authenticate on SMTP server with username "testuser" using the following authenticators: "XOAUTH2".', $e->getMessage()); |
| 180 | + } |
| 181 | + |
| 182 | + $this->assertEquals( |
| 183 | + [ |
| 184 | + "EHLO [127.0.0.1]\r\n", |
| 185 | + // S: 250 localhost |
| 186 | + // S: 250-AUTH PLAIN LOGIN CRAM-MD5 XOAUTH2 |
| 187 | + "AUTH XOAUTH2 dXNlcj10ZXN0dXNlcgFhdXRoPUJlYXJlciBwNHNzdzByZAEB\r\n", |
| 188 | + // S: 535 5.7.139 Authentication unsuccessful |
| 189 | + "RSET\r\n", |
| 190 | + // S: 250 2.0.0 Resetting |
| 191 | + ], |
| 192 | + $stream->getCommands() |
| 193 | + ); |
| 194 | + } |
60 | 195 | }
|
61 | 196 |
|
62 | 197 | class CustomEsmtpTransport extends EsmtpTransport
|
|
0 commit comments