Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wthijmen authored and myparcel-bot[bot] committed Nov 1, 2023
1 parent b7e8935 commit af84b59
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion tests/Unit/App/Webhook/PdkWebhookManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,63 @@

usesShared(new UsesMockPdkInstance(), new UsesMockEachCron());

it('dispatches incoming webhook', function (string $hook) {
it('dispatches and executes webhooks with myparcel header', function (string $hook) {
/** @var PdkWebhooksRepositoryInterface $repository */
$repository = Pdk::get(PdkWebhooksRepositoryInterface::class);
/** @var PdkWebhookManagerInterface $webhookManager */
$webhookManager = Pdk::get(PdkWebhookManagerInterface::class);
/** @var \MyParcelNL\Pdk\Tests\Bootstrap\MockCronService $cronService */
$cronService = Pdk::get(CronServiceInterface::class);

$repository->store(new WebhookSubscriptionCollection([['hook' => $hook, 'url' => $repository->getHashedUrl()]]));

$time = time();

$request = Request::create(
$repository->getHashedUrl(),
Request::METHOD_POST,
[],
[],
[],
['HTTP_X_MYPARCEL_HOOK' => $hook],
json_encode([
'data' => [
'hooks' => [
array_merge(['event' => $hook]),
],
],
])
);

$response = $webhookManager->call($request);

$scheduled = $cronService->getScheduledTasks();
$timestamp = $scheduled->first()['timestamp'];

expect($response->getStatusCode())
->toBe(Response::HTTP_ACCEPTED)
->and($request->headers->all())
->toHaveKey('x-myparcel-hook')
->and($response->getContent())
->toBe('')
->and($response->getStatusCode())
->toBe(Response::HTTP_ACCEPTED)
->and($scheduled->all())
->toHaveLength(1)
// The webhook is scheduled to start immediately. Add a little window to account for the time it takes to run the test.
->and($timestamp)
->toBeGreaterThanOrEqual($time - 10)
->and($timestamp)
->toBeLessThanOrEqual($time + 10);

$cronService->executeAllTasks();
expect(
$cronService->getScheduledTasks()
->all()
)->toBeEmpty();
})->with(WebhookSubscription::ALL);

it('dispatches and executes incoming webhook without myparcel header', function (string $hook) {
/** @var PdkWebhooksRepositoryInterface $repository */
$repository = Pdk::get(PdkWebhooksRepositoryInterface::class);
/** @var PdkWebhookManagerInterface $webhookManager */
Expand Down Expand Up @@ -47,4 +103,10 @@
->toBeGreaterThanOrEqual($time - 10)
->and($timestamp)
->toBeLessThanOrEqual($time + 10);

$cronService->executeAllTasks();
expect(
$cronService->getScheduledTasks()
->all()
)->toBeEmpty();
})->with(WebhookSubscription::ALL);

0 comments on commit af84b59

Please sign in to comment.