Skip to content

Commit

Permalink
fix: webhooks unit tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomiejmarszal committed Sep 12, 2023
1 parent 87b82b5 commit 1f73cbd
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 42 deletions.
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
15 changes: 5 additions & 10 deletions test/unit/webhooks/task/AuthTypeFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

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

use oat\generis\test\ServiceManagerMockTrait;
use oat\tao\model\auth\AbstractAuthType;
use PHPUnit\Framework\MockObject\Generator;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use oat\generis\test\MockObject;
use PHPUnit\Framework\MockObject\Generator\Generator;

class AuthTypeFake extends AbstractAuthType implements ServiceLocatorAwareInterface
class AuthTypeFake extends AbstractAuthType
{
use ServiceLocatorAwareTrait;
use ServiceManagerMockTrait;

/**
* Call a request through current authenticator
Expand All @@ -43,12 +42,8 @@ public function call(RequestInterface $request, array $clientOptions = [])
{
$mockGenerator = new Generator();

if (!$this->getServiceLocator()) {
throw new \RuntimeException('Service Locator is not set');
}

/** @var MockObject|ResponseInterface $response */
$response = $mockGenerator->getMock(ResponseInterface::class);
$response = $mockGenerator->generate(ResponseInterface::class, true);
$response->callRequest = $request;
$response->callClientOptions = $clientOptions;
$response->credentials = $this->credentials;
Expand Down
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
7 changes: 5 additions & 2 deletions test/unit/webhooks/task/WebhookSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
namespace oat\tao\test\unit\webhooks\task;

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

class WebhookSenderTest extends TestCase
{
use ServiceManagerMockTrait;

/**
* @throws GuzzleException
* @throws \common_exception_InvalidArgumentType
Expand All @@ -44,7 +47,7 @@ public function testPerformRequestWithAuth()
$authConfig->method('getCredentials')->willReturn(['c' => 'v']);

$sender = new WebhookSender();
$sender->setServiceLocator($this->getServiceLocatorMock());
$sender->setServiceLocator($this->getServiceManagerMock());
/** @var \stdClass $response */
$response = $sender->performRequest($request, $authConfig);

Expand Down
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
7 changes: 5 additions & 2 deletions test/unit/webhooks/task/WebhookTaskReportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use oat\generis\test\TestCase;
use oat\generis\test\ServiceManagerMockTrait;
use PHPUnit\Framework\TestCase;
use oat\tao\model\webhooks\log\WebhookEventLogInterface;
use oat\tao\model\webhooks\task\WebhookResponse;
use oat\tao\model\webhooks\task\WebhookTaskContext;
Expand All @@ -36,6 +37,8 @@

class WebhookTaskReportsTest extends TestCase
{
use ServiceManagerMockTrait;

/**
* @var WebhookEventLogInterface|MockObject
*/
Expand Down Expand Up @@ -65,7 +68,7 @@ public function setUp(): void
{
$this->webhookEventLogMock = $this->createMock(WebhookEventLogInterface::class);
$this->loggerMock = $this->createMock(LoggerInterface::class);
$this->serviceLocatorMock = $this->getServiceLocatorMock([
$this->serviceLocatorMock = $this->getServiceManagerMock([
WebhookEventLogInterface::SERVICE_ID => $this->webhookEventLogMock,
]);

Expand Down
Loading

0 comments on commit 1f73cbd

Please sign in to comment.