diff --git a/src/Traits/WebhookController.php b/src/Traits/WebhookController.php index 56b45b16..98f58817 100644 --- a/src/Traits/WebhookController.php +++ b/src/Traits/WebhookController.php @@ -24,18 +24,24 @@ public function handle(string $type, Request $request): ResponseResponse { // Get the job class and dispatch $jobClass = Util::getShopifyConfig('job_namespace').str_replace('-', '', ucwords($type, '-')).'Job'; + $jobQueue = Util::getShopifyConfig('job_queues')['webhooks']; $jobData = json_decode($request->getContent()); // If we have manually mapped a class, use that instead $config = Util::getShopifyConfig('webhooks'); + if (!empty($config[$type]['class'])) { $jobClass = $config[$type]['class']; } + if (!empty($config[$type]['queue'])) { + $jobQueue = $config[$type]['queue']; + } + $jobClass::dispatch( $request->header('x-shopify-shop-domain'), $jobData - )->onQueue(Util::getShopifyConfig('job_queues')['webhooks']); + )->onQueue($jobQueue); return Response::make('', ResponseResponse::HTTP_CREATED); } diff --git a/tests/Traits/WebhookControllerTest.php b/tests/Traits/WebhookControllerTest.php index 84d0e857..940c4595 100644 --- a/tests/Traits/WebhookControllerTest.php +++ b/tests/Traits/WebhookControllerTest.php @@ -44,7 +44,8 @@ public function testSuccess(): void Queue::assertPushed(OrdersCreateJob::class, function ($job) use ($shop) { return ShopDomain::fromNative($job->shopDomain)->isSame($shop->getDomain()) && $job->data instanceof stdClass - && $job->data->email === 'jon@doe.ca'; + && $job->data->email === 'jon@doe.ca' + && $job->queue === 'webhooks-queue'; }); } @@ -98,6 +99,41 @@ public function testHandleWithCustomClassMapping(): void ); } + public function testHandleWithCustomQueueMapping(): void + { + // Fake the queue + Queue::fake(); + + // Extend Job::class into a custom class + $shop = factory($this->model)->create(['name' => 'example.myshopify.com']); + + // Mock headers that match Shopify + $headers = [ + 'HTTP_CONTENT_TYPE' => 'application/json', + 'HTTP_X_SHOPIFY_SHOP_DOMAIN' => $shop->name, + 'HTTP_X_SHOPIFY_HMAC_SHA256' => 'hvTE9wpDzMcDnPEuHWvYZ58ElKn5vHs0LomurfNIuUc=', // Matches fixture data and API secret + ]; + + // Create a webhook call and pass in our own headers and data + $response = $this->call( + 'post', + '/webhook/orders-create-custom-queue', + [], + [], + [], + $headers, + file_get_contents(__DIR__.'/../fixtures/webhook.json') + ); + + // Check it was created and job was pushed + $response->assertStatus(Response::HTTP_CREATED); + $response->assertStatus(201); + + Queue::assertPushed(OrdersCreateJob::class, function ($job) { + return $job->queue === 'custom-queue'; + }); + } + /** * Override the default config * Allow config change to persist when using $this->call() @@ -117,6 +153,13 @@ protected function getEnvironmentSetUp($app): void 'address' => 'https://some-app.com/webhook/orders-create-example', 'class' => OrdersCreateJob::class, ]; + $webhooks['orders-create-custom-queue'] = [ + 'topic' => 'ORDERS_PAID', + 'address' => 'https://some-app.com/webhook/orders-create-example', + 'class' => OrdersCreateJob::class, + 'queue' => 'custom-queue', + ]; $app['config']->set('shopify-app.webhooks', $webhooks); + $app['config']->set('shopify-app.job_queues.webhooks', 'webhooks-queue'); } }