Skip to content

Commit 61ad790

Browse files
abhihyderTofayel Hyder AbhiKyon147
authored
configure job connections (#323)
* configure job connections * write test case in existing test file * linting --------- Co-authored-by: Tofayel Hyder Abhi <[email protected]> Co-authored-by: Luke Walsh <[email protected]>
1 parent 3dc5e3a commit 61ad790

File tree

9 files changed

+196
-3
lines changed

9 files changed

+196
-3
lines changed

src/Actions/AfterAuthorize.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public function __invoke(ShopIdValue $shopId): bool
5858
} else {
5959
// Run later
6060
$job::dispatch($shop)
61+
->onConnection(Util::getShopifyConfig('job_connections')['after_authenticate'])
6162
->onQueue(Util::getShopifyConfig('job_queues')['after_authenticate']);
6263
}
6364

src/Actions/DispatchScripts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool
6969
($this->jobClass)::dispatch(
7070
$shop->getId(),
7171
$scripttags
72-
)->onQueue(Util::getShopifyConfig('job_queues')['scripttags']);
72+
)->onConnection(Util::getShopifyConfig('job_connections')['scripttags'])
73+
->onQueue(Util::getShopifyConfig('job_queues')['scripttags']);
7374
}
7475

7576
return true;

src/Actions/DispatchWebhooks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool
6969
($this->jobClass)::dispatch(
7070
$shop->getId(),
7171
$webhooks
72-
)->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
72+
)->onConnection(Util::getShopifyConfig('job_connections')['webhooks'])
73+
->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
7374
}
7475

7576
return true;

src/Traits/WebhookController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function handle(string $type, Request $request): ResponseResponse
3535
$jobClass::dispatch(
3636
$request->header('x-shopify-shop-domain'),
3737
$jobData
38-
)->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
38+
)->onConnection(Util::getShopifyConfig('job_connections')['webhooks'])
39+
->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
3940

4041
return Response::make('', ResponseResponse::HTTP_CREATED);
4142
}

src/resources/config/shopify-app.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,21 @@
468468
'scripttags' => env('SCRIPTTAGS_JOB_QUEUE', null),
469469
'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_QUEUE', null),
470470
],
471+
/*
472+
|--------------------------------------------------------------------------
473+
| Job Connections
474+
|--------------------------------------------------------------------------
475+
|
476+
| This option is for setting a specific job connection for webhooks, scripttags
477+
| and after_authenticate_job.
478+
|
479+
*/
471480

