Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webhooks unit tests fix #3880

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions test/unit/webhooks/WebHookClassServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
use core_kernel_classes_Property as Property;
use core_kernel_classes_Resource as Resource;
use oat\generis\model\data\Ontology;
use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use PHPUnit\Framework\TestCase;
use oat\tao\model\auth\BasicAuthType;
use oat\tao\model\auth\BasicType;
use oat\tao\model\webhooks\configEntity\Webhook;
Expand All @@ -41,6 +42,8 @@

class WebHookClassServiceTest extends TestCase
{
use ServiceManagerMockTrait;

private WebHookClassService $webhookService;
private WebhookEntryFactory $webhookEntryFactory;

Expand All @@ -58,7 +61,7 @@ protected function setUp(): void
['getDataBinder', 'getModel', 'getProperty', 'createInstance', 'getRootClass']
);

$serviceLocator = $this->getServiceLocatorMock([
$serviceLocator = $this->getServiceManagerMock([
WebhookEntryFactory::class => $this->webhookEntryFactory,
WebhookAuthService::class => $authService,
]);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/webhooks/WebhookEntryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace oat\tao\test\unit\webhooks;

use oat\generis\test\TestCase;
use PHPUnit\Framework\TestCase;
use oat\tao\model\webhooks\configEntity\WebhookEntryFactory;
use oat\tao\model\webhooks\configEntity\WebhookInterface;

Expand Down
19 changes: 11 additions & 8 deletions test/unit/webhooks/WebhookEventsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace oat\tao\test\unit\webhooks;

use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use oat\oatbox\event\Event;
use oat\oatbox\event\EventManager;
use oat\tao\model\exceptions\WebhookConfigMissingException;
Expand All @@ -31,10 +31,13 @@
use oat\tao\model\webhooks\task\WebhookTaskParams;
use oat\tao\model\webhooks\WebhookTaskServiceInterface;
use oat\generis\test\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class WebhookEventsServiceTest extends TestCase
{
use ServiceManagerMockTrait;

/** @var EventManager|MockObject */
private $eventManagerMock;

Expand Down Expand Up @@ -81,7 +84,7 @@ public function testHandleEventMissingWebhookConfig()
]
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
WebhookRegistryInterface::class => $this->whConfigRegistryMock,
WebhookTaskServiceInterface::SERVICE_ID => $this->whTaskServiceMock
]));
Expand Down Expand Up @@ -112,7 +115,7 @@ public function testRegisterEvent()
WebhookEventsService::OPTION_SUPPORTED_EVENTS => []
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
EventManager::SERVICE_ID => $this->eventManagerMock
]));

Expand All @@ -134,7 +137,7 @@ public function testUnregisterEvent()
]
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
EventManager::SERVICE_ID => $this->eventManagerMock
]));

Expand Down Expand Up @@ -167,7 +170,7 @@ public function testHandleEventPositive()
]
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
WebhookRegistryInterface::class => $this->whConfigRegistryMock,
WebhookTaskServiceInterface::SERVICE_ID => $this->whTaskServiceMock
]));
Expand Down Expand Up @@ -203,7 +206,7 @@ public function testHandleEventPositive()
);
$this->assertGreaterThanOrEqual($whParams[WebhookTaskParams::TRIGGERED_TIMESTAMP], $timestampStart);
$this->assertLessThanOrEqual($whParams[WebhookTaskParams::TRIGGERED_TIMESTAMP], $timestampEnd);
$this->assertRegExp('/^([a-z0-9]{32})$/', $whParams[WebhookTaskParams::EVENT_ID]);
$this->assertMatchesRegularExpression('/^([a-z0-9]{32})$/', $whParams[WebhookTaskParams::EVENT_ID]);
}
}

Expand Down Expand Up @@ -242,7 +245,7 @@ public function testHandleEventNotSerializable()
]
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
WebhookTaskServiceInterface::SERVICE_ID => $this->whTaskServiceMock,
]));

Expand Down Expand Up @@ -275,7 +278,7 @@ public function testHandleEventNoWebhooks()
]
]);

