Skip to content

Commit

Permalink
Add user not found error (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
silas authored Jun 4, 2024
1 parent 08a0e7e commit 726ae97
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
20 changes: 20 additions & 0 deletions lib/Webhook/WebhookUserNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace UserHub\Webhook;

use UserHub\Code;
use UserHub\UserHubError;

/**
* WebhookUserNotFound is an error which can be used to indicate a user was
* not found in the onGetUser, onUpdateUser, and onDeleteUser methods.
*/
class WebhookUserNotFound extends UserHubError
{
public function __construct()
{
parent::__construct(message: 'User not found', apiCode: Code::NotFound);
}
}
79 changes: 76 additions & 3 deletions tests/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

use PHPUnit\Framework\TestCase;
use UserHub\Code;
use UserHub\ConnectionsV1\CustomUser;
use UserHub\ConnectionsV1\GetCustomUserRequest;
use UserHub\ConnectionsV1\ListCustomUsersRequest;
use UserHub\ConnectionsV1\ListCustomUsersResponse;
use UserHub\EventsV1\Event;
use UserHub\Internal\Constants;
use UserHub\UserHubError;
use UserHub\Webhook;
use UserHub\Webhook\WebhookRequest;
use UserHub\Webhook\WebhookResponse;
use UserHub\Webhook\WebhookUserNotFound;

/**
* @internal
Expand All @@ -35,10 +40,27 @@ public function testHandle(
$onError = static function (Exception $ex): void {};

$webhook = new Webhook($secret, onError: $onError);
$webhook->onEvent(static function (Event $event): void {
if ('ok' !== $event->type) {
throw new UserHubError("Event failed: {$event->type}", apiCode: Code::InvalidArgument);

$webhook->onEvent(static function (Event $input): void {
if ('ok' !== $input->type) {
throw new UserHubError("Event failed: {$input->type}", apiCode: Code::InvalidArgument);
}
});

$webhook->onListUsers(static function (ListCustomUsersRequest $input): ListCustomUsersResponse {
if (100 !== $input->pageSize) {
throw new Error("unexpected page size {$input->pageSize}");
}

return new ListCustomUsersResponse(users: [], nextPageToken: '');
});

$webhook->onGetUser(static function (GetCustomUserRequest $input): CustomUser {
if ('not-found' === $input->id) {
throw new WebhookUserNotFound();
}

return new CustomUser(id: $input->id);
});

if ($setTimestamp) {
Expand Down Expand Up @@ -383,6 +405,57 @@ public static function provideHandleCases(): iterable
false,
true,
],
[
'List users',
'test',
new WebhookRequest(
headers: [
'UserHub-Action' => 'users.list',
],
body: '{"pageSize":100}',
),
new WebhookResponse(
statusCode: 200,
body: '{"nextPageToken":"","users":[]}',
),
true,
false,
true,
],
[
'Get user',
'test',
new WebhookRequest(
headers: [
'UserHub-Action' => 'users.get',
],
body: '{"id": "1"}',
),
new WebhookResponse(
statusCode: 200,
body: '{"id":"1","displayName":"","email":"","emailVerified":false,"phoneNumber":"","phoneNumberVerified":false,"imageUrl":"","disabled":false}',
),
true,
false,
true,
],
[
'Get user not found',
'test',
new WebhookRequest(
headers: [
'UserHub-Action' => 'users.get',
],
body: '{"id": "not-found"}',
),
new WebhookResponse(
statusCode: 404,
body: '{"message":"User not found","code":"NOT_FOUND"}',
),
true,
false,
true,
],
];
}
}

0 comments on commit 726ae97

Please sign in to comment.