88use AndroidSmsGateway \Domain \MessageState ;
99use AndroidSmsGateway \Domain \Settings ;
1010use AndroidSmsGateway \Domain \Webhook ;
11+ use AndroidSmsGateway \Domain \TokenRequest ;
12+ use AndroidSmsGateway \Domain \TokenResponse ;
1113use AndroidSmsGateway \Enums \ProcessState ;
1214use AndroidSmsGateway \Enums \WebhookEvent ;
1315use Http \Client \Curl \Client as CurlClient ;
@@ -384,6 +386,72 @@ public function testDeleteWebhook(): void {
384386 );
385387 }
386388
389+ public function testGenerateToken (): void {
390+ $ tokenRequest = new TokenRequest (['read ' , 'write ' ], 3600 );
391+
392+ $ responseMock = self ::mockResponse (
393+ '{"access_token":"test-token","token_type":"Bearer","id":"token-id","expires_at":"2023-12-31T23:59:59Z"} ' ,
394+ 201 ,
395+ ['Content-Type ' => 'application/json ' ]
396+ );
397+
398+ $ this ->mockClient ->addResponse ($ responseMock );
399+
400+ $ tokenResponse = $ this ->client ->GenerateToken ($ tokenRequest );
401+ $ req = $ this ->mockClient ->getLastRequest ();
402+ $ this ->assertEquals ('POST ' , $ req ->getMethod ());
403+ $ this ->assertEquals ('/3rdparty/v1/auth/token ' , $ req ->getUri ()->getPath ());
404+ $ this ->assertEquals (
405+ 'Basic ' . base64_encode (self ::MOCK_LOGIN . ': ' . self ::MOCK_PASSWORD ),
406+ $ req ->getHeaderLine ('Authorization ' )
407+ );
408+ $ this ->assertEquals (
409+ 'application/json ' ,
410+ $ req ->getHeaderLine ('Content-Type ' )
411+ );
412+
413+ $ this ->assertInstanceOf (TokenResponse::class, $ tokenResponse );
414+ $ this ->assertEquals ('test-token ' , $ tokenResponse ->AccessToken ());
415+ $ this ->assertEquals ('Bearer ' , $ tokenResponse ->TokenType ());
416+ $ this ->assertEquals ('token-id ' , $ tokenResponse ->ID ());
417+ $ this ->assertEquals ('2023-12-31T23:59:59Z ' , $ tokenResponse ->ExpiresAt ());
418+ }
419+
420+ public function testRevokeToken (): void {
421+ $ responseMock = self ::mockResponse ('' , 204 );
422+
423+ $ this ->mockClient ->addResponse ($ responseMock );
424+
425+ $ this ->client ->RevokeToken ('token-id ' );
426+ $ req = $ this ->mockClient ->getLastRequest ();
427+ $ this ->assertEquals ('DELETE ' , $ req ->getMethod ());
428+ $ this ->assertEquals ('/3rdparty/v1/auth/token/token-id ' , $ req ->getUri ()->getPath ());
429+ $ this ->assertEquals (
430+ 'Basic ' . base64_encode (self ::MOCK_LOGIN . ': ' . self ::MOCK_PASSWORD ),
431+ $ req ->getHeaderLine ('Authorization ' )
432+ );
433+ }
434+
435+ public function testClientWithJwtToken (): void {
436+ $ jwtToken = 'test-jwt-token ' ;
437+ $ client = new Client (null , $ jwtToken , Client::DEFAULT_URL , $ this ->mockClient );
438+
439+ $ responseMock = self ::mockResponse (
440+ '{"id":"123","state":"Sent","recipients":[{"phoneNumber":"+79000000000","state":"Sent"}]} ' ,
441+ 201 ,
442+ ['Content-Type ' => 'application/json ' ]
443+ );
444+
445+ $ this ->mockClient ->addResponse ($ responseMock );
446+
447+ $ messageMock = $ this ->createMock (Message::class);
448+ $ messageMock ->method ('ToObject ' )->willReturn ((object ) []);
449+
450+ $ client ->SendMessage ($ messageMock );
451+ $ req = $ this ->mockClient ->getLastRequest ();
452+ $ this ->assertEquals ('Bearer ' . $ jwtToken , $ req ->getHeaderLine ('Authorization ' ));
453+ }
454+
387455 public const MOCK_LOGIN = 'login ' ;
388456 public const MOCK_PASSWORD = 'password ' ;
389457}
0 commit comments