7
7
use Google \Cloud \Tasks \V2 \CloudTasksClient ;
8
8
use Illuminate \Cache \Events \CacheHit ;
9
9
use Illuminate \Cache \Events \KeyWritten ;
10
+ use Illuminate \Support \Facades \DB ;
10
11
use Illuminate \Support \Facades \Event ;
11
12
use Illuminate \Support \Facades \Mail ;
12
13
use Mockery ;
@@ -45,10 +46,23 @@ protected function setUp(): void
45
46
$ googlePublicKey ->shouldReceive ('getPublicKey ' )->andReturnNull ();
46
47
$ googlePublicKey ->shouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
47
48
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
+
48
63
$ this ->handler = new TaskHandler (
49
- new CloudTasksClient () ,
64
+ $ cloudTasksClient ,
50
65
request (),
51
- $ this ->jwt ,
52
66
$ googlePublicKey
53
67
);
54
68
@@ -66,21 +80,6 @@ public function it_needs_an_authorization_header()
66
80
$ this ->handler ->handle ();
67
81
}
68
82
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
-
84
83
/** @test */
85
84
public function it_will_validate_the_token_iss ()
86
85
{
@@ -144,8 +143,46 @@ public function it_runs_the_incoming_job()
144
143
Mail::assertSent (TestMailable::class);
145
144
}
146
145
146
+ /** @test */
147
+ public function after_max_attempts_it_will_log_to_failed_table ()
148
+ {
149
+ $ this ->request ->headers ->add (['X-Cloudtasks-Queuename ' => 'my-queue ' ]);
150
+
151
+ $ this ->request ->headers ->add (['X-CloudTasks-TaskRetryCount ' => 1 ]);
152
+ try {
153
+ $ this ->handler ->handle ($ this ->failingJob ());
154
+ } catch (\Throwable $ e ) {
155
+ //
156
+ }
157
+
158
+ $ this ->assertCount (0 , DB ::table ('failed_jobs ' )->get ());
159
+
160
+ $ this ->request ->headers ->add (['X-CloudTasks-TaskRetryCount ' => 2 ]);
161
+ try {
162
+ $ this ->handler ->handle ($ this ->failingJob ());
163
+ } catch (\Throwable $ e ) {
164
+ //
165
+ }
166
+
167
+ $ this ->assertDatabaseHas ('failed_jobs ' , [
168
+ 'connection ' => 'cloudtasks ' ,
169
+ 'queue ' => 'my-queue ' ,
170
+ 'payload ' => rtrim ($ this ->failingJobPayload ()),
171
+ ]);
172
+ }
173
+
147
174
private function simpleJob ()
148
175
{
149
176
return json_decode (file_get_contents (__DIR__ . '/Support/test-job-payload.json ' ), true );
150
177
}
178
+
179
+ private function failingJobPayload ()
180
+ {
181
+ return file_get_contents (__DIR__ . '/Support/failing-job-payload.json ' );
182
+ }
183
+
184
+ private function failingJob ()
185
+ {
186
+ return json_decode ($ this ->failingJobPayload (), true );
187
+ }
151
188
}
0 commit comments