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,10 +46,23 @@ protected function setUp(): void
4546        $ googlePublicKeyshouldReceive ('getPublicKey ' )->andReturnNull ();
4647        $ googlePublicKeyshouldReceive ('getKidFromOpenIdToken ' )->andReturnNull ();
4748
49+         $ cloudTasksClientmock (new  CloudTasksClient ());
50+ 
51+         // Ensure we don't fetch the Queue name and attempts each test... 
52+         $ cloudTasksClientshouldReceive ('queueName ' )->andReturn ('my-queue ' );
53+         $ cloudTasksClientshouldReceive ('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 (),
51-             $ this jwt ,
5266            $ googlePublicKey
5367        );
5468
@@ -66,21 +80,6 @@ public function it_needs_an_authorization_header()
6680        $ this handler ->handle ();
6781    }
6882
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- 
8483    /** @test */ 
8584    public  function  it_will_validate_the_token_iss ()
8685    {
@@ -144,8 +143,46 @@ public function it_runs_the_incoming_job()
144143        Mail::assertSent (TestMailable::class);
145144    }
146145
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+ 
147174    private  function  simpleJob ()
148175    {
149176        return  json_decode (file_get_contents (__DIR__  . '/Support/test-job-payload.json ' ), true );
150177    }
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+     }
151188}
0 commit comments