481+
'job_connections' => [
482+
'webhooks' => env('WEBHOOKS_JOB_CONNECTION', null),
483+
'scripttags' => env('SCRIPTTAGS_JOB_CONNECTION', null),
484+
'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_CONNECTION', null),
485+
],
472486
/*
473487
|--------------------------------------------------------------------------
474488
| Config API Callback

tests/Actions/AfterAuthorizeTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,47 @@ public function testRunDispatch(): void
5353
Queue::assertPushed($jobClass);
5454
}
5555

56+
public function testRunDispatchCustomConnection(): void
57+
{
58+
// Fake the queue
59+
Queue::fake();
60+
61+
// Create the config
62+
$jobClass = AfterAuthorizeJob::class;
63+
$this->app['config']->set('shopify-app.after_authenticate_job', [
64+
[
65+
'job' => $jobClass,
66+
'inline' => false,
67+
],
68+
[
69+
'job' => $jobClass,
70+
'inline' => false,
71+
],
72+
]);
73+
74+
// Define the custom job connection
75+
$customConnection = 'custom_connection';
76+
77+
// Set up the configuration
78+
$this->app['config']->set('shopify-app.job_connections', [
79+
'after_authenticate' => $customConnection,
80+
]);
81+
82+
// Create the shop
83+
$shop = factory($this->model)->create();
84+
85+
// Run
86+
call_user_func(
87+
$this->action,
88+
$shop->getId()
89+
);
90+
91+
// Assert the job was pushed with the correct connection
92+
Queue::assertPushed($jobClass, function ($job) use ($customConnection) {
93+
return $job->connection === $customConnection;
94+
});
95+
}
96+
5697
public function testRunInline(): void
5798
{
5899
// Create the config

tests/Actions/DispatchScriptsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,48 @@ public function testRunDispatch(): void
7272
$this->assertTrue($result);
7373
}
7474

75+
public function testRunDispatchCustomConnection(): void
76+
{
77+
// Fake the queue
78+
Queue::fake();
79+
80+
// Create the config
81+
$this->app['config']->set('shopify-app.scripttags', [
82+
[
83+
'src' => 'https://js-aplenty.com/foo.js',
84+
],
85+
]);
86+
87+
// Define the custom job connection
88+
$customConnection = 'custom_connection';
89+
90+
// Set up the configuration
91+
$this->app['config']->set('shopify-app.job_connections', [
92+
'scripttags' => $customConnection,
93+
]);
94+
95+
// Setup API stub
96+
$this->setApiStub();
97+
ApiStub::stubResponses(['get_script_tags']);
98+
99+
// Create the shop
100+
$shop = factory($this->model)->create();
101+
102+
// Run
103+
$result = call_user_func(
104+
$this->action,
105+
$shop->getId(),
106+
false // async
107+
);
108+
109+
// Assert the job was pushed with the correct connection
110+
Queue::assertPushed(ScripttagInstaller::class, function ($job) use ($customConnection) {
111+
return $job->connection === $customConnection;
112+
});
113+
114+
$this->assertTrue($result);
115+
}
116+
75117
public function testRunDispatchNow(): void
76118
{
77119
// Fake the queue

tests/Actions/DispatchWebhooksTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,53 @@ public function testRunDispatch(): void
7777
$this->assertTrue($result);
7878
}
7979

80+
public function testRunDispatchCustomConnection(): void
81+
{
82+
// Fake the queue
83+
Queue::fake();
84+
85+
// Create the config
86+
$this->app['config']->set('shopify-app.webhooks', [
87+
[
88+
'topic' => 'orders/create',
89+
'address' => 'https://localhost/webhooks/orders-create',
90+
],
91+
[
92+
'topic' => 'app/uninstalled',
93+
'address' => 'http://apple.com/uninstall',
94+
],
95+
]);
96+
97+
// Define the custom job connection
98+
$customConnection = 'custom_connection';
99+
100+
// Set up the configuration
101+
$this->app['config']->set('shopify-app.job_connections', [
102+
'webhooks' => $customConnection,
103+
]);
104+
105+
// Setup API stub
106+
$this->setApiStub();
107+
ApiStub::stubResponses(['get_webhooks']);
108+
109+
// Create the shop
110+
$shop = factory($this->model)->create();
111+
112+
// Run
113+
$result = call_user_func(
114+
$this->action,
115+
$shop->getId(),
116+
false // async
117+
);
118+
119+
// Assert the job was pushed with the correct connection
120+
Queue::assertPushed(WebhookInstaller::class, function ($job) use ($customConnection) {
121+
return $job->connection === $customConnection;
122+
});
123+
124+
$this->assertTrue($result);
125+
}
126+
80127
public function testRunDispatchNow(): void
81128
{
82129
// Fake the queue

tests/Traits/WebhookControllerTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,51 @@ public function testHandleWithCustomClassMapping(): void
9898
);
9999
}
100100

101+
public function testHandleDispatchesJobWithCustomConnection(): void
102+
{
103+
// Fake the queue
104+
Queue::fake();
105+
106+
// Extend Job::class into a custom class
107+
$shop = factory($this->model)->create(['name' => 'example.myshopify.com']);
108+
109+
// Define the custom job connection
110+
$customConnection = 'custom_connection';
111+
112+
// Set up the configuration
113+
$this->app['config']->set('shopify-app.job_connections', [
114+
'webhooks' => $customConnection,
115+
]);
116+
117+
// Mock headers that match Shopify
118+
$headers = [
119+
'HTTP_CONTENT_TYPE' => 'application/json',
120+
'HTTP_X_SHOPIFY_SHOP_DOMAIN' => $shop->name,
121+
'HTTP_X_SHOPIFY_HMAC_SHA256' => 'hvTE9wpDzMcDnPEuHWvYZ58ElKn5vHs0LomurfNIuUc=', // Matches fixture data and API secret
122+
];
123+
124+
// Create a webhook call and pass in our own headers and data
125+
$response = $this->call(
126+
'post',
127+
'/webhook/orders-create-example',
128+
[],
129+
[],
130+
[],
131+
$headers,
132+
file_get_contents(__DIR__.'/../fixtures/webhook.json')
133+
);
134+
135+
// Check it was created and job was pushed
136+
$response->assertStatus(Response::HTTP_CREATED);
137+
$response->assertStatus(201);
138+
139+
140+
// Assert the job was pushed with the correct connection
141+
Queue::assertPushed(OrdersCreateJob::class, function ($job) use ($customConnection) {
142+
return $job->connection === $customConnection;
143+
});
144+
}
145+
101146
/**
102147
* Override the default config
103148
* Allow config change to persist when using $this->call()

0 commit comments

Comments
 (0)