77use Google \Cloud \Tasks \V2 \CloudTasksClient ;
88use Illuminate \Cache \Events \CacheHit ;
99use Illuminate \Cache \Events \KeyWritten ;
10+ use Illuminate \Support \Facades \DB ;
1011use Illuminate \Support \Facades \Event ;
1112use Illuminate \Support \Facades \Mail ;
1213use 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}
0 commit comments