diff --git a/.gitignore b/.gitignore index 008dc11..d9d04d8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,8 +3,9 @@ composer.phar coverage package.xml *.tgz +.phpunit.result.cache # You should be keeping these in a global gitignore, see for example # https://help.github.com/articles/ignoring-files#global-gitignore .DS_Store -.idea \ No newline at end of file +.idea diff --git a/README.md b/README.md index b49665b..1268828 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ This package of tools is provided by Greenhouse for customers who use PHP. Ther # Requirements 1. PHP Version - a. 5.6 or greater for V1. - b. 7.3 or greater for V2. + - 5.6 or greater for V1. + - 7.3 or greater for V2. 2. [Composer](https://getcomposer.org/). You should be using Composer to manage this package. -Due to the EOL on PHP 5 and Guzzle 6, this package was upgraded to require PHP 7.3. We will no longer be supporting V1. +Due to the EOL on PHP 5 and Guzzle 6, this package was upgraded to require PHP 7.3. We will no longer be supporting V1. Going forward, new features will be added exclusively to the 2.0 branch. You should update to 2.0 as soon as reasonably possible. # Installing This is available on Packagist. Install via Composer. Add the following to your requirements: @@ -22,6 +22,11 @@ This is available on Packagist. Install via Composer. Add the following to you "grnhse/greenhouse-tools-php": "~2.0" ``` +# New In 2.1 +In order to support Greenhouse's new Inclusion URLs, we dropped a requirement that an $id be included for some URLs. Previously, a URL like `getActivityFeedForUser()` would throw a Greenhouse Exception with a message that id was required. Now, that method will just translate to `user/activity_feed` and return a 404 Not Found response. We did this in order to support Greenhouse's new Inclusion URLs. In the previous version `getQuestionsSetsForDemographics()`, [(found here)](https://developers.greenhouse.io/harvest.html#get-list-demographic-question-sets), would have thrown an "id required" exception instead of correctly routing to `demographics/question_sets`. + +# New in 2.0 +Support for PHP 5.6, 7.0, 7.1, and 7.2 were dropped in order to support the latest version of Guzzle. # Greenhouse Service The Greenhouse Service is a parent service that returns the other Greenhouse Services. By using this service, you have access to all the other services. The Greenhouse service takes an array that optionally includes your job board URL Token [(found here in Greenhouse)](https://app.greenhouse.io/configure/dev_center/config/) and your Job Board API Credentials [(found here in Greenhouse)](https://app.greenhouse.io/configure/dev_center/credentials). Create a Greenhouse Service object like this: @@ -161,6 +166,17 @@ Some method calls and URLs do not fit this format, but the methods were named as * `getTrackingLinks`: [Return a specific traking link for the supplied token.](https://developers.greenhouse.io/harvest.html#get-tracking-link-data-for-token): Note for this link, the token will be provided in the 'id' argument. `$harvestService->getTrackingLink(array('id' => ''));` * `patchEnableUser`: [Enable a disabled user from accessing Greenhouse.](https://developers.greenhouse.io/harvest.html#patch-enable-user) * `patchDisableUser`: [Disable a user from accessing Greenhouse.](https://developers.greenhouse.io/harvest.html#patch-disable-user) + * `getQuestionSetsForDemographics`: [List all demographic question sets.](https://developers.greenhouse.io/harvest.html#get-list-demographic-question-sets) + * `getQuestionSetsForDemographics(['id' => 12345])`: [Fetch demographic question set with the id 12345](https://developers.greenhouse.io/harvest.html#get-retrieve-demographic-question-set) + * `getQuestionsForDemographics`: [List all demographic questions.](https://developers.greenhouse.io/harvest.html#get-list-demographic-questions) + * `getQuestionsForDemographics(['id' => 12345])`: [List demographic question with id 12345.](https://developers.greenhouse.io/harvest.html#get-retrieve-demographic-question) + * `getQuestionsForQuestionSetsForDemographics(['id' => 12345])`: [Fetch all demographic questions for question set with id 12345](https://developers.greenhouse.io/harvest.html#get-list-demographic-questions-for-demographic-question-set) + * `getAnswerOptionsForDemographics`: [List all demographic answer options.](https://developers.greenhouse.io/harvest.html#get-list-demographic-answer-options) + * `getAnswerOptionsForDemographics(['id' => 12345])`: [List demographic answer option with id 12345.](https://developers.greenhouse.io/harvest.html#get-retrieve-demographic-answer-option) + * `getAnswerOptionsForQuestionsForDemographics(['id' => 12345])`: [Fetch all answer option for demographic question with id 12345](https://developers.greenhouse.io/harvest.html#get-list-demographic-questions-for-demographic-question-set) + * `getAnswersForDemographics`: [List all demographic answers.](https://developers.greenhouse.io/harvest.html#get-list-demographic-answers) + * `getAnswersForDemographics(['id' => 12345])`: [List demographic answer with id 12345.](https://developers.greenhouse.io/harvest.html#get-retrieve-demographic-answer) + * `getDemographicAnswersForApplications(['id' => 12345])`: [List demographics answers for the application with id 12345](https://developers.greenhouse.io/harvest.html#get-list-demographic-answers-for-application) You should use the parameters array to supply any URL parameters and headers required by the harvest methods. For any items that require a JSON body, this will also be supplied in the parameter array. diff --git a/src/Services/HarvestService.php b/src/Services/HarvestService.php index 7ec6715..613f3e6 100644 --- a/src/Services/HarvestService.php +++ b/src/Services/HarvestService.php @@ -262,6 +262,76 @@ public function deleteHiringTeamForJob($parameters=array()) $this->_trimUrlAndSendRequest(); } + public function getQuestionSetsForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getQuestionSetsForDemographics', $parameters); + if (array_key_exists('id', $parameters) && $parameters['id']) { + $this->_harvest['url'] = 'demographics/question_sets/' . $parameters['id']; + } else { + $this->_harvest['url'] = 'demographics/question_sets'; + } + + return $this->sendRequest(); + } + + public function getQuestionsForQuestionSetsForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getQuestionsForQuestionSetsForDemographics', $parameters); + $this->_harvest['url'] = 'demographics/question_sets/' . $parameters['id'] . '/questions'; + + return $this->sendRequest(); + } + + public function getAnswerOptionsForQuestionsForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getAnswerOptionsForQuestionsForDemographics', $parameters); + $this->_harvest['url'] = 'demographics/questions/' . $parameters['id'] . '/answer_options'; + + return $this->sendRequest(); + } + + public function getQuestionsForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getQuestionsForDemographics', $parameters); + if (array_key_exists('id', $parameters) && $parameters['id']) { + $this->_harvest['url'] = 'demographics/questions/' . $parameters['id']; + } else { + $this->_harvest['url'] = 'demographics/questions'; + } + + return $this->sendRequest(); + } + + public function getAnswerOptionsForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getAnswerOptionsForDemographics', $parameters); + if (array_key_exists('id', $parameters) && $parameters['id']) { + $this->_harvest['url'] = 'demographics/answer_options/' . $parameters['id']; + } else { + $this->_harvest['url'] = 'demographics/answer_options'; + } + + return $this->sendRequest(); + } + + public function getAnswersForDemographics($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getAnswersForDemographics', $parameters); + if (array_key_exists('id', $parameters) && $parameters['id']) { + $this->_harvest['url'] = 'demographics/answers/' . $parameters['id']; + } else { + $this->_harvest['url'] = 'demographics/answers'; + } + + return $this->sendRequest(); + } + + public function getDemographicAnswersForApplications($parameters=array()) + { + $this->_harvest = $this->_harvestHelper->parse('getDemographicsForAnswersForApplications', $parameters); + return $this->sendRequest(); + } + private function _trimUrlAndSendRequest() { $this->_harvest['url'] = substr($this->_harvest['url'], 0, -1); diff --git a/src/Tools/HarvestHelper.php b/src/Tools/HarvestHelper.php index 23d9e1c..254e026 100644 --- a/src/Tools/HarvestHelper.php +++ b/src/Tools/HarvestHelper.php @@ -54,14 +54,16 @@ public function methodToEndpoint($methodText, $parameters) // Double object, expect the format object/id/object } else if (sizeof($objects) == 2) { - if (!$id) throw new GreenhouseServiceException("Harvest Service: method call $methodText must include an id parameter"); - $url = $this->_decamelizeAndPluralize($objects[1]) . "/$id/" . $this->_decamelizeAndPluralize($objects[0]); + $url = $this->_decamelizeAndPluralize($objects[1]) . + $this->_getDivider($id) . + $this->_decamelizeAndPluralize($objects[0]); $url = $secondId ? $url . '/' . $secondId : $url; // Triple object, expect the format object/id/object/object } else if (sizeof($objects) == 3) { if (!$id) throw new GreenhouseServiceException("Harvest Service: method call $methodText must include an id parameter"); - $url = $this->_decamelizeAndPluralize($objects[2]) . "/$id/" . + $url = $this->_decamelizeAndPluralize($objects[2]) . + $this->_getDivider($id) . $this->_decamelizeAndPluralize($objects[0]) . '/' . $this->_decamelizeAndPluralize($objects[1]); } else { @@ -79,6 +81,11 @@ public function addQueryString($url, $parameters=array()) return $url; } } + + private function _getDivider($id) + { + return $id ? "/$id/" : '/'; + } private function _decamelizeAndPluralize($string) { diff --git a/tests/Services/HarvestServiceTest.php b/tests/Services/HarvestServiceTest.php index d4789aa..0d608f8 100644 --- a/tests/Services/HarvestServiceTest.php +++ b/tests/Services/HarvestServiceTest.php @@ -57,14 +57,6 @@ public function testGetActivityFeed() $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } - public function testGetActivityFeedRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->getActivityFeedForCandidate($params); - } - public function testGetApplicationsNoPaging() { $expected = array( @@ -250,46 +242,6 @@ public function testPostUnrejectApplication() $this->assertEquals($expected, $this->harvestService->getHarvest()); $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } - - public function testMoveApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->postMoveApplication($params); - } - - public function testAdvanceApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->postAdvanceApplication($params); - } - - public function testPostTransferApplicationToJobIdRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->postTransferApplicationToJob($params); - } - - public function testRejectApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->postRejectApplication($params); - } - - public function testPostUnrejectApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->postUnrejectApplication($params); - } public function testGetCandidatesNoPaging() { @@ -1056,14 +1008,6 @@ public function testGetOffersForApplications() $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } - public function testGetOffersForApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->getOffersForApplication($params); - } - public function testGetCurrentOfferForApplication() { $expected = array( @@ -1080,14 +1024,6 @@ public function testGetCurrentOfferForApplication() $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } - public function testGetCurrentOfferForApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->getCurrentOfferForApplication($params); - } - public function testGetOfficesNoPaging() { $expected = array( @@ -1301,14 +1237,6 @@ public function testGetScorecardForApplication() $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } - public function testGetScorecardForApplicationRequiresId() - { - $params = array('noid' => 12345); - - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->harvestService->getScorecardsForApplication($params); - } - public function testGetSourcesNoPaging() { $expected = array( @@ -1695,4 +1623,180 @@ public function testDeleteHiringTeamForJob() $this->assertEquals($expected, $this->harvestService->getHarvest()); $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); } + + public function testGetQuestionSetsForDemographics() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/question_sets', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array(); + + $this->harvestService->getQuestionSetsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetQuestionSetsForDemographicsWithId() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/question_sets/12345', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getQuestionSetsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetQuestionsForDemographics() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/questions', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array(); + + $this->harvestService->getQuestionsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetQuestionsForDemographicsWithId() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/questions/12345', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getQuestionsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetQuestionsForQuestionSetsForDemographic() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/question_sets/12345/questions', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getQuestionsForQuestionSetsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetAnswerOptionsForDemographics() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/answer_options', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array(); + + $this->harvestService->getAnswerOptionsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetAnswerOptionsForDemographicsWithId() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/answer_options/12345', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getAnswerOptionsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetAnswerOptionsForQuestionsForDemographicsWithId() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/questions/12345/answer_options', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getAnswerOptionsForQuestionsForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetAnswersForDemographics() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/answers', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array(); + + $this->harvestService->getAnswersForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetAnswersForDemographicsWithId() + { + $expected = array( + 'method' => 'get', + 'url' => 'demographics/answers/12345', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getAnswersForDemographics($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } + + public function testGetDemographicAnswersForApplication() + { + $expected = array( + 'method' => 'get', + 'url' => 'applications/12345/demographics/answers', + 'headers' => array(), + 'body' => null, + 'parameters' => array() + ); + $params = array('id' => 12345); + + $this->harvestService->getDemographicAnswersForApplications($params); + $this->assertEquals($expected, $this->harvestService->getHarvest()); + $this->assertEquals($this->expectedAuth, $this->harvestService->getAuthorizationHeader()); + } } \ No newline at end of file diff --git a/tests/Tools/HarvestHelperTest.php b/tests/Tools/HarvestHelperTest.php index db516a6..c576811 100644 --- a/tests/Tools/HarvestHelperTest.php +++ b/tests/Tools/HarvestHelperTest.php @@ -145,10 +145,17 @@ public function testParseGetDoubleWordMethodWithSecondId() public function testParseGetDoubleWordMethodWithForNoId() { - $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); - $this->parser->parse('getScorecardsForApplication', array()); + $expected = array( + 'method' => 'get', + 'url' => 'activity_feeds/email_templates', + 'parameters' => $this->parameters, + 'headers' => array(), + 'body' => null + ); + $params = array_merge($this->parameters, array()); + $this->assertEquals($expected, $this->parser->parse('getEmailTemplateForActivityFeed', $params)); } - + public function testParseTripleWordMethod() { $expected = array( @@ -167,7 +174,7 @@ public function testParseTripleWordMethodRequiresId() $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException'); $this->parser->parse('deletePermissionForJobForUser', array()); } - + public function testBadHttpMethodFails() { $this->expectException('\Greenhouse\GreenhouseToolsPhp\Services\Exceptions\GreenhouseServiceException');