diff --git a/.styleci.yml b/.styleci.yml index 044e610..5cbce59 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -9,7 +9,6 @@ enabled: - linebreak_after_opening_tag - single_quote - no_blank_lines_after_phpdoc - - unary_operator_spaces - no_useless_else - no_useless_return - trailing_comma_in_multiline_array diff --git a/src/Queue/Driver.php b/src/Queue/Driver.php index bdf25a9..163b948 100644 --- a/src/Queue/Driver.php +++ b/src/Queue/Driver.php @@ -40,6 +40,8 @@ enum Driver: string */ case NSQ = 'nsq'; + case PubSub = 'google_pub_sub'; + /** * @internal Used when the driver is not specified. */ diff --git a/src/Queue/PubSubCreateInfo.php b/src/Queue/PubSubCreateInfo.php new file mode 100644 index 0000000..87e23cc --- /dev/null +++ b/src/Queue/PubSubCreateInfo.php @@ -0,0 +1,51 @@ +projectId !== '', 'Precondition [projectId !== ""] failed'); + \assert($this->topic !== '', 'Precondition [topic !== ""] failed'); + \assert($this->maxDeliveryAttempts >= 5, 'Precondition [maxDeliveryAttempts >= 5] failed'); + if ($this->deadLetterTopic !== null) { + \assert($this->deadLetterTopic !== '', 'Precondition [deadLetterTopic !== ""] failed'); + } + } + + public function toArray(): array + { + $result = \array_merge(parent::toArray(), [ + 'project_id' => $this->projectId, + 'topic' => $this->topic, + ]); + + if ($this->deadLetterTopic !== null && $this->deadLetterTopic !== '') { + $result['dead_letter_topic'] = $this->deadLetterTopic; + $result['max_delivery_attempts'] = $this->maxDeliveryAttempts; + } + + return $result; + } +} diff --git a/tests/Unit/Queue/PubSubCreateInfoTest.php b/tests/Unit/Queue/PubSubCreateInfoTest.php new file mode 100644 index 0000000..e263dd9 --- /dev/null +++ b/tests/Unit/Queue/PubSubCreateInfoTest.php @@ -0,0 +1,92 @@ +assertSame(Driver::PubSub, $pubSubCreateInfo->driver); + $this->assertSame('test_name', $pubSubCreateInfo->name); + $this->assertSame('test_project_id', $pubSubCreateInfo->projectId); + $this->assertSame('test_topic', $pubSubCreateInfo->topic); + $this->assertSame(3, $pubSubCreateInfo->priority); + $this->assertSame('test_dead_letter_topic', $pubSubCreateInfo->deadLetterTopic); + $this->assertSame(15, $pubSubCreateInfo->maxDeliveryAttempts); + } + + public function testCreatePubSubCreateInfoOnlyRequiredData(): void + { + $pubSubCreateInfo = new PubSubCreateInfo( + name: 'test_name', + projectId: 'test_project_id', + topic: 'test_topic', + ); + + $this->assertSame(Driver::PubSub, $pubSubCreateInfo->driver); + $this->assertSame('test_name', $pubSubCreateInfo->name); + $this->assertSame('test_project_id', $pubSubCreateInfo->projectId); + $this->assertSame('test_topic', $pubSubCreateInfo->topic); + $this->assertSame(10, $pubSubCreateInfo->priority); + $this->assertNull($pubSubCreateInfo->deadLetterTopic); + $this->assertSame(10, $pubSubCreateInfo->maxDeliveryAttempts); + } + + public function testToArray(): void + { + $pubSubCreateInfo = new PubSubCreateInfo( + name: 'test_name', + projectId: 'test_project_id', + topic: 'test_topic', + priority: 3, + deadLetterTopic: 'test_dead_letter_topic', + maxDeliveryAttempts: 15, + ); + + $expectedArray = [ + 'name' => 'test_name', + 'driver' => Driver::PubSub->value, + 'priority' => 3, + 'project_id' => 'test_project_id', + 'topic' => 'test_topic', + 'dead_letter_topic' => 'test_dead_letter_topic', + 'max_delivery_attempts' => 15, + ]; + + $this->assertSame($expectedArray, $pubSubCreateInfo->toArray()); + } + + public function testToArrayOnlyRequiredData(): void + { + $pubSubCreateInfo = new PubSubCreateInfo( + name: 'test_name', + projectId: 'test_project_id', + topic: 'test_topic', + ); + + $expectedArray = [ + 'name' => 'test_name', + 'driver' => Driver::PubSub->value, + 'priority' => 10, + 'project_id' => 'test_project_id', + 'topic' => 'test_topic', + ]; + + $this->assertSame($expectedArray, $pubSubCreateInfo->toArray()); + } +}