diff --git a/Tests/Transport/GpsConfigurationTest.php b/Tests/Transport/GpsConfigurationTest.php index 61f92c3..360302f 100644 --- a/Tests/Transport/GpsConfigurationTest.php +++ b/Tests/Transport/GpsConfigurationTest.php @@ -39,9 +39,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -53,9 +53,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( 'something', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'something', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -67,9 +67,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( 'topic_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'subscription_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -81,9 +81,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( 'topic_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'subscription_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, ['apiEndpoint' => 'https://europe-west3-pubsub.googleapis.com'], ['labels' => ['label_topic1']], ['labels' => ['label_subscription1'], 'enableMessageOrdering' => true, 'ackDeadlineSeconds' => 100], @@ -97,9 +97,9 @@ public static function dataProvider(): array ], 'expectedConfiguration' => new GpsConfiguration( 'something', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'something', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -114,9 +114,9 @@ public static function dataProvider(): array ], 'expectedConfiguration' => new GpsConfiguration( 'topic_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'subscription_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -147,9 +147,9 @@ public static function dataProvider(): array ], 'expectedConfiguration' => new GpsConfiguration( 'topic_name1', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, 'subscription_name', - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, ['apiEndpoint' => 'https://europe-west3-pubsub.googleapis.com'], ['labels' => ['label_topic1']], ['labels' => ['label_subscription1'], 'enableMessageOrdering' => true, 'ackDeadlineSeconds' => 100], @@ -161,9 +161,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -175,9 +175,9 @@ public static function dataProvider(): array 'options' => [], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -196,9 +196,9 @@ public static function dataProvider(): array ], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -217,9 +217,9 @@ public static function dataProvider(): array ], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], @@ -228,30 +228,95 @@ public static function dataProvider(): array ], 'Subscription is not created' => [ 'dsn' => 'gps://default', - 'enableCreation' => false, + 'options' => [ + 'subscription' => [ + 'createIfNotExist' => false + ], + ], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, false, [], [], [], - ['maxMessages' => 5, 'returnImmediately' => true] + ['maxMessages' => 10, 'returnImmediately' => false] ), ], 'Topic is not created' => [ 'dsn' => 'gps://default', - 'enableCreation' => false, + 'options' => [ + 'topic' => [ + 'createIfNotExist' => false + ], + ], 'expectedConfiguration' => new GpsConfiguration( GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, false, GpsConfigurationResolverInterface::DEFAULT_TOPIC_NAME, - GpsConfigurationResolverInterface::CREATION_ENABLE, + true, [], [], [], - ['maxMessages' => 5, 'returnImmediately' => true] + ['maxMessages' => 10, 'returnImmediately' => false] + ), + ], + 'DSN: Subscription is not created' => [ + 'dsn' => 'gps://default?topic[name]=foo&subscription[name]=bar&subscription[createIfNotExist]=false', + 'options' => [], + //&topic[createIfNotExist]=true + '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] ), ], ]; diff --git a/Tests/Transport/GpsTransportTest.php b/Tests/Transport/GpsTransportTest.php index 16ac378..3d3b7c5 100644 --- a/Tests/Transport/GpsTransportTest.php +++ b/Tests/Transport/GpsTransportTest.php @@ -134,6 +134,16 @@ public function testSetup() ->method('getSubscriptionName') ->willReturn($queue); + $this->gpsConfiguration + ->expects($this->atLeast(1)) + ->method('createTopicIfNotExist') + ->willReturn(true); + + $this->gpsConfiguration + ->expects($this->atLeast(1)) + ->method('createSubscriptionIfNotExist') + ->willReturn(true); + $topicMock = $this->createMock(Topic::class); $topicMock ->expects($this->once()) diff --git a/Transport/GpsConfiguration.php b/Transport/GpsConfiguration.php index 2d282b4..e861465 100644 --- a/Transport/GpsConfiguration.php +++ b/Transport/GpsConfiguration.php @@ -10,9 +10,9 @@ final class GpsConfiguration implements GpsConfigurationInterface { private string $topicName; - private bool $isTopicEnabled; + private bool $createTopicIfNotExist; private string $subscriptionName; - private bool $isSubscriptionEnabled; + private bool $createSubscriptionIfNotExist; private array $clientConfig; private array $topicOptions; private array $subscriptionOptions; @@ -20,18 +20,18 @@ final class GpsConfiguration implements GpsConfigurationInterface public function __construct( string $queueName, - bool $isTopicEnabled, + bool $isTopicEnabled, string $subscriptionName, - bool $isSubscriptionEnabled, - array $clientConfig, - array $topicOptions, - array $subscriptionOptions, - array $subscriptionPullOptions + bool $isSubscriptionEnabled, + array $clientConfig, + array $topicOptions, + array $subscriptionOptions, + array $subscriptionPullOptions ) { $this->topicName = $queueName; - $this->isTopicEnabled = $isTopicEnabled; + $this->createTopicIfNotExist = $isTopicEnabled; $this->subscriptionName = $subscriptionName; - $this->isSubscriptionEnabled = $isSubscriptionEnabled; + $this->createSubscriptionIfNotExist = $isSubscriptionEnabled; $this->clientConfig = $clientConfig; $this->topicOptions = $topicOptions; $this->subscriptionOptions = $subscriptionOptions; @@ -43,9 +43,9 @@ public function getTopicName(): string return $this->topicName; } - public function isTopicEnabled(): bool + public function createTopicIfNotExist(): bool { - return $this->isTopicEnabled; + return $this->createTopicIfNotExist; } public function getSubscriptionName(): string @@ -53,9 +53,9 @@ public function getSubscriptionName(): string return $this->subscriptionName; } - public function isSubscriptionEnabled(): bool + public function createSubscriptionIfNotExist(): bool { - return $this->isSubscriptionEnabled; + return $this->createSubscriptionIfNotExist; } public function getClientConfig(): array diff --git a/Transport/GpsConfigurationInterface.php b/Transport/GpsConfigurationInterface.php index bd98931..2a4d221 100644 --- a/Transport/GpsConfigurationInterface.php +++ b/Transport/GpsConfigurationInterface.php @@ -15,11 +15,11 @@ interface GpsConfigurationInterface { public function getTopicName(): string; - public function isTopicEnabled(): bool; + public function createTopicIfNotExist(): bool; public function getSubscriptionName(): string; - public function isSubscriptionEnabled(): bool; + public function createSubscriptionIfNotExist(): bool; /** * @see PubSubClient constructor options diff --git a/Transport/GpsConfigurationResolver.php b/Transport/GpsConfigurationResolver.php index fe79389..e267bc3 100644 --- a/Transport/GpsConfigurationResolver.php +++ b/Transport/GpsConfigurationResolver.php @@ -16,11 +16,11 @@ final class GpsConfigurationResolver implements GpsConfigurationResolverInterfac private const BOOL_NORMALIZER_KEY = 'bool'; private const NORMALIZABLE_SUBSCRIPTION_OPTIONS = [ self::INT_NORMALIZER_KEY => ['ackDeadlineSeconds', 'maxDeliveryAttempts'], - self::BOOL_NORMALIZER_KEY => ['enableMessageOrdering', 'retainAckedMessages', 'enableExactlyOnceDelivery', 'enableCreation'], + self::BOOL_NORMALIZER_KEY => ['enableMessageOrdering', 'retainAckedMessages', 'enableExactlyOnceDelivery', 'createIfNotExist'], ]; private const NORMALIZABLE_SUBSCRIPTION_PULL_OPTIONS = [ self::INT_NORMALIZER_KEY => ['maxMessages'], - self::BOOL_NORMALIZER_KEY => ['returnImmediately', 'enableCreation'], + self::BOOL_NORMALIZER_KEY => ['returnImmediately', 'createIfNotExist'], ]; /** @@ -108,9 +108,10 @@ function (OptionsResolver $resolver, Options $parentOptions) use ($subscriptionO ->setDefault('topic', function (OptionsResolver $topicResolver): void { $topicResolver ->setDefault('name', self::DEFAULT_TOPIC_NAME) - ->setDefault('enableCreation', true) + ->setDefault('createIfNotExist', true) ->setDefault('options', []) ->setAllowedTypes('name', 'string') + ->setAllowedTypes('createIfNotExist', 'bool') ->setAllowedTypes('options', 'array') ; }) @@ -120,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', @@ -131,6 +133,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void { } ) ->setAllowedTypes('name', 'string') + ->setAllowedTypes('createIfNotExist', 'bool') ->setAllowedTypes('options', 'array') ->setAllowedTypes('pull', 'array') ->setNormalizer('pull', $subscriptionPullOptionsNormalizer) @@ -141,7 +144,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void { $resolver ->setDefault('name', $parentOptions['topic']['name']) - ->setDefault('enableCreation', true) + ->setDefault('createIfNotExist', true) ->setDefault('options', []) ->setDefault( 'pull', @@ -153,6 +156,7 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void { } ) ->setAllowedTypes('name', 'string') + ->setAllowedTypes('createIfNotExist', 'bool') ->setAllowedTypes('options', 'array') ->setAllowedTypes('pull', 'array') ->setNormalizer('options', $subscriptionOptionsNormalizer) @@ -167,9 +171,9 @@ function (OptionsResolver $pullResolver) use ($parentOptions): void { return new GpsConfiguration( $resolvedOptions['topic']['name'], - $resolvedOptions['topic']['enableCreation'], + $resolvedOptions['topic']['createIfNotExist'], $resolvedOptions['subscription']['name'], - $resolvedOptions['subscription']['enableCreation'], + $resolvedOptions['subscription']['createIfNotExist'], $resolvedOptions['client_config'], $resolvedOptions['topic']['options'], $resolvedOptions['subscription']['options'], @@ -192,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; + } } diff --git a/Transport/GpsConfigurationResolverInterface.php b/Transport/GpsConfigurationResolverInterface.php index e7d48ac..9c771b7 100644 --- a/Transport/GpsConfigurationResolverInterface.php +++ b/Transport/GpsConfigurationResolverInterface.php @@ -8,7 +8,6 @@ interface GpsConfigurationResolverInterface { public const DEFAULT_TOPIC_NAME = 'messages'; public const DEFAULT_MAX_MESSAGES_PULL = 10; - public const CREATION_ENABLE = true; public function resolve(string $dsn, array $options): GpsConfigurationInterface; } diff --git a/Transport/GpsTransport.php b/Transport/GpsTransport.php index e4ac485..23c469b 100644 --- a/Transport/GpsTransport.php +++ b/Transport/GpsTransport.php @@ -91,7 +91,7 @@ public function setup(): void { $topic = $this->pubSubClient->topic($this->gpsConfiguration->getTopicName()); - if (false === $topic->exists() && $this->gpsConfiguration->isTopicEnabled()) { + if ($this->gpsConfiguration->createTopicIfNotExist() && false === $topic->exists()) { $topic = $this->pubSubClient->createTopic( $this->gpsConfiguration->getTopicName(), $this->gpsConfiguration->getTopicOptions() @@ -100,7 +100,7 @@ public function setup(): void $subscription = $topic->subscription($this->gpsConfiguration->getSubscriptionName()); - if (false === $subscription->exists() && $this->gpsConfiguration->isSubscriptionEnabled()) { + if ($this->gpsConfiguration->createSubscriptionIfNotExist() && false === $subscription->exists()) { $topic->subscribe( $this->gpsConfiguration->getSubscriptionName(), $this->gpsConfiguration->getSubscriptionOptions()