Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testFromDsnAsQueueUrl()
{
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
$this->assertEquals(
new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], new SqsClient(['region' => 'us-east-2', 'endpoint' => 'https://sqs.us-east-2.amazonaws.com', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], new SqsClient(['region' => 'us-east-2', 'endpoint' => 'https://sqs.us-east-2.amazonaws.com', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient), 'https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4'),
Connection::fromDsn('https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4', [], $httpClient)
);
}
Expand Down Expand Up @@ -259,4 +259,57 @@ public function testUnexpectedSqsError()
$connection = new Connection(['queue_name' => 'queue', 'account' => 123, 'auto_setup' => false], $client);
$connection->get();
}

/**
* @dataProvider provideQueueUrl
*/
public function testInjectQueueUrl(string $dsn, string $queueUrl)
{
$connection = Connection::fromDsn($dsn);

$r = new \ReflectionObject($connection);
$queueProperty = $r->getProperty('queueUrl');
$queueProperty->setAccessible(true);

$this->assertSame($queueUrl, $queueProperty->getValue($connection));
}

public function provideQueueUrl()
{
yield ['https://sqs.us-east-2.amazonaws.com/123456/queue', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
yield ['https://KEY:SECRET@sqs.us-east-2.amazonaws.com/123456/queue', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
yield ['https://sqs.us-east-2.amazonaws.com/123456/queue?auto_setup=1', 'https://sqs.us-east-2.amazonaws.com/123456/queue'];
}

/**
* @dataProvider provideNotQueueUrl
*/
public function testNotInjectQueueUrl(string $dsn)
{
$connection = Connection::fromDsn($dsn);

$r = new \ReflectionObject($connection);
$queueProperty = $r->getProperty('queueUrl');
$queueProperty->setAccessible(true);

$this->assertNull($queueProperty->getValue($connection));
}

public function provideNotQueueUrl()
{
yield ['https://sqs.us-east-2.amazonaws.com/queue'];
yield ['https://us-east-2/123456/ab1-MyQueue-A2BCDEF3GHI4'];
yield ['sqs://default/queue'];
}

public function testGetQueueUrlNotCalled()
{
$client = $this->getMockBuilder(SqsClient::class)->getMock();
$connection = new Connection(['queue_name' => 'ab1-MyQueue-A2BCDEF3GHI4', 'account' => '123456789012'], $client, 'https://sqs.us-east-2.amazonaws.com/123456789012/ab1-MyQueue-A2BCDEF3GHI4');

$client->expects($this->never())->method('getQueueUrl');
$client->expects($this->once())->method('deleteMessage');

$connection->delete('id');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ class Connection
/** @var string|null */
private $queueUrl;

public function __construct(array $configuration, SqsClient $client = null)
public function __construct(array $configuration, SqsClient $client = null, string $queueUrl = null)
{
$this->configuration = array_replace_recursive(self::DEFAULT_OPTIONS, $configuration);
$this->client = $client ?? new SqsClient([]);
$this->queueUrl = $queueUrl;
}

public function __destruct()
Expand Down Expand Up @@ -140,7 +141,18 @@ public static function fromDsn(string $dsn, array $options = [], HttpClientInter
}
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $options['account'] ?? self::DEFAULT_OPTIONS['account'];

return new self($configuration, new SqsClient($clientConfiguration, null, $client));
// When the DNS looks like a QueueUrl, we can directly inject it in the connection
// https://sqs.REGION.amazonaws.com/ACCOUNT/QUEUE
$queueUrl = null;
if (
'https' === $parsedUrl['scheme']
&& ($parsedUrl['host'] ?? 'default') === "sqs.{$clientConfiguration['region']}.amazonaws.com"
&& ($parsedUrl['path'] ?? '/') === "/{$configuration['account']}/{$configuration['queue_name']}"
) {
$queueUrl = 'https://'.$parsedUrl['host'].$parsedUrl['path'];
}

return new self($configuration, new SqsClient($clientConfiguration, null, $client), $queueUrl);
}

public function get(): ?array
Expand Down