Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mandatory topic subscription creation #30

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
128 changes: 128 additions & 0 deletions Tests/Transport/GpsConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
Expand All @@ -51,7 +53,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'something',
true,
'something',
true,
[],
[],
[],
Expand All @@ -63,7 +67,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'topic_name',
true,
'subscription_name',
true,
[],
[],
[],
Expand All @@ -75,7 +81,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'topic_name',
true,
'subscription_name',
true,
['apiEndpoint' => 'https://europe-west3-pubsub.googleapis.com'],
['labels' => ['label_topic1']],
['labels' => ['label_subscription1'], 'enableMessageOrdering' => true, 'ackDeadlineSeconds' => 100],
Expand All @@ -89,7 +97,9 @@ public static function dataProvider(): array
],
'expectedConfiguration' => new GpsConfiguration(
'something',
true,
'something',
true,
[],
[],
[],
Expand All @@ -104,7 +114,9 @@ public static function dataProvider(): array
],
'expectedConfiguration' => new GpsConfiguration(
'topic_name',
true,
'subscription_name',
true,
[],
[],
[],
Expand Down Expand Up @@ -135,7 +147,9 @@ public static function dataProvider(): array
],
'expectedConfiguration' => new GpsConfiguration(
'topic_name1',
true,
'subscription_name',
true,
['apiEndpoint' => 'https://europe-west3-pubsub.googleapis.com'],
['labels' => ['label_topic1']],
['labels' => ['label_subscription1'], 'enableMessageOrdering' => true, 'ackDeadlineSeconds' => 100],
Expand All @@ -147,7 +161,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
Expand All @@ -159,7 +175,9 @@ public static function dataProvider(): array
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
Expand All @@ -178,7 +196,9 @@ public static function dataProvider(): array
],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
Expand All @@ -197,13 +217,121 @@ public static function dataProvider(): array
],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
['maxMessages' => 5, 'returnImmediately' => true]
),
],
'Subscription is not created' => [
'dsn' => 'gps://default',
'options' => [
'subscription' => [
'createIfNotExist' => false
],
],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
false,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'Topic is not created' => [
'dsn' => 'gps://default',
'options' => [
'topic' => [
'createIfNotExist' => false
],
],
'expectedConfiguration' => new GpsConfiguration(
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
false,
GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME,
true,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'DSN: Subscription is not created' => [
'dsn' => 'gps://default?topic[name]=foo&subscription[name]=bar&subscription[createIfNotExist]=false',
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'foo',
true,
'bar',
false,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'DSN: Subscription is not created #2' => [
'dsn' => 'gps://default?topic[name]=foo&topic[createIfNotExist]=true&subscription[name]=bar&subscription[createIfNotExist]=false',
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'foo',
true,
'bar',
false,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'DSN: Topic is not created' => [
'dsn' => 'gps://default?topic[name]=foo&topic[createIfNotExist]=false&subscription[name]=bar&subscription[createIfNotExist]=true',
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'foo',
false,
'bar',
true,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'DSN: Topic is not created #2' => [
'dsn' => 'gps://default?topic[name]=foo&topic[createIfNotExist]=false&subscription[name]=bar',
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'foo',
false,
'bar',
true,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
'DSN: createIfNotExist contains invalid value' => [
'dsn' => 'gps://default?topic[name]=foo&topic[createIfNotExist]=quux&subscription[name]=bar',
'options' => [],
'expectedConfiguration' => new GpsConfiguration(
'foo',
true,
'bar',
true,
[],
[],
[],
['maxMessages' => 10, 'returnImmediately' => false]
),
],
];
}
}
10 changes: 10 additions & 0 deletions Tests/Transport/GpsTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public function testSetup()
->method('getSubscriptionName')
->willReturn($queue);

$this->gpsConfiguration
->expects($this->atLeast(1))
->method('isTopicCreationEnabled')
->willReturn(true);

$this->gpsConfiguration
->expects($this->atLeast(1))
->method('isSubscriptionCreationEnabled')
->willReturn(true);

$topicMock = $this->createMock(Topic::class);
$topicMock
->expects($this->once())
Expand Down
24 changes: 20 additions & 4 deletions Transport/GpsConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
final class GpsConfiguration implements GpsConfigurationInterface
{
private string $topicName;
private bool $topicCreationEnabled;
private string $subscriptionName;
private array $clientConfig;
private array $topicOptions;
private array $subscriptionOptions;
private array $subscriptionPullOptions;
private bool $subscriptionCreationEnabled;
private array $clientConfig;
private array $topicOptions;
private array $subscriptionOptions;
private array $subscriptionPullOptions;

public function __construct(
string $queueName,
bool $topicCreationEnabled,
string $subscriptionName,
bool $subscriptionCreationEnabled,
array $clientConfig,
array $topicOptions,
array $subscriptionOptions,
array $subscriptionPullOptions
) {
$this->topicName = $queueName;
$this->topicCreationEnabled = $topicCreationEnabled;
$this->subscriptionName = $subscriptionName;
$this->subscriptionCreationEnabled = $subscriptionCreationEnabled;
$this->clientConfig = $clientConfig;
$this->topicOptions = $topicOptions;
$this->subscriptionOptions = $subscriptionOptions;
Expand All @@ -37,11 +43,21 @@ public function getTopicName(): string
return $this->topicName;
}

public function isTopicCreationEnabled(): bool
{
return $this->topicCreationEnabled;
}

public function getSubscriptionName(): string
{
return $this->subscriptionName;
}

public function isSubscriptionCreationEnabled(): bool
{
return $this->subscriptionCreationEnabled;
}

public function getClientConfig(): array
{
return $this->clientConfig;
Expand Down
4 changes: 4 additions & 0 deletions Transport/GpsConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ interface GpsConfigurationInterface
{
public function getTopicName(): string;

public function isTopicCreationEnabled(): bool;

public function getSubscriptionName(): string;

public function isSubscriptionCreationEnabled(): bool;

/**
* @see PubSubClient constructor options
*/
Expand Down
23 changes: 23 additions & 0 deletions Transport/GpsConfigurationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ function (OptionsResolver $resolver, Options $parentOptions) use ($subscriptionO
->setDefault('topic', function (OptionsResolver $topicResolver): void {
$topicResolver
->setDefault('name', self::DEFAULT_TOPIC_NAME)
->setDefault('createIfNotExist', true)
->setDefault('options', [])
->setAllowedTypes('name', 'string')
->setAllowedTypes('createIfNotExist', 'bool')
->setAllowedTypes('options', 'array')
;
})
Expand All @@ -119,6 +121,7 @@ function (OptionsResolver $resolver, Options $parentOptions) use ($subscriptionO
if ($parentOptions->offsetExists('queue')) {
$resolver
->setDefault('name', $parentOptions['queue']['name'])
->setDefault('createIfNotExist', true)
->setDefault('options', $parentOptions['queue']['options'])
->setDefault(
'pull',
Expand All @@ -130,6 +133,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void {
}
)
->setAllowedTypes('name', 'string')
->setAllowedTypes('createIfNotExist', 'bool')
->setAllowedTypes('options', 'array')
->setAllowedTypes('pull', 'array')
->setNormalizer('pull', $subscriptionPullOptionsNormalizer)
Expand All @@ -140,6 +144,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void {

$resolver
->setDefault('name', $parentOptions['topic']['name'])
->setDefault('createIfNotExist', true)
->setDefault('options', [])
->setDefault(
'pull',
Expand All @@ -151,6 +156,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void {
}
)
->setAllowedTypes('name', 'string')
->setAllowedTypes('createIfNotExist', 'bool')
->setAllowedTypes('options', 'array')
->setAllowedTypes('pull', 'array')
->setNormalizer('options', $subscriptionOptionsNormalizer)
Expand All @@ -165,7 +171,9 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void {

return new GpsConfiguration(
$resolvedOptions['topic']['name'],
$resolvedOptions['topic']['createIfNotExist'],
$resolvedOptions['subscription']['name'],
$resolvedOptions['subscription']['createIfNotExist'],
$resolvedOptions['client_config'],
$resolvedOptions['topic']['options'],
$resolvedOptions['subscription']['options'],
Expand All @@ -188,6 +196,21 @@ private function getMergedOptions(string $dsn, array $options): array
$dnsOptions['topic']['name'] = substr($dnsPathOption, 1);
}

if (isset($dnsOptions['topic']['createIfNotExist'])) {
$dnsOptions['topic']['createIfNotExist'] = $this->toBool($dnsOptions['topic']['createIfNotExist'], true);
}

if (isset($dnsOptions['subscription']['createIfNotExist'])) {
$dnsOptions['subscription']['createIfNotExist'] = $this->toBool($dnsOptions['subscription']['createIfNotExist'], true);
}

return array_merge($dnsOptions, $options);
}

private function toBool(string $value, bool $default): bool
{
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

return $result ?? $default;
}
}
4 changes: 2 additions & 2 deletions Transport/GpsTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function setup(): void
{
$topic = $this->pubSubClient->topic($this->gpsConfiguration->getTopicName());

if (false === $topic->exists()) {
if (true === $this->gpsConfiguration->isTopicCreationEnabled() && false === $topic->exists()) {
$topic = $this->pubSubClient->createTopic(
$this->gpsConfiguration->getTopicName(),
$this->gpsConfiguration->getTopicOptions()
Expand All @@ -100,7 +100,7 @@ public function setup(): void

$subscription = $topic->subscription($this->gpsConfiguration->getSubscriptionName());

if (false === $subscription->exists()) {
if (true === $this->gpsConfiguration->isSubscriptionCreationEnabled() && false === $subscription->exists()) {
$topic->subscribe(
$this->gpsConfiguration->getSubscriptionName(),
$this->gpsConfiguration->getSubscriptionOptions()
Expand Down
Loading