From 88de9eeebf92ae2a60bd67d1c08213544658be3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Marsza=C5=82?= Date: Tue, 12 Sep 2023 16:11:15 +0200 Subject: [PATCH] fix: WebhookSenderTest --- test/unit/webhooks/task/WebhookSenderTest.php | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/test/unit/webhooks/task/WebhookSenderTest.php b/test/unit/webhooks/task/WebhookSenderTest.php index 0bf5367f9d..153533f4ff 100644 --- a/test/unit/webhooks/task/WebhookSenderTest.php +++ b/test/unit/webhooks/task/WebhookSenderTest.php @@ -22,11 +22,17 @@ use GuzzleHttp\Exception\GuzzleException; use oat\generis\test\ServiceManagerMockTrait; +use oat\taoOauth\model\bootstrap\OAuth2AuthType; +use oat\taoOauth\model\Oauth2Service; +use oat\taoOauth\model\OAuthClient; +use oat\taoOauth\model\storage\grant\OauthCredentials; +use oat\taoOauth\model\storage\OauthCredentialsFactory; use PHPUnit\Framework\TestCase; use oat\tao\model\webhooks\configEntity\WebhookAuthInterface; use oat\tao\model\webhooks\task\WebhookSender; use Psr\Http\Message\RequestInterface; use oat\generis\test\MockObject; +use Psr\Http\Message\ResponseInterface; class WebhookSenderTest extends TestCase { @@ -40,18 +46,34 @@ public function testPerformRequestWithAuth() { /** @var MockObject|RequestInterface $request */ $request = $this->createMock(RequestInterface::class); + $OauthCredentialsFactoryMock = $this->createMock(OauthCredentialsFactory::class); + $responseMock = $this->createMock(ResponseInterface::class); + $OauthCredentialsMock = $this->createMock(OauthCredentials::class); + $OAuthClientMock = $this->createMock(OAuthClient::class); + $Oauth2ServiceMock = $this->createMock(Oauth2Service::class); + $OauthCredentialsFactoryMock->method('getCredentialTypeByCredentials')->willReturn($OauthCredentialsMock); + $Oauth2ServiceMock->method('getClient')->willReturn($OAuthClientMock); + $OAuthClientMock->method('request')->willReturn($responseMock); /** @var MockObject|WebhookAuthInterface $authConfig */ $authConfig = $this->createMock(WebhookAuthInterface::class); - $authConfig->method('getAuthClass')->willReturn(AuthTypeFake::class); - $authConfig->method('getCredentials')->willReturn(['c' => 'v']); + + $authConfig + ->expects(self::once()) + ->method('getAuthClass') + ->willReturn(OAuth2AuthType::class); + + $authConfig + ->expects(self::once()) + ->method('getCredentials') + ->willReturn(['c' => 'v']); $sender = new WebhookSender(); - $sender->setServiceLocator($this->getServiceManagerMock()); - /** @var \stdClass $response */ - $response = $sender->performRequest($request, $authConfig); + $sender->setServiceLocator($this->getServiceManagerMock([ + OauthCredentialsFactory::class => $OauthCredentialsFactoryMock, + Oauth2Service::SERVICE_ID => $Oauth2ServiceMock + ])); - self::assertSame($request, $response->callRequest); - self::assertSame(['c' => 'v'], $response->credentials); + $sender->performRequest($request, $authConfig); } }