Skip to content

Commit

Permalink
Merge pull request #25 from grnhse/GREEN-25568-allow-demographic-educ…
Browse files Browse the repository at this point in the history
…ations-and-employments-question-answers

Allow demographic educations and employments question answers
  • Loading branch information
tdphillipsjr authored Sep 15, 2020
2 parents 972db2e + dc88b38 commit 6cd22e8
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 45 deletions.
59 changes: 38 additions & 21 deletions src/Clients/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,50 @@ public function send($method, $url, Array $options=array())
*/
public function formatPostParameters(Array $postParameters=array())
{
$guzzleParams = array();
/**
* There are three possibile inputs for $postParameter values. Strings, Curlfiles, and arrays.
* This look should process them in to something that Guzzle understands.
*/
foreach ($postParameters as $key => $value) {
if ($value instanceof \CURLFile) {
$guzzleParams[] = array(
'name' => $key,
'contents' => fopen($value->getFilename(), 'r'),
'filename' => $value->getPostFilename()
);
} else if (is_array($value)) {
foreach ($value as $val) $guzzleParams[] = array('name' => $key . '[]', 'contents' => "$val");
} else {
$guzzleParams[] = array('name' => $key, 'contents' => "$value");
$guzzleParams = array();
/**
* There are three possibile inputs for $postParameter values. Strings, Curlfiles, and arrays.
* This look should process them in to something that Guzzle understands.
*/
foreach ($postParameters as $key => $value) {
if ($value instanceof \CURLFile) {
$guzzleParams[] = array(
'name' => $key,
'contents' => fopen($value->getFilename(), 'r'),
'filename' => $value->getPostFilename()
);
} else if (is_array($value)) {
foreach ($value as $val) {
if (!is_array($val)) {
$guzzleParams[] = array('name' => $key .'[]', 'contents' => "$val");
continue;
}

// $val is an array, potentially containing nested elements that need to be serialized in
// a way that the API accepts. See the `curl` example in the documentation to better understand the
// nested array sturcture: https://developers.greenhouse.io/job-board.html#submit-an-application
// Namely, arrays must be non-indexed, e.g. myarray[][mykey]=value instead of myarray[0][mykey]=value
$query = http_build_query($val);
$query = preg_replace('/%5B[\d]+%5D/simU', '%5B%5D', $query);
foreach (explode('&', $query) as $val) {
$val = urldecode($val);
$tuple = explode('=', $val);
$guzzleParams[] = array('name' => $key .'[]'. $tuple[0], 'contents' => "$tuple[1]");
}
}
} else {
$guzzleParams[] = array('name' => $key, 'contents' => "$value");
}

return $guzzleParams;
}

return $guzzleParams;
}

public function getClient()
{
return $this->_client;
}

/**
* Set the next/prev/last links using the current response object.
*/
Expand Down Expand Up @@ -161,9 +178,9 @@ public function getLastLink()
{
return $this->_lastLink;
}

public function getResponse()
{
return $this->guzzleResponse;
}
}
}
88 changes: 64 additions & 24 deletions tests/Clients/GuzzleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public function testGuzzleInitialize()
'\Greenhouse\GreenhouseToolsPhp\Clients\GuzzleClient',
$client
);

$this->assertInstanceOf('\GuzzleHttp\Client', $client->getClient());
}

public function testGetException()
{
$errorUrl = 'https://api.greenhouse.io/v1/boards/exception_co/embed/';
$client = new GuzzleClient(array('base_uri' => $errorUrl));
$this->expectException('\Greenhouse\GreenhouseToolsPhp\Clients\Exceptions\GreenhouseAPIResponseException');
$response = $client->get('jobs');
}

public function testFormatPostParametersNoFiles()
{
$postVars = array(
Expand All @@ -47,23 +47,47 @@ public function testFormatPostParametersNoFiles()
['name' => 'talents[]', 'contents' => 'and'],
['name' => 'talents[]', 'contents' => 'stuff']
);

$this->assertEquals($expected, $this->client->formatPostParameters($postVars));
}

