Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/Traits/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
45 changes: 44 additions & 1 deletion tests/Traits/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '[email protected]';
&& $job->data->email === '[email protected]'
&& $job->queue === 'webhooks-queue';
});
}

Expand Down Expand Up @@ -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()
Expand All @@ -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');
}
}