44
55namespace Keboola \Google \ClientBundle \Tests ;
66
7+ use GuzzleHttp \Exception \ClientException ;
78use Keboola \Google \ClientBundle \Google \RestApi ;
9+ use Monolog \Handler \TestHandler ;
810use Monolog \Logger ;
911use PHPUnit \Framework \TestCase ;
1012use SebastianBergmann \Timer \Timer ;
@@ -23,12 +25,17 @@ class RestApiTest extends TestCase
2325 /** @var Logger */
2426 private $ logger ;
2527
28+ /** @var TestHandler */
29+ private $ testHandler ;
30+
2631 protected function initApi (): RestApi
2732 {
2833 $ this ->clientId = $ this ->getEnv ('CLIENT_ID ' );
2934 $ this ->clientSecret = $ this ->getEnv ('CLIENT_SECRET ' );
3035 $ this ->refreshToken = $ this ->getEnv ('REFRESH_TOKEN ' );
36+ $ this ->testHandler = new TestHandler ();
3137 $ this ->logger = new Logger ('Google Rest API tests ' );
38+ $ this ->logger ->pushHandler ($ this ->testHandler );
3239
3340 return new RestApi (
3441 $ this ->clientId ,
@@ -99,6 +106,22 @@ public function testDelayFn(): void
99106 $ this ->assertGreaterThan (5 , $ time );
100107 }
101108
109+ public function testRetries (): void
110+ {
111+ $ restApi = $ this ->initApi ();
112+ try {
113+ $ restApi ->request ('/auth/invalid-scope ' );
114+ } catch (ClientException $ e ) {
115+ }
116+
117+ $ this ->assertNotCount (0 , $ this ->testHandler ->getRecords ());
118+
119+ foreach ($ this ->testHandler ->getRecords () as $ key => $ value ) {
120+ $ this ->assertEquals (Logger::INFO , $ value ['level ' ]);
121+ $ this ->assertEquals (sprintf ('Retrying request (%dx) ' , $ key ), $ value ['message ' ]);
122+ }
123+ }
124+
102125 protected function getEnv (string $ name ): string
103126 {
104127 $ value = getenv ($ name );
0 commit comments