Skip to content

Commit f4c77f6

Browse files
committed
Add test to assert queue failed driver works
1 parent 454a592 commit f4c77f6

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<env name="MAIL_DRIVER" value="log"/>
2424
<env name="QUEUE_DRIVER" value="cloudtasks"/>
2525
<env name="GOOGLE_APPLICATION_CREDENTIALS" value="./tests/Support/gcloud-key-valid.json" />
26+
<env name="DB_CONNECTION" value="sqlite" />
2627
</php>
2728

2829
<filter>

tests/TaskHandlerTest.php

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Google\Cloud\Tasks\V2\CloudTasksClient;
88
use Illuminate\Cache\Events\CacheHit;
99
use Illuminate\Cache\Events\KeyWritten;
10+
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\Event;
1112
use Illuminate\Support\Facades\Mail;
1213
use Mockery;
@@ -45,8 +46,22 @@ protected function setUp(): void
4546
$googlePublicKey->shouldReceive('getPublicKey')->andReturnNull();
4647
$googlePublicKey->shouldReceive('getKidFromOpenIdToken')->andReturnNull();
4748

49+
$cloudTasksClient = Mockery::mock(new CloudTasksClient());
50+
51+
// Ensure we don't fetch the Queue name and attempts each test...
52+
$cloudTasksClient->shouldReceive('queueName')->andReturn('my-queue');
53+
$cloudTasksClient->shouldReceive('getQueue')->andReturn(new class {
54+
public function getRetryConfig() {
55+
return new class {
56+
public function getMaxAttempts() {
57+
return 3;
58+
}
59+
};
60+
}
61+
});
62+
4863
$this->handler = new TaskHandler(
49-
new CloudTasksClient(),
64+
$cloudTasksClient,
5065
request(),
5166
$this->jwt,
5267
$googlePublicKey
@@ -66,21 +81,6 @@ public function it_needs_an_authorization_header()
6681
$this->handler->handle();
6782
}
6883

69-
/** @test */
70-
public function the_authorization_header_must_contain_a_valid_gcloud_token()
71-
{
72-
request()->headers->add([
73-
'Authorization' => 'Bearer 123',
74-
]);
75-
76-
$this->expectException(CloudTasksException::class);
77-
$this->expectExceptionMessage('Could not decode incoming task');
78-
79-
$this->handler->handle();
80-
81-
// @todo - test with a valid token, not sure how to do that right now
82-
}
83-
8484
/** @test */
8585
public function it_will_validate_the_token_iss()
8686
{
@@ -144,8 +144,46 @@ public function it_runs_the_incoming_job()
144144
Mail::assertSent(TestMailable::class);
145145
}
146146

147+
/** @test */
148+
public function after_max_attempts_it_will_log_to_failed_table()
149+
{
150+
$this->request->headers->add(['X-Cloudtasks-Queuename' => 'my-queue']);
151+
152+
$this->request->headers->add(['X-CloudTasks-TaskRetryCount' => 1]);
153+
try {
154+
$this->handler->handle($this->failingJob());
155+
} catch (\Throwable $e) {
156+
//
157+
}
158+
159+
$this->assertCount(0, DB::table('failed_jobs')->get());
160+
161+
$this->request->headers->add(['X-CloudTasks-TaskRetryCount' => 2]);
162+
try {
163+
$this->handler->handle($this->failingJob());
164+
} catch (\Throwable $e) {
165+
//
166+
}
167+
168+
$this->assertDatabaseHas('failed_jobs', [
169+
'connection' => 'cloudtasks',
170+
'queue' => 'my-queue',
171+
'payload' => rtrim($this->failingJobPayload()),
172+
]);
173+
}
174+
147175
private function simpleJob()
148176
{
149177
return json_decode(file_get_contents(__DIR__ . '/Support/test-job-payload.json'), true);
150178
}
179+
180+
private function failingJobPayload()
181+
{
182+
return file_get_contents(__DIR__ . '/Support/failing-job-payload.json');
183+
}
184+
185+
private function failingJob()
186+
{
187+
return json_decode(file_get_contents(__DIR__ . '/Support/failing-job-payload.json'), true);
188+
}
151189
}

tests/TestCase.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,30 @@
22

33
namespace Tests;
44

5-
use Illuminate\Support\Facades\Artisan;
6-
75
class TestCase extends \Orchestra\Testbench\TestCase
86
{
7+
public static $migrated = false;
8+
9+
protected function setUp()
10+
{
11+
parent::setUp();
12+
13+
// There is probably a more sane way to do this
14+
if (!static::$migrated) {
15+
unlink(database_path('database.sqlite'));
16+
touch(database_path('database.sqlite'));
17+
18+
foreach(glob(database_path('migrations/*.php')) as $file) {
19+
unlink($file);
20+
}
21+
22+
$this->artisan('queue:failed-table');
23+
$this->artisan('migrate');
24+
25+
static::$migrated = true;
26+
}
27+
}
28+
929
/**
1030
* Get package providers. At a minimum this is the package being tested, but also
1131
* would include packages upon which our package depends, e.g. Cartalyst/Sentry
@@ -45,6 +65,9 @@ protected function getEnvironmentSetUp($app)
4565
'handler' => 'https://localhost/my-handler',
4666
'service_account_email' => '[email protected]',
4767
]);
68+
69+
$app['config']->set('database.default', 'sqlite');
70+
$app['config']->set('database.connections.sqlite.database', database_path('database.sqlite'));
4871
}
4972

5073
protected function setConfigValue($key, $value)

0 commit comments

Comments
 (0)