-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Showing
22 changed files
with
1,317 additions
and
0 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
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Messenger/Bridge/AmazonSqs/.gitattributes
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 | ||
/.gitignore 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 |
8 changes: 8 additions & 0 deletions
8
src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md
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,8 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.1.0 | ||
----- | ||
|
||
* Introduced the Amazon SQS 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) 2020 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. |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/Messenger/Bridge/AmazonSqs/README.md
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,12 @@ | ||
Amazon SQS Messenger | ||
==================== | ||
|
||
Provides Amazon SQS integration for Symfony Messenger. | ||
|
||
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) |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Fixtures/DummyMessage.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,18 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures; | ||
|
||
class DummyMessage | ||
{ | ||
private $message; | ||
|
||
public function __construct(string $message) | ||
{ | ||
$this->message = $message; | ||
} | ||
|
||
public function getMessage(): string | ||
{ | ||
return $this->message; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsIntegrationTest.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,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection; | ||
|
||
class AmazonSqsIntegrationTest extends TestCase | ||
{ | ||
private $connection; | ||
|
||
protected function setUp(): void | ||
{ | ||
if (!getenv('MESSENGER_SQS_DSN')) { | ||
$this->markTestSkipped('The "MESSENGER_SQS_DSN" environment variable is required.'); | ||
} | ||
|
||
$this->connection = Connection::fromDsn(getenv('MESSENGER_SQS_DSN'), []); | ||
$this->connection->setup(); | ||
$this->clearSqs(); | ||
} | ||
|
||
public function testConnectionSendAndGet() | ||
{ | ||
$this->connection->send('{"message": "Hi"}', ['type' => DummyMessage::class]); | ||
$this->assertSame(1, $this->connection->getMessageCount()); | ||
|
||
$wait = 0; | ||
while ((null === $encoded = $this->connection->get()) && $wait++ < 200) { | ||
usleep(5000); | ||
} | ||
|
||
$this->assertEquals('{"message": "Hi"}', $encoded['body']); | ||
$this->assertEquals(['type' => DummyMessage::class], $encoded['headers']); | ||
} | ||
|
||
private function clearSqs() | ||
{ | ||
$wait = 0; | ||
while ($wait++ < 50) { | ||
if (null === $message = $this->connection->get()) { | ||
usleep(5000); | ||
continue; | ||
} | ||
$this->connection->delete($message['id']); | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsReceiverTest.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,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsReceiver; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection; | ||
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException; | ||
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer; | ||
use Symfony\Component\Messenger\Transport\Serialization\Serializer; | ||
use Symfony\Component\Serializer as SerializerComponent; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
|
||
class AmazonSqsReceiverTest extends TestCase | ||
{ | ||
public function testItReturnsTheDecodedMessageToTheHandler() | ||
{ | ||
$serializer = $this->createSerializer(); | ||
|
||
$sqsEnvelop = $this->createSqsEnvelope(); | ||
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); | ||
$connection->method('get')->willReturn($sqsEnvelop); | ||
|
||
$receiver = new AmazonSqsReceiver($connection, $serializer); | ||
$actualEnvelopes = iterator_to_array($receiver->get()); | ||
$this->assertCount(1, $actualEnvelopes); | ||
$this->assertEquals(new DummyMessage('Hi'), $actualEnvelopes[0]->getMessage()); | ||
} | ||
|
||
public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException() | ||
{ | ||
$this->expectException(MessageDecodingFailedException::class); | ||
|
||
$serializer = $this->createMock(PhpSerializer::class); | ||
$serializer->method('decode')->willThrowException(new MessageDecodingFailedException()); | ||
|
||
$sqsEnvelop = $this->createSqsEnvelope(); | ||
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); | ||
$connection->method('get')->willReturn($sqsEnvelop); | ||
$connection->expects($this->once())->method('delete'); | ||
|
||
$receiver = new AmazonSqsReceiver($connection, $serializer); | ||
iterator_to_array($receiver->get()); | ||
} | ||
|
||
private function createSqsEnvelope() | ||
{ | ||
return [ | ||
'id' => 1, | ||
'body' => '{"message": "Hi"}', | ||
'headers' => [ | ||
'type' => DummyMessage::class, | ||
], | ||
]; | ||
} | ||
|
||
private function createSerializer(): Serializer | ||
{ | ||
$serializer = new Serializer( | ||
new SerializerComponent\Serializer([new ObjectNormalizer()], ['json' => new JsonEncoder()]) | ||
); | ||
|
||
return $serializer; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsSenderTest.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,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsSender; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; | ||
|
||
class AmazonSqsSenderTest extends TestCase | ||
{ | ||
public function testSend() | ||
{ | ||
$envelope = new Envelope(new DummyMessage('Oy')); | ||
$encoded = ['body' => '...', 'headers' => ['type' => DummyMessage::class]]; | ||
|
||
$connection = $this->getMockBuilder(Connection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$connection->expects($this->once())->method('send')->with($encoded['body'], $encoded['headers']); | ||
|
||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(); | ||
$serializer->method('encode')->with($envelope)->willReturnOnConsecutiveCalls($encoded); | ||
|
||
$sender = new AmazonSqsSender($connection, $serializer); | ||
$sender->send($envelope); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ny/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportFactoryTest.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,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory; | ||
|
||
class AmazonSqsTransportFactoryTest extends TestCase | ||
{ | ||
public function testSupportsOnlySqsTransports() | ||
{ | ||
$factory = new AmazonSqsTransportFactory(); | ||
|
||
$this->assertTrue($factory->supports('sqs://localhost', [])); | ||
$this->assertFalse($factory->supports('redis://localhost', [])); | ||
$this->assertFalse($factory->supports('invalid-dsn', [])); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Messenger/Bridge/AmazonSqs/Tests/Transport/AmazonSqsTransportTest.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,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Transport; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransport; | ||
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; | ||
use Symfony\Component\Messenger\Transport\TransportInterface; | ||
|
||
class AmazonSqsTransportTest extends TestCase | ||
{ | ||
public function testItIsATransport() | ||
{ | ||
$transport = $this->getTransport(); | ||
|
||
$this->assertInstanceOf(TransportInterface::class, $transport); | ||
} | ||
|
||
public function testReceivesMessages() | ||
{ | ||
$transport = $this->getTransport( | ||
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock(), | ||
$connection = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock() | ||
); | ||
|
||
$decodedMessage = new DummyMessage('Decoded.'); | ||
|
||
$sqsEnvelope = [ | ||
'id' => '5', | ||
'body' => 'body', | ||
'headers' => ['my' => 'header'], | ||
]; | ||
|
||
$serializer->method('decode')->with(['body' => 'body', 'headers' => ['my' => 'header']])->willReturn(new Envelope($decodedMessage)); | ||
$connection->method('get')->willReturn($sqsEnvelope); | ||
|
||
$envelopes = iterator_to_array($transport->get()); | ||
$this->assertSame($decodedMessage, $envelopes[0]->getMessage()); | ||
} | ||
|
||
private function getTransport(SerializerInterface $serializer = null, Connection $connection = null) | ||
{ | ||
$serializer = $serializer ?: $this->getMockBuilder(SerializerInterface::class)->getMock(); | ||
$connection = $connection ?: $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock(); | ||
|
||
return new AmazonSqsTransport($connection, $serializer); | ||
} | ||
} |
Oops, something went wrong.