Skip to content

Commit a4a3098

Browse files
committed
Add tests
1 parent 74c96ad commit a4a3098

5 files changed

+157
-13
lines changed

src/DashboardService.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ public static function make(): DashboardService
2121

2222
private function getTaskBody(Task $task): string
2323
{
24-
$httpRequest = $task->getHttpRequest();
24+
$httpRequest = $task->getHttpRequest() ?: $task->getAppEngineHttpRequest();
2525

26-
if (! $httpRequest instanceof HttpRequest) {
26+
if (! $httpRequest) {
2727
throw new Exception('Task does not have a HTTP request.');
2828
}
2929

tests/CloudTasksDashboardTest.php

+48-8
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,15 @@ public function it_returns_info_about_a_specific_task()
241241

242242
/**
243243
* @test
244+
*
245+
* @testWith [{"task_type": "http"}]
246+
* [{"task_type": "appengine"}]
244247
*/
245-
public function when_a_job_is_dispatched_it_will_be_added_to_the_dashboard()
248+
public function when_a_job_is_dispatched_it_will_be_added_to_the_dashboard(array $test)
246249
{
247250
// Arrange
251+
$this->withTaskType($test['task_type']);
252+
248253
CloudTasksApi::fake();
249254
$tasksBefore = StackkitCloudTask::count();
250255
$job = $this->dispatch(new SimpleJob());
@@ -280,10 +285,15 @@ public function when_dashboard_is_disabled_jobs_will_not_be_added_to_the_dashboa
280285

281286
/**
282287
* @test
288+
*
289+
* @testWith [{"task_type": "http"}]
290+
* [{"task_type": "appengine"}]
283291
*/
284-
public function when_a_job_is_scheduled_it_will_be_added_as_such()
292+
public function when_a_job_is_scheduled_it_will_be_added_as_such(array $test)
285293
{
286294
// Arrange
295+
$this->withTaskType($test['task_type']);
296+
287297
CloudTasksApi::fake();
288298
Carbon::setTestNow(now());
289299
$tasksBefore = StackkitCloudTask::count();
@@ -305,10 +315,15 @@ public function when_a_job_is_scheduled_it_will_be_added_as_such()
305315

306316
/**
307317
* @test
318+
*
319+
* @testWith [{"task_type": "http"}]
320+
* [{"task_type": "appengine"}]
308321
*/
309-
public function when_a_job_is_running_it_will_be_updated_in_the_dashboard()
322+
public function when_a_job_is_running_it_will_be_updated_in_the_dashboard(array $test)
310323
{
311324
// Arrange
325+
$this->withTaskType($test['task_type']);
326+
312327
\Illuminate\Support\Carbon::setTestNow(now());
313328
CloudTasksApi::fake();
314329
OpenIdVerificator::fake();
@@ -331,10 +346,15 @@ public function when_a_job_is_running_it_will_be_updated_in_the_dashboard()
331346

332347
/**
333348
* @test
349+
*
350+
* @testWith [{"task_type": "http"}]
351+
* [{"task_type": "appengine"}]
334352
*/
335-
public function when_a_job_is_successful_it_will_be_updated_in_the_dashboard()
353+
public function when_a_job_is_successful_it_will_be_updated_in_the_dashboard(array $test)
336354
{
337355
// Arrange
356+
$this->withTaskType($test['task_type']);
357+
338358
\Illuminate\Support\Carbon::setTestNow(now());
339359
CloudTasksApi::fake();
340360
OpenIdVerificator::fake();
@@ -357,10 +377,15 @@ public function when_a_job_is_successful_it_will_be_updated_in_the_dashboard()
357377

358378
/**
359379
* @test
380+
*
381+
* @testWith [{"task_type": "http"}]
382+
* [{"task_type": "appengine"}]
360383
*/
361-
public function when_a_job_errors_it_will_be_updated_in_the_dashboard()
384+
public function when_a_job_errors_it_will_be_updated_in_the_dashboard(array $test)
362385
{
363386
// Arrange
387+
$this->withTaskType($test['task_type']);
388+
364389
\Illuminate\Support\Carbon::setTestNow(now());
365390
CloudTasksApi::fake();
366391
OpenIdVerificator::fake();
@@ -384,10 +409,15 @@ public function when_a_job_errors_it_will_be_updated_in_the_dashboard()
384409

385410
/**
386411
* @test
412+
*
413+
* @testWith [{"task_type": "http"}]
414+
* [{"task_type": "appengine"}]
387415
*/
388-
public function when_a_job_fails_it_will_be_updated_in_the_dashboard()
416+
public function when_a_job_fails_it_will_be_updated_in_the_dashboard(array $test)
389417
{
390418
// Arrange
419+
$this->withTaskType($test['task_type']);
420+
391421
\Illuminate\Support\Carbon::setTestNow(now());
392422
CloudTasksApi::fake();
393423
OpenIdVerificator::fake();
@@ -416,10 +446,15 @@ public function when_a_job_fails_it_will_be_updated_in_the_dashboard()
416446

417447
/**
418448
* @test
449+
*
450+
* @testWith [{"task_type": "http"}]
451+
* [{"task_type": "appengine"}]
419452
*/
420-
public function when_a_job_is_released_it_will_be_updated_in_the_dashboard()
453+
public function when_a_job_is_released_it_will_be_updated_in_the_dashboard(array $test)
421454
{
422455
// Arrange
456+
$this->withTaskType($test['task_type']);
457+
423458
\Illuminate\Support\Carbon::setTestNow(now());
424459
CloudTasksApi::fake();
425460
OpenIdVerificator::fake();
@@ -447,10 +482,15 @@ public function when_a_job_is_released_it_will_be_updated_in_the_dashboard()
447482

448483
/**
449484
* @test
485+
*
486+
* @testWith [{"task_type": "http"}]
487+
* [{"task_type": "appengine"}]
450488
*/
451-
public function job_release_delay_is_added_to_the_metadata()
489+
public function job_release_delay_is_added_to_the_metadata(array $test)
452490
{
453491
// Arrange
492+
$this->withTaskType($test['task_type']);
493+
454494
\Illuminate\Support\Carbon::setTestNow(now());
455495
CloudTasksApi::fake();
456496
OpenIdVerificator::fake();

tests/ConfigHandlerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function test_it_allows_a_handler_url_to_contain_path(string $handler, st
1414
self::assertSame($expectedHandler, Config::getHandler($handler));
1515
}
1616

17-
public function handlerDataProvider(): array
17+
public static function handlerDataProvider(): array
1818
{
1919
return [
2020
['https://example.com', 'https://example.com/handle-task'],

tests/QueueAppEngineTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Google\Cloud\Tasks\V2\Task;
8+
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
9+
use Tests\Support\SimpleJob;
10+
11+
class QueueAppEngineTest extends TestCase
12+
{
13+
protected function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->withTaskType('appengine');
18+
}
19+
20+
/**
21+
* @test
22+
*/
23+
public function an_app_engine_http_request_with_the_handler_url_is_made()
24+
{
25+
// Arrange
26+
CloudTasksApi::fake();
27+
28+
// Act
29+
$this->dispatch(new SimpleJob());
30+
31+
// Assert
32+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
33+
return $task->getAppEngineHttpRequest()->getRelativeUri() === '/handle-task';
34+
});
35+
}
36+
37+
/**
38+
* @test
39+
*/
40+
public function it_routes_to_the_service()
41+
{
42+
// Arrange
43+
CloudTasksApi::fake();
44+
45+
// Act
46+
$this->dispatch(new SimpleJob());
47+
48+
// Assert
49+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
50+
return $task->getAppEngineHttpRequest()->getAppEngineRouting()->getService() === 'api';
51+
});
52+
}
53+
54+
/**
55+
* @test
56+
*/
57+
public function it_contains_the_payload()
58+
{
59+
// Arrange
60+
CloudTasksApi::fake();
61+
62+
// Act
63+
$this->dispatch($job = new SimpleJob());
64+
65+
// Assert
66+
CloudTasksApi::assertTaskCreated(function (Task $task) use ($job): bool {
67+
$decoded = json_decode($task->getAppEngineHttpRequest()->getBody(), true);
68+
69+
return $decoded['displayName'] === SimpleJob::class
70+
&& $decoded['job'] === 'Illuminate\Queue\CallQueuedHandler@call'
71+
&& $decoded['data']['command'] === serialize($job);
72+
});
73+
}
74+
}

tests/TestCase.php

+32-2
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,20 @@ public function dispatch($job)
130130
$task = null;
131131

132132
Event::listen(TaskCreated::class, function (TaskCreated $event) use (&$payload, &$payloadAsArray, &$task) {
133-
$payload = $event->task->getHttpRequest()->getBody();
133+
$request = $event->task->getHttpRequest() ?? $event->task->getAppEngineHttpRequest();
134+
$payload = $request->getBody();
134135
$payloadAsArray = json_decode($payload, true);
135136
$task = $event->task;
136137

137138
[,,,,,,,$taskName] = explode('/', $task->getName());
138-
request()->headers->set('X-Cloudtasks-Taskname', $taskName);
139+
140+
if ($task->hasHttpRequest()) {
141+
request()->headers->set('X-Cloudtasks-Taskname', $taskName);
142+
}
143+
144+
if ($task->hasAppEngineHttpRequest()) {
145+
request()->headers->set('X-AppEngine-TaskName', $taskName);
146+
}
139147
});
140148

141149
dispatch($job);
@@ -248,4 +256,26 @@ public function getJobReleasedAfterExceptionEvent(): string
248256
? PackageJobReleasedAfterException::class
249257
: JobReleasedAfterException::class;
250258
}
259+
260+
public function withTaskType(string $taskType): void
261+
{
262+
switch ($taskType) {
263+
case 'appengine':
264+
$this->setConfigValue('handler', null);
265+
$this->setConfigValue('service_account_email', null);
266+
$this->setConfigValue('signed_audience', null);
267+
268+
$this->setConfigValue('app_engine', true);
269+
$this->setConfigValue('app_engine_service', 'api');
270+
break;
271+
case 'http':
272+
$this->setConfigValue('app_engine', false);
273+
$this->setConfigValue('app_engine_service', null);
274+
275+
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8080');
276+
$this->setConfigValue('service_account_email', '[email protected]');
277+
$this->setConfigValue('signed_audience', true);
278+
break;
279+
}
280+
}
251281
}

0 commit comments

Comments
 (0)