forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ed27d0
commit 4271090
Showing
17 changed files
with
612 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.git* export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
composer.lock | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.2 | ||
--- | ||
|
||
* Add the bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2024-present Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Sweego Bridge | ||
============= | ||
|
||
Provides Sweego integration for Symfony Mailer. | ||
|
||
Configuration example: | ||
|
||
```env | ||
# SMTP | ||
MAILER_DSN=sweego+smtp://sweego:API_KEY@default | ||
# API | ||
MAILER_DSN=sweego+api://API_KEY@default | ||
``` | ||
|
||
where: | ||
- `API_KEY` is your Sweego API Key | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
177 changes: 177 additions & 0 deletions
177
src/Symfony/Component/Mailer/Bridge/Sweego/Tests/Transport/SweegoApiTransportTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sweego\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\JsonMockResponse; | ||
use Symfony\Component\Mailer\Bridge\Sweego\Transport\SweegoApiTransport; | ||
use Symfony\Component\Mailer\Envelope; | ||
use Symfony\Component\Mailer\Exception\HttpTransportException; | ||
use Symfony\Component\Mailer\Header\MetadataHeader; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\Part\DataPart; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
class SweegoApiTransportTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getTransportData | ||
*/ | ||
public function testToString(SweegoApiTransport $transport, string $expected) | ||
{ | ||
$this->assertSame($expected, (string) $transport); | ||
} | ||
|
||
public static function getTransportData(): \Generator | ||
{ | ||
yield [ | ||
new SweegoApiTransport('ACCESS_KEY'), | ||
'sweego+api://api.sweego.io', | ||
]; | ||
|
||
yield [ | ||
(new SweegoApiTransport('ACCESS_KEY'))->setHost('example.com'), | ||
'sweego+api://example.com', | ||
]; | ||
|
||
yield [ | ||
(new SweegoApiTransport('ACCESS_KEY'))->setHost('example.com')->setPort(99), | ||
'sweego+api://example.com:99', | ||
]; | ||
} | ||
|
||
public function testCustomHeader() | ||
{ | ||
$params = ['param1' => 'foo', 'param2' => 'bar']; | ||
$json = json_encode(['custom_header_1' => 'custom_value_1']); | ||
|
||
$email = new Email(); | ||
$email->getHeaders() | ||
->add(new MetadataHeader('custom', $json)) | ||
->addTextHeader('templateId', 1) | ||
->addParameterizedHeader('params', 'params', $params) | ||
->addTextHeader('foo', 'bar'); | ||
$envelope = new Envelope(new Address('[email protected]', 'Alice'), [new Address('[email protected]', 'Bob')]); | ||
|
||
$transport = new SweegoApiTransport('ACCESS_KEY'); | ||
$method = new \ReflectionMethod(SweegoApiTransport::class, 'getPayload'); | ||
$payload = $method->invoke($transport, $email, $envelope); | ||
|
||
$this->assertArrayHasKey('X-Metadata-custom', $payload['headers']); | ||
$this->assertEquals($json, $payload['headers']['X-Metadata-custom']); | ||
$this->assertArrayHasKey('templateId', $payload['headers']); | ||
$this->assertEquals('1', $payload['headers']['templateId']); | ||
$this->assertArrayHasKey('params', $payload['headers']); | ||
$this->assertEquals('params; param1=foo; param2=bar', $payload['headers']['params']); | ||
$this->assertArrayHasKey('foo', $payload['headers']); | ||
$this->assertEquals('bar', $payload['headers']['foo']); | ||
} | ||
|
||
public function testSendThrowsForErrorResponse() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.sweego.io:8984/send', $url); | ||
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]); | ||
|
||
return new JsonMockResponse(['message' => 'i\'m a teapot'], [ | ||
'http_code' => 418, | ||
]); | ||
}); | ||
|
||
$transport = new SweegoApiTransport('ACCESS_KEY', $client); | ||
$transport->setPort(8984); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('[email protected]', 'Tony Stark')) | ||
->from(new Address('[email protected]', 'Fabien')) | ||
->text('Hello There!'); | ||
|
||
$this->expectException(HttpTransportException::class); | ||
$this->expectExceptionMessage('Unable to send an email: {"message":"i\'m a teapot"} (code 418).'); | ||
$transport->send($mail); | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.sweego.io:8984/send', $url); | ||
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]); | ||
|
||
return new JsonMockResponse(['id' => 'foobar'], [ | ||
'http_code' => 200, | ||
]); | ||
}); | ||
|
||
$transport = new SweegoApiTransport('ACCESS_KEY', $client); | ||
$transport->setPort(8984); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('[email protected]', 'Tony Stark')) | ||
->from(new Address('[email protected]', 'Fabien')) | ||
->text('Hello here!') | ||
->html('Hello there!') | ||
->addCc('[email protected]') | ||
->addBcc('[email protected]') | ||
; | ||
|
||
$message = $transport->send($mail); | ||
|
||
$this->assertSame('foobar', $message->getMessageId()); | ||
} | ||
|
||
/** | ||
* IDN (internationalized domain names) like kältetechnik-xyz.de need to be transformed to ACE | ||
* (ASCII Compatible Encoding) e.g.xn--kltetechnik-xyz-0kb.de, otherwise resend api answers with 400 http code. | ||
*/ | ||
public function testSendForIdnDomains() | ||
{ | ||
$client = new MockHttpClient(function (string $method, string $url, array $options): ResponseInterface { | ||
$this->assertSame('POST', $method); | ||
$this->assertSame('https://api.sweego.io:8984/send', $url); | ||
$this->assertStringContainsString('Accept: */*', $options['headers'][2] ?? $options['request_headers'][1]); | ||
|
||
$body = json_decode($options['body'], true); | ||
// to | ||
$this->assertSame([ | ||
'email' => 'kä[email protected]', | ||
'name' => 'Kältetechnik Xyz', | ||
], $body['recipients'][0]); | ||
// sender | ||
$this->assertStringContainsString('[email protected]', $body['from']['email']); | ||
$this->assertStringContainsString('Kältetechnik Xyz', $body['from']['name']); | ||
|
||
return new JsonMockResponse(['id' => 'foobar'], [ | ||
'http_code' => 200, | ||
]); | ||
}); | ||
|
||
$transport = new SweegoApiTransport('ACCESS_KEY', $client); | ||
$transport->setPort(8984); | ||
|
||
$mail = new Email(); | ||
$mail->subject('Hello!') | ||
->to(new Address('kältetechnik@kältetechnik-xyz.de', 'Kältetechnik Xyz')) | ||
->from(new Address('info@kältetechnik-xyz.de', 'Kältetechnik Xyz')) | ||
->text('Hello here!') | ||
->html('Hello there!'); | ||
|
||
$message = $transport->send($mail); | ||
|
||
$this->assertSame('foobar', $message->getMessageId()); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
src/Symfony/Component/Mailer/Bridge/Sweego/Tests/Transport/SweegoTransportFactoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sweego\Tests\Transport; | ||
|
||
use Psr\Log\NullLogger; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Mailer\Bridge\Sweego\Transport\SweegoApiTransport; | ||
use Symfony\Component\Mailer\Bridge\Sweego\Transport\SweegoSmtpTransport; | ||
use Symfony\Component\Mailer\Bridge\Sweego\Transport\SweegoTransportFactory; | ||
use Symfony\Component\Mailer\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Mailer\Transport\Dsn; | ||
use Symfony\Component\Mailer\Transport\TransportFactoryInterface; | ||
|
||
class SweegoTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
public function getFactory(): TransportFactoryInterface | ||
{ | ||
return new SweegoTransportFactory(null, new MockHttpClient(), new NullLogger()); | ||
} | ||
|
||
public static function supportsProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('sweego', 'default'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+smtp', 'default'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+smtp', 'example.com'), | ||
true, | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+api', 'default'), | ||
true, | ||
]; | ||
} | ||
|
||
public static function createProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('sweego', 'default', self::USER, self::PASSWORD, 465), | ||
new SweegoSmtpTransport('default', 465, self::USER, self::PASSWORD, null, new NullLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+smtp', 'default', self::USER, self::PASSWORD, 465), | ||
new SweegoSmtpTransport('default', 465, self::USER, self::PASSWORD, null, new NullLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+smtp', 'default', self::USER, self::PASSWORD, 465), | ||
new SweegoSmtpTransport('default', 465, self::USER, self::PASSWORD, null, new NullLogger()), | ||
]; | ||
|
||
yield [ | ||
new Dsn('sweego+api', 'default', self::USER), | ||
new SweegoApiTransport(self::USER, new MockHttpClient(), null, new NullLogger()), | ||
]; | ||
} | ||
|
||
public static function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield [ | ||
new Dsn('sweego+foo', 'default', self::USER, self::PASSWORD, 465), | ||
'The "sweego+foo" scheme is not supported; supported schemes for mailer "sweego" are: "sweego", "sweego+smtp", "sweego+api".', | ||
]; | ||
} | ||
|
||
public static function incompleteDsnProvider(): iterable | ||
{ | ||
yield [new Dsn('sweego+smtp', 'default', self::USER)]; | ||
|
||
yield [new Dsn('sweego+api', 'default')]; | ||
} | ||
} |
Oops, something went wrong.