Skip to content

Commit

Permalink
Add entity search
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanraftery committed Nov 3, 2020
1 parent e575428 commit 85e3351
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 105 deletions.
53 changes: 34 additions & 19 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Client
* @throws Auth\Exception\InvalidConfigException
* @throws Auth\Exception\BullhornAuthException
*/
public function __construct(array $options = []) {
function __construct(array $options = []) {
if (array_key_exists(ClientOptions::AuthDataStore, $options)
&& array_key_exists(ClientOptions::AuthClient, $options)
) {
Expand Down Expand Up @@ -132,7 +132,7 @@ protected function setupHttpClient() {
* @param array $options
* @throws Auth\Exception\BullhornAuthException
*/
public function initiateSession(array $options = []) {
function initiateSession(array $options = []) {
$gotSession = false;
$tries = 0;
do {
Expand All @@ -156,7 +156,7 @@ public function initiateSession(array $options = []) {
* @throws Auth\Exception\RestLoginException
* @throws Auth\Exception\InvalidRefreshTokenException
*/
public function refreshSession(array $options = []) {
function refreshSession(array $options = []) {
$this->authClient->refreshSession($options);
$this->setupHttpClient();
}
Expand All @@ -165,7 +165,7 @@ public function refreshSession(array $options = []) {
* Returns if the client's current REST session is valid
* @return bool
*/
public function sessionIsValid() {
function sessionIsValid() {
return $this->authClient->sessionIsValid();
}

Expand All @@ -175,7 +175,7 @@ public function sessionIsValid() {
* @throws Auth\Exception\CreateSessionException
* @throws Auth\Exception\RestLoginException
*/
public function refreshOrInitiateSession(array $options = []) {
function refreshOrInitiateSession(array $options = []) {
try {
$this->refreshSession($options);
}
Expand All @@ -190,15 +190,10 @@ public function refreshOrInitiateSession(array $options = []) {
* @param string $url
* @param array $options
* @return ResponseInterface
* @throws HttpException
* @throws GuzzleException
*/
public function rawRequest(string $method, string $url, $options = []) {
// try {
return $this->httpClient->request($method, $url, $options);
// }
// catch (GuzzleException $e) {
// throw new HttpException($e);
// }
function rawRequest(string $method, string $url, $options = []) {
return $this->httpClient->request($method, $url, $options);
}

/**
Expand All @@ -208,7 +203,7 @@ public function rawRequest(string $method, string $url, $options = []) {
* @return mixed
* @throws HttpException
*/
public function createEntity(string $entityType, array $entityProps) {
function createEntity(string $entityType, array $entityProps) {
try {
$response = $this->httpClient->put("entity/$entityType", [
'body' => json_encode((object)$entityProps),
Expand All @@ -227,7 +222,7 @@ public function createEntity(string $entityType, array $entityProps) {
* @return mixed
* @throws HttpException
*/
public function deleteEntity(string $entityType, int $entityId) {
function deleteEntity(string $entityType, int $entityId) {
try {
$response = $this->httpClient->delete("entity/$entityType/$entityId");
$body = $response->getBody()->getContents();
Expand All @@ -246,7 +241,7 @@ public function deleteEntity(string $entityType, int $entityId) {
* @return mixed
* @throws HttpException
*/
public function fetchEntities(string $entityType, array $entityIds, array $params = []) {
function fetchEntities(string $entityType, array $entityIds, array $params = []) {
try {
$joinedIds = implode(',', $entityIds);
$response = $this->httpClient->get("entity/{$entityType}/{$joinedIds}", [
Expand All @@ -259,6 +254,26 @@ public function fetchEntities(string $entityType, array $entityIds, array $param
}
}

/**
* Searches entities
* @param string $entityType
* @param string $luceneQuery
* @param array $params
* @return mixed
* @throws HttpException
*/
function searchEntities(string $entityType, string $luceneQuery, array $params = []) {
try {
$response = $this->httpClient->get("search/$entityType", [
'query' => array_merge($params, ['query' => $luceneQuery]),
]);
$response->getBody()->rewind();
return json_decode($response->getBody()->getContents());
} catch (GuzzleException $e) {
throw new HttpException($e);
}
}

/**
* Creates an event subscription
* @param string $subscriptionName
Expand All @@ -267,7 +282,7 @@ public function fetchEntities(string $entityType, array $entityIds, array $param
* @return mixed
* @throws HttpException
*/
public function createEventSubscription(string $subscriptionName, array $entityTypes, array $eventTypes) {
function createEventSubscription(string $subscriptionName, array $entityTypes, array $eventTypes) {
try {
$response = $this->httpClient->put("event/subscription/$subscriptionName", [
'query' => [
Expand All @@ -289,7 +304,7 @@ public function createEventSubscription(string $subscriptionName, array $entityT
* @return mixed
* @throws HttpException
*/
public function deleteEventSubscription(string $subscriptionName) {
function deleteEventSubscription(string $subscriptionName) {
try {
$response = $this->httpClient->delete("event/subscription/$subscriptionName");
return json_decode($response->getBody()->getContents());
Expand All @@ -307,7 +322,7 @@ public function deleteEventSubscription(string $subscriptionName) {
* @return mixed
* @throws HttpException
*/
public function fetchEventSubscriptionEvents(string $subscriptionName, int $maxEvents = 100, ?int $requestId = null) {
function fetchEventSubscriptionEvents(string $subscriptionName, int $maxEvents = 100, ?int $requestId = null) {
try {
$response = $this->httpClient->get("event/subscription/$subscriptionName", [
'query' => [
Expand Down
139 changes: 75 additions & 64 deletions tests/Integration/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,77 @@ protected function setUp(): void {
$this->client->initiateSession();
}

/**
* @throws BullhornAuthException
* @throws CreateSessionException
* @throws InvalidRefreshTokenException
* @throws RestLoginException|InvalidConfigException
*/
function test_itTrowsExceptionOnInvalidRefreshToken() {
$dataStore = new MemoryDataStore();
$client = new Client([
ClientOptions::AuthDataStore => $dataStore
]);
$client->initiateSession();
$dataKey = $_ENV['BULLHORN_CLIENT_ID'] . '-refresh-token';
$dataStore->store($dataKey, 'invalid-refresh-token');
$this->expectException(InvalidRefreshTokenException::class);
$client->refreshSession();
}

/**
* @throws BullhornAuthException
* @throws HttpException
* @throws InvalidConfigException
* @throws \jonathanraftery\Bullhorn\Rest\Auth\Exception\InvalidConfigException
*/
function test_onInvalidTokenUsed_itThrowsInvalidTokenException() {
$client = new Client([
ClientOptions::AuthClient => new InvalidTokenAuthClient([
AuthClientOptions::DataStore => new MemoryDataStore(),
]),
ClientOptions::AutoRefreshSessions => false,
]);
$client->initiateSession();
$this->expectException(InvalidTokenException::class);
$client->rawRequest('GET', 'entity/Candidate', [
'query' => [
'fields' => 'id',
],
]);
}

/**
* @group slow
* @throws InvalidConfigException
* @throws BullhornAuthException
* @throws HttpException
*/
function test_onExpiredTokenUsed_itThrowsInvalidTokenException() {
$client = new Client([
ClientOptions::AuthDataStore => new MemoryDataStore(),
ClientOptions::AutoRefreshSessions => false,
]);
$client->initiateSession(['ttl' => 1]);
sleep(61);
$this->expectException(InvalidTokenException::class);
$client->fetchEntities(BullhornEntities::Candidate, [1]);
}

/**
* @group slow
* @throws BullhornAuthException
* @throws BullhornClientException
*/
function test_itRefreshesSessionIfInvalidTokenDetected() {
$this->client->initiateSession(['ttl' => 1]);
sleep(61);
$result = $this->client->fetchEntities(BullhornEntities::CorporateUser, [1], [
'fields' => 'id',
]);
$this->assertEquals(1, $result->id);
}

/**
* @throws BullhornClientException
*/
Expand Down Expand Up @@ -107,73 +178,13 @@ function test_itFetchesEventSubscriptionEvents() {
}

/**
* @throws BullhornAuthException
* @throws CreateSessionException
* @throws InvalidRefreshTokenException
* @throws RestLoginException|InvalidConfigException
*/
function test_itTrowsExceptionOnInvalidRefreshToken() {
$dataStore = new MemoryDataStore();
$client = new Client([
ClientOptions::AuthDataStore => $dataStore
]);
$client->initiateSession();
$dataKey = $_ENV['BULLHORN_CLIENT_ID'] . '-refresh-token';
$dataStore->store($dataKey, 'invalid-refresh-token');
$this->expectException(InvalidRefreshTokenException::class);
$client->refreshSession();
}

/**
* @throws BullhornAuthException
* @throws HttpException
* @throws InvalidConfigException
* @throws \jonathanraftery\Bullhorn\Rest\Auth\Exception\InvalidConfigException
*/
function test_onInvalidTokenUsed_itThrowsInvalidTokenException() {
$client = new Client([
ClientOptions::AuthClient => new InvalidTokenAuthClient([
AuthClientOptions::DataStore => new MemoryDataStore(),
]),
ClientOptions::AutoRefreshSessions => false,
]);
$client->initiateSession();
$this->expectException(InvalidTokenException::class);
$client->rawRequest('GET', 'entity/Candidate', [
'query' => [
'fields' => 'id',
],
]);
}

/**
* @group slow
* @throws InvalidConfigException
* @throws BullhornAuthException
* @throws HttpException
*/
function test_onExpiredTokenUsed_itThrowsInvalidTokenException() {
$client = new Client([
ClientOptions::AuthDataStore => new MemoryDataStore(),
ClientOptions::AutoRefreshSessions => false,
]);
$client->initiateSession(['ttl' => 1]);
sleep(61);
$this->expectException(InvalidTokenException::class);
$client->fetchEntities(BullhornEntities::Candidate, [1]);
}

/**
* @group slow
* @throws BullhornAuthException
* @throws BullhornClientException
*/
function test_itRefreshesSessionIfInvalidTokenDetected() {
$this->client->initiateSession(['ttl' => 1]);
sleep(61);
$result = $this->client->fetchEntities(BullhornEntities::CorporateUser, [1], [
function test_itSearchesEntities() {
$result = $this->client->searchEntities(BullhornEntities::Candidate, 'isDeleted:0', [
'count' => 3,
'fields' => 'id',
]);
$this->assertEquals(1, $result->id);
$this->assertNotNull($result->data[0]->id);
}
}

0 comments on commit 85e3351

Please sign in to comment.