$service->setServiceLocator($this->getServiceLocatorMock([
$service->setServiceLocator($this->getServiceManagerMock([
WebhookRegistryInterface::class => $this->whConfigRegistryMock
]));

Expand Down
7 changes: 5 additions & 2 deletions test/unit/webhooks/WebhookFileRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace oat\tao\test\unit\webhooks;

use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use PHPUnit\Framework\TestCase;
use oat\tao\model\webhooks\configEntity\Webhook;
use oat\tao\model\webhooks\configEntity\WebhookAuth;
use oat\tao\model\webhooks\configEntity\WebhookEntryFactory;
Expand All @@ -29,6 +30,8 @@

class WebhookFileRegistryTest extends TestCase
{
use ServiceManagerMockTrait;

/** @var WebhookFileRegistry */
private $registry;

Expand Down Expand Up @@ -61,7 +64,7 @@ public function testGetWebhookConfig()
]
]);

$serviceLocator = $this->getServiceLocatorMock([
$serviceLocator = $this->getServiceManagerMock([
WebhookEntryFactory::class => $this->webhookEntryFactoryMock
]);

Expand Down
7 changes: 5 additions & 2 deletions test/unit/webhooks/WebhookRegistryManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

namespace oat\tao\test\unit\webhooks;

use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use PHPUnit\Framework\TestCase;
use oat\oatbox\service\ServiceManager;
use oat\tao\model\webhooks\configEntity\Webhook;
use oat\tao\model\webhooks\WebhookFileRegistry;
Expand All @@ -32,6 +33,8 @@

class WebhookRegistryManagerTest extends TestCase
{
use ServiceManagerMockTrait;

/** @var WebhookRegistryManager */
private $subject;

Expand All @@ -49,7 +52,7 @@ public function setUp(): void
$this->webhookMock = $this->createMock(Webhook::class);
$this->webhookFileRegistryMock = $this->createMock(WebhookFileRegistry::class);

$this->serviceLocator = $this->getServiceLocatorMock([
$this->serviceLocator = $this->getServiceManagerMock([
WebhookRegistryInterface::class => $this->webhookFileRegistryMock
]);

Expand Down
87 changes: 0 additions & 87 deletions test/unit/webhooks/task/AuthTypeFake.php

This file was deleted.

9 changes: 6 additions & 3 deletions test/unit/webhooks/task/JsonWebhookResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
namespace oat\tao\test\unit\webhooks\task;

use GuzzleHttp\Psr7\Response;
use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use PHPUnit\Framework\TestCase;
use oat\tao\model\webhooks\task\InvalidJsonException;
use oat\tao\model\webhooks\task\JsonValidator;
use oat\tao\model\webhooks\task\JsonWebhookResponseFactory;

class JsonWebhookResponseFactoryTest extends TestCase
{
use ServiceManagerMockTrait;

public function testGetAcceptedContentType()
{
$factory = new JsonWebhookResponseFactory();
Expand Down Expand Up @@ -57,7 +60,7 @@ public function testCreatePositive()
$event->status === 'accepted';
}));

$factory->setServiceLocator($this->getServiceLocatorMock([
$factory->setServiceLocator($this->getServiceManagerMock([
JsonValidator::class => $jsonValidatorMock
]));

Expand Down Expand Up @@ -103,7 +106,7 @@ public function testCreateInvalidData()
->with(['ab'])
->willThrowException(new InvalidJsonException('Err', 0, ['err1']));

$factory->setServiceLocator($this->getServiceLocatorMock([
$factory->setServiceLocator($this->getServiceManagerMock([
JsonValidator::class => $jsonValidatorMock
]));

Expand Down
41 changes: 33 additions & 8 deletions test/unit/webhooks/task/WebhookSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@
namespace oat\tao\test\unit\webhooks\task;

use GuzzleHttp\Exception\GuzzleException;
use oat\generis\test\TestCase;
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
{
use ServiceManagerMockTrait;

/**
* @throws GuzzleException
* @throws \common_exception_InvalidArgumentType
Expand All @@ -37,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->getServiceLocatorMock());
/** @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);
}
}
2 changes: 1 addition & 1 deletion test/unit/webhooks/task/WebhookTaskParamsFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace oat\tao\test\unit\webhooks\task;

use oat\generis\test\TestCase;
use PHPUnit\Framework\TestCase;
use oat\tao\model\webhooks\task\WebhookTaskParams;
use oat\tao\model\webhooks\task\WebhookTaskParamsFactory;

Expand Down
Loading
Loading