public function testFormatPostParametersWithFiles()
{
$testDoc = new \CURLFile($this->resumePath, 'application/msword', 'resume');

$postVars = array(
'first_name' => 'Hiram',
'last_name' => 'Abiff',
'talents' => array('building', 'things', 'and', 'stuff'),
'resume' => $testDoc
'first_name' => 'Hiram',
'last_name' => 'Abiff',
'talents' => array('building', 'things', 'and', 'stuff'),
'resume' => $testDoc,
'demographic_answers' => [
[
'question_id' => '1',
'answer_options' => [
['answer_option_id' => '100'],
['answer_option_id' => '101']
]
]
],
'educations' => [
[
"school_name_id"=>5417077,
"degree_id" => 5494452,
"discipline_id" => 5494865,
"start_date" => [
"month" => 8,
"year" => 2012
],
"end_date" => [
"month" => 5,
"year" => 2016
]
]
]
);

$response = $this->client->formatPostParameters($postVars);

$this->assertEquals($response[0], ['name' => 'first_name', 'contents' => 'Hiram']);
$this->assertEquals($response[1], ['name' => 'last_name', 'contents' => 'Abiff']);
$this->assertEquals($response[2], ['name' => 'talents[]', 'contents' => 'building']);
Expand All @@ -72,8 +96,24 @@ public function testFormatPostParametersWithFiles()
$this->assertEquals($response[5], ['name' => 'talents[]', 'contents' => 'stuff']);
$this->assertEquals($response[6]['name'], 'resume');
$this->assertEquals($response[6]['filename'], 'resume');
$this->assertEquals($response[7], ['name' => 'demographic_answers[]question_id', 'contents' => '1']);
$this->assertEquals($response[8], [
'name' => 'demographic_answers[]answer_options[][answer_option_id]',
'contents' => '100'
]);
$this->assertEquals($response[9], [
'name' => 'demographic_answers[]answer_options[][answer_option_id]',
'contents' => '101'
]);
$this->assertEquals($response[10], ['name' => 'educations[]school_name_id', 'contents' => '5417077']);
$this->assertEquals($response[11], ['name' => 'educations[]degree_id', 'contents' => '5494452']);
$this->assertEquals($response[12], ['name' => 'educations[]discipline_id', 'contents' => '5494865']);
$this->assertEquals($response[13], ['name' => 'educations[]start_date[month]', 'contents' => '8']);
$this->assertEquals($response[14], ['name' => 'educations[]start_date[year]', 'contents' => '2012']);
$this->assertEquals($response[15], ['name' => 'educations[]end_date[month]', 'contents' => '5']);
$this->assertEquals($response[16], ['name' => 'educations[]end_date[year]', 'contents' => '2016']);
}

public function testLinksAllIncluded()
{
$linksResponse = array(
Expand All @@ -90,22 +130,22 @@ public function testLinksAllIncluded()
$reflector = new \ReflectionClass('Greenhouse\GreenhouseToolsPhp\Clients\GuzzleClient');
$method = $reflector->getMethod('_setLinks');
$method->setAccessible(true);

$this->assertEquals($this->client->getNextLink(), '');
$this->assertEquals($this->client->getPrevLink(), '');
$this->assertEquals($this->client->getLastLink(), '');

$method->invokeArgs($this->client, array());

$this->assertEquals($this->client->getNextLink(), 'https://harvest.greenhouse.io/v1/candidates?page=3&per_page=100');
$this->assertEquals($this->client->getPrevLink(), 'https://harvest.greenhouse.io/v1/candidates?page=1&per_page=100');
$this->assertEquals($this->client->getLastLink(), 'https://harvest.greenhouse.io/v1/candidates?page=8273&per_page=100');
}

public function testLinksNoneIncluded()
{
$linksResponse = array('');

$mockResponse = $this->createMock('Greenhouse\GreenhouseToolsPhp\Tests\Clients\Mocks\MockGuzzleResponse');
$mockResponse->method('getHeader')
->willReturn($linksResponse);
Expand All @@ -114,25 +154,25 @@ public function testLinksNoneIncluded()
$reflector = new \ReflectionClass('Greenhouse\GreenhouseToolsPhp\Clients\GuzzleClient');
$method = $reflector->getMethod('_setLinks');
$method->setAccessible(true);

$this->assertEquals($this->client->getNextLink(), '');
$this->assertEquals($this->client->getPrevLink(), '');
$this->assertEquals($this->client->getLastLink(), '');

$method->invokeArgs($this->client, array());

$this->assertEquals($this->client->getNextLink(), '');
$this->assertEquals($this->client->getPrevLink(), '');
$this->assertEquals($this->client->getLastLink(), '');
}

public function testLinksSomeIncluded()
{
$linksResponse = array(
'<https://harvest.greenhouse.io/v1/candidates?page=1&per_page=100>; rel="prev",' .
'<https://harvest.greenhouse.io/v1/candidates?page=8273&per_page=100>; rel="last"'
);

$mockResponse = $this->createMock('Greenhouse\GreenhouseToolsPhp\Tests\Clients\Mocks\MockGuzzleResponse');
$mockResponse->method('getHeader')
->willReturn($linksResponse);
Expand All @@ -141,15 +181,15 @@ public function testLinksSomeIncluded()
$reflector = new \ReflectionClass('Greenhouse\GreenhouseToolsPhp\Clients\GuzzleClient');
$method = $reflector->getMethod('_setLinks');
$method->setAccessible(true);

$this->assertEquals($this->client->getNextLink(), '');
$this->assertEquals($this->client->getPrevLink(), '');
$this->assertEquals($this->client->getLastLink(), '');

$method->invokeArgs($this->client, array());

$this->assertEquals($this->client->getNextLink(), '');
$this->assertEquals($this->client->getPrevLink(), 'https://harvest.greenhouse.io/v1/candidates?page=1&per_page=100');
$this->assertEquals($this->client->getLastLink(), 'https://harvest.greenhouse.io/v1/candidates?page=8273&per_page=100');
$this->assertEquals($this->client->getLastLink(), 'https://harvest.greenhouse.io/v1/candidates?page=8273&per_page=100');
}
}
}

0 comments on commit 6cd22e8

Please sign in to comment.