-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
AmazonSqsIntegrationTest.php
68 lines (56 loc) · 2.14 KB
/
AmazonSqsIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?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 AsyncAws\Sqs\SqsClient;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\Connection;
/**
* @group integration
*/
class AmazonSqsIntegrationTest extends TestCase
{
public function testConnectionSendToFifoQueueAndGet(): void
{
if (!getenv('MESSENGER_SQS_FIFO_QUEUE_DSN')) {
$this->markTestSkipped('The "MESSENGER_SQS_FIFO_QUEUE_DSN" environment variable is required.');
}
$this->execute(getenv('MESSENGER_SQS_FIFO_QUEUE_DSN'));
}
public function testConnectionSendAndGet(): void
{
if (!getenv('MESSENGER_SQS_DSN')) {
$this->markTestSkipped('The "MESSENGER_SQS_DSN" environment variable is required.');
}
$this->execute(getenv('MESSENGER_SQS_DSN'));
}
private function execute(string $dsn): void
{
$connection = Connection::fromDsn($dsn, []);
$connection->setup();
$this->clearSqs($dsn);
$connection->send('{"message": "Hi"}', ['type' => DummyMessage::class, DummyMessage::class => 'special']);
$this->assertSame(1, $connection->getMessageCount());
$wait = 0;
while ((null === $encoded = $connection->get()) && $wait++ < 200) {
usleep(5000);
}
$this->assertEquals('{"message": "Hi"}', $encoded['body']);
$this->assertEquals(['type' => DummyMessage::class, DummyMessage::class => 'special'], $encoded['headers']);
}
private function clearSqs(string $dsn): void
{
$url = parse_url($dsn);
$client = new SqsClient(['endpoint' => "http://{$url['host']}:{$url['port']}"]);
$client->purgeQueue([
'QueueUrl' => $client->getQueueUrl(['QueueName' => ltrim($url['path'], '/')])->getQueueUrl(),
]);
}
}