Skip to content

Commit

Permalink
Adding support for multiple jobs from each returned job if multiple l…
Browse files Browse the repository at this point in the history
…ocations are present
  • Loading branch information
karllhughes committed May 3, 2015
1 parent 79417a3 commit 28941fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
36 changes: 24 additions & 12 deletions src/Govt.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function createJobObject($payload)
'id',
'position_title',
'organization_name',
'locations',
'location',
'start_date',
'end_date',
'url',
Expand All @@ -34,6 +34,7 @@ public function createJobObject($payload)
'title' => $payload['position_title'],
'url' => $payload['url'],
'company' => $payload['organization_name'],
'location' => $payload['location'],
'minimumSalary' => $payload['minimum'],
'maximumSalary' => $payload['maximum'],
'startDate' => $payload['start_date'],
Expand All @@ -42,10 +43,6 @@ public function createJobObject($payload)

$job->addCodes($payload['rate_interval_code']);

if (is_array($payload['locations']) && isset($payload['locations'][0])) {
$job->setLocation($payload['locations'][0]);
}

return $job;
}

Expand All @@ -60,17 +57,32 @@ protected function getJobsCollectionFromListings(array $listings = array())
{
$collection = new Collection;
array_map(function ($item) use ($collection) {

// Add stuff to handle multiple locations here...

$job = $this->createJobObject($item);
$job->setQuery($this->keyword)
->setSource($this->getSource());
$collection->add($job);
$jobs = $this->createJobArray($item);
foreach ($jobs as $item) {
$job = $this->createJobObject($item);
$job->setQuery($this->keyword)
->setSource($this->getSource());
$collection->add($job);
}
}, $listings);
return $collection;
}

public function createJobArray($item)
{
$jobs = [];
if (isset($item['locations']) && count($item['locations']) > 1) {
foreach ($item['locations'] as $location) {
$item['location'] = $location;
$jobs[] = $item;
}
} else {
$item['location'] = $item['locations'][0];
$jobs[] = $item;
}
return $jobs;
}

/**
* Get data format
*
Expand Down
28 changes: 20 additions & 8 deletions tests/src/GovtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ public function testUrlNotIncludesFromWhenNotProvided()
$this->assertNotContains($param, $url);
}

public function testItCreatesMultipleJobsWhenMultipleLocationsReturned()
{
$job_count = 1;
$loc_count = rand(1,3);
$jobs = $this->createJobArray($job_count, $loc_count);

$array = $this->client->createJobArray($jobs[0]);

foreach ($array as $key => $job) {
$this->assertEquals($jobs[0]['position_title'], $array[0]['position_title']);
$this->assertEquals($jobs[0]['locations'][$key], $array[$key]['location']);
}
$this->assertEquals(($job_count*$loc_count), count($array));
}

public function testItCanConnect()
{
$job_count = rand(2,10);
Expand Down Expand Up @@ -161,25 +176,22 @@ public function testItCanConnect()
$this->assertEquals($listings[$i]['end_date'], $results->get($i)->endDate);
$this->assertEquals($listings[$i]['minimum'], $results->get($i)->minimumSalary);
$this->assertEquals($listings[$i]['maximum'], $results->get($i)->maximumSalary);

// Add assertion related to locations

$this->assertEquals($keyword, $results->get($i)->query);
$this->assertEquals($source, $results->get($i)->source);
}

$this->assertEquals(count($listings), $results->count());
}

private function createJobArray($num = 10) {
private function createJobArray($job_count = 10, $loc_count = 1) {
$jobs = [];
$i = 0;
while ($i < $num) {
while ($i < $job_count) {
$jobs[] = [
'id' => uniqid(),
'position_title' => uniqid(),
'organization_name' => uniqid(),
'locations' => $this->createLocationsArray(),
'locations' => $this->createLocationsArray($loc_count),
'start_date' => uniqid(),
'end_date' => uniqid(),
'url' => uniqid(),
Expand All @@ -191,10 +203,10 @@ private function createJobArray($num = 10) {
return $jobs;
}

private function createLocationsArray($num = 2) {
private function createLocationsArray($loc_count = 3) {
$locations = [];
$i = 0;
while ($i < $num) {
while ($i < $loc_count) {
$locations[] = uniqid();
$i++;
}
Expand Down

0 comments on commit 28941fc

Please sign in to comment.