From e6f3e4b05cd84eb2df2db4edaab0dc116a364140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Fri, 5 Apr 2024 14:52:39 +0200 Subject: [PATCH] Made the versioning more robust --- composer.json | 1 + src/Registrar/Webhook.php | 17 +++++++++++ src/Registrar/WebhookRegistrar.php | 32 ++++++++++----------- src/Registrar/WebhookRegistrarInterface.php | 9 +++++- tests/Registrar/WebhookRegistrarTest.php | 31 ++++++++++++++++++++ 5 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 src/Registrar/Webhook.php create mode 100644 tests/Registrar/WebhookRegistrarTest.php diff --git a/composer.json b/composer.json index 9b3a8e0..f0649c4 100644 --- a/composer.json +++ b/composer.json @@ -51,6 +51,7 @@ "lexik/jwt-authentication-bundle": "^2.16", "matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.0", "nyholm/psr7": "^1.8", + "phpspec/prophecy-phpunit": "^2.2", "phpunit/phpunit": "^9.6", "psalm/plugin-phpunit": "^0.18", "psalm/plugin-symfony": "^5.1", diff --git a/src/Registrar/Webhook.php b/src/Registrar/Webhook.php new file mode 100644 index 0000000..7dd180e --- /dev/null +++ b/src/Registrar/Webhook.php @@ -0,0 +1,17 @@ +getWebhooks() as $webhook) { $this->client->webhooks()->create(new WebhookRequest( - $webhook['name'], - $webhook['endpoint'], - $webhook['key'], - $webhook['action'], - $webhook['resource'], + $webhook->name, + $webhook->endpoint, + $webhook->key, + $webhook->action, + $webhook->resource, )); } } @@ -40,17 +40,17 @@ public function register(): void public function getVersion(): string { $webhooks = iterator_to_array($this->getWebhooks()); - usort($webhooks, static fn (array $a, array $b) => $a['name'] <=> $b['name']); + usort($webhooks, static fn (Webhook $a, Webhook $b) => $a->name <=> $b->name); - $webhooks = array_map(static fn (array $webhook) => $webhook['name'] . $webhook['endpoint'] . $webhook['key'] . $webhook['action'] . $webhook['resource'], $webhooks); + $webhooks = array_map(static fn (Webhook $webhook) => $webhook->name . $webhook->endpoint . $webhook->key . $webhook->action . $webhook->resource, $webhooks); return md5(implode('', $webhooks)); } /** - * @return \Generator + * @return \Generator */ - private function getWebhooks(): \Generator + public function getWebhooks(): \Generator { $resources = [ 'Shipments' => ['create', 'cancel'], @@ -60,9 +60,9 @@ private function getWebhooks(): \Generator foreach ($resources as $resource => $actions) { foreach ($actions as $action) { - yield [ - 'name' => sprintf('%s - [%s:%s]', $this->namePrefix, u($resource)->snake()->toString(), $action), - 'endpoint' => $this->urlGenerator->generate( + yield new Webhook( + sprintf('%s - [%s:%s]', $this->namePrefix, u($resource)->snake()->toString(), $action), + $this->urlGenerator->generate( '_webhook_controller', [ 'type' => 'shipmondo', @@ -71,10 +71,10 @@ private function getWebhooks(): \Generator ], UrlGeneratorInterface::ABSOLUTE_URL, ), - 'key' => $this->webhooksKey, - 'action' => $action, - 'resource' => $resource, - ]; + $this->webhooksKey, + $action, + $resource, + ); } } } diff --git a/src/Registrar/WebhookRegistrarInterface.php b/src/Registrar/WebhookRegistrarInterface.php index 06cfaf1..442dc0b 100644 --- a/src/Registrar/WebhookRegistrarInterface.php +++ b/src/Registrar/WebhookRegistrarInterface.php @@ -12,7 +12,14 @@ interface WebhookRegistrarInterface public function register(): void; /** - * Should return a version (string) that uniquely identifies the webhooks being registered. + * Will return a list of webhooks that this registrar will register + * + * @return iterable + */ + public function getWebhooks(): iterable; + + /** + * Should return a version (string) that uniquely identifies the webhooks being registered */ public function getVersion(): string; } diff --git a/tests/Registrar/WebhookRegistrarTest.php b/tests/Registrar/WebhookRegistrarTest.php new file mode 100644 index 0000000..1c1a774 --- /dev/null +++ b/tests/Registrar/WebhookRegistrarTest.php @@ -0,0 +1,31 @@ +prophesize(ClientInterface::class); + $urlGenerator = $this->prophesize(UrlGeneratorInterface::class); + $urlGenerator->generate('_webhook_controller', Argument::type('array'), UrlGeneratorInterface::ABSOLUTE_URL)->willReturn('https://example.com/webhook'); + + $registrar = new WebhookRegistrar($client->reveal(), $urlGenerator->reveal(), 'key', 'prefix'); + + self::assertSame('49fb4dab33cd8cf27e6a9d1944dcbbdb', $registrar->getVersion()); + } +}