Skip to content

Commit

Permalink
Merge pull request #27 from JobBrander/guzzle-upgrade
Browse files Browse the repository at this point in the history
adding support for Guzzle 6.0; concrete format parsers
  • Loading branch information
karllhughes committed Jun 7, 2015
2 parents 6bb6f2a + 7c5eac8 commit 861c532
Show file tree
Hide file tree
Showing 3 changed files with 304 additions and 47 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Changelog
All Notable changes to `jobs-common` will be documented in this file

## 0.7.0 - 2015-06-07

### Added
- Support for Guzzle v.6.0 in abstract provider

### Deprecated
- xml() and json() methods previously used in provider are no longer supported by Guzzle

### Fixed
- Nothing

### Security
- Nothing

## 0.6.1 - 2015-05-27

### Added
Expand Down
71 changes: 69 additions & 2 deletions src/Providers/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ public function getJobs()

$response = $client->{$verb}($url, $options);

$payload = $response->{$this->getFormat()}();
$payload = json_decode(json_encode($payload), true);
$body = (string) $response->getBody();

$payload = $this->parseAsFormat($body, $this->getFormat());

$listings = $this->getRawListings($payload);

Expand Down Expand Up @@ -288,6 +289,72 @@ public static function parseAttributeDefaults(array $attributes, array $defaults
return $attributes;
}

/**
* Attempt to parse string as given format
*
* @param string $string
* @param string $format
*
* @return array
*/
private function parseAsFormat($string, $format)
{
$method = 'parseAs'.ucfirst(strtolower($format));

if (method_exists($this, $method)) {
return $this->$method($string);
}

return [];
}

/**
* Attempt to parse as Json
*
* @param string $string
*
* @return array
*/
private function parseAsJson($string)
{
try {
$json = json_decode($string, true);

if (json_last_error() != JSON_ERROR_NONE) {
throw new \Exception;
}

return $json;
} catch (\Exception $e) {
}

return [];
}

/**
* Attempt to parse as XML
*
* @param string $string
*
* @return array
*/
private function parseAsXml($string)
{
try {
return json_decode(
json_encode(
simplexml_load_string(
$string
)
),
true
);
} catch (\Exception $e) {
}

return [];
}

/**
* Sets http client
*
Expand Down
266 changes: 221 additions & 45 deletions test/src/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,6 @@ public function testItPopulatesExistingAttributeswhenBuilt()
});
}

public function testItCanGetJobs()
{
$url = uniqid();
$verb = uniqid();
$path = uniqid();
$format = uniqid();
$keyword = uniqid();
$source = uniqid();
$params = [uniqid()];
$jobs_count = rand(2,10);
$payload = [$path => []];

for ($i = 0; $i < $jobs_count; $i++) {
array_push($payload[$path], ['title' => uniqid()]);
}

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($keyword)->times($jobs_count)->andReturnSelf();
$job->shouldReceive('setSource')->with($source)->times($jobs_count)->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($keyword);
$this->client->shouldReceive('createJobObject')->times($jobs_count)->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($format);
$this->client->shouldReceive('getSource')->andReturn($source);
$this->client->shouldReceive('getListingsPath')->andReturn($path);
$this->client->shouldReceive('getParameters')->andReturn($params);
$this->client->shouldReceive('getUrl')->andReturn($url);
$this->client->shouldReceive('getVerb')->andReturn($verb);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive($format)->once()->andReturn($payload);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($verb))
->with($url, $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($jobs_count, $results);
}

public function testDefaultCityIsNull()
{
$city = $this->client->city;
Expand Down Expand Up @@ -193,4 +148,225 @@ public function testItCanGetSource()

$this->assertNotNull($source);
}

public function testItCanGetJobsValidJson()
{
$provider = $this->getProviderAttributes();

$payload = [$provider['path'] => []];

for ($i = 0; $i < $provider['jobs_count']; $i++) {
array_push($payload[$provider['path']], ['title' => uniqid()]);
}

$responseBody = json_encode($payload);

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($provider['keyword'])
->times($provider['jobs_count'])->andReturnSelf();
$job->shouldReceive('setSource')->with($provider['source'])
->times($provider['jobs_count'])->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($provider['keyword']);
$this->client->shouldReceive('createJobObject')->times($provider['jobs_count'])->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($provider['format']);
$this->client->shouldReceive('getSource')->andReturn($provider['source']);
$this->client->shouldReceive('getListingsPath')->andReturn($provider['path']);
$this->client->shouldReceive('getParameters')->andReturn($provider['params']);
$this->client->shouldReceive('getUrl')->andReturn($provider['url']);
$this->client->shouldReceive('getVerb')->andReturn($provider['verb']);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive('getBody')->once()->andReturn($responseBody);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($provider['verb']))
->with($provider['url'], $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($provider['jobs_count'], $results);
}

public function testItCanNotGetJobsInvalidJson()
{
$provider = $this->getProviderAttributes([ 'path' => '', 'jobs_count' => 0 ]);

$responseBody = uniqid();

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($provider['keyword'])
->times($provider['jobs_count'])->andReturnSelf();
$job->shouldReceive('setSource')->with($provider['source'])
->times($provider['jobs_count'])->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($provider['keyword']);
$this->client->shouldReceive('createJobObject')->times($provider['jobs_count'])->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($provider['format']);
$this->client->shouldReceive('getSource')->andReturn($provider['source']);
$this->client->shouldReceive('getListingsPath')->andReturn($provider['path']);
$this->client->shouldReceive('getParameters')->andReturn($provider['params']);
$this->client->shouldReceive('getUrl')->andReturn($provider['url']);
$this->client->shouldReceive('getVerb')->andReturn($provider['verb']);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive('getBody')->once()->andReturn($responseBody);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($provider['verb']))
->with($provider['url'], $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($provider['jobs_count'], $results);
}

public function testItCanGetJobsValidXml()
{
$provider = $this->getProviderAttributes([
'path' => 'a'.uniqid(), 'format' => 'xml'
]);

$payload = [$provider['path'] => []];

$responseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><Root>";

for ($i = 0; $i < $provider['jobs_count']; $i++) {
$title = uniqid();
$path = $provider['path'];
array_push($payload[$provider['path']], ['title' => $title]);
$responseBody .= "<$path><title>$title</title></$path>";
}

$responseBody .= "</Root>";

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($provider['keyword'])
->times($provider['jobs_count'])->andReturnSelf();
$job->shouldReceive('setSource')->with($provider['source'])
->times($provider['jobs_count'])->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($provider['keyword']);
$this->client->shouldReceive('createJobObject')->times($provider['jobs_count'])->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($provider['format']);
$this->client->shouldReceive('getSource')->andReturn($provider['source']);
$this->client->shouldReceive('getListingsPath')->andReturn($provider['path']);
$this->client->shouldReceive('getParameters')->andReturn($provider['params']);
$this->client->shouldReceive('getUrl')->andReturn($provider['url']);
$this->client->shouldReceive('getVerb')->andReturn($provider['verb']);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive('getBody')->once()->andReturn($responseBody);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($provider['verb']))
->with($provider['url'], $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($provider['jobs_count'], $results);
}

public function testItCanNotGetJobsInvalidXml()
{
$provider = $this->getProviderAttributes([
'path' => '', 'format' => 'xml', 'jobs_count' => 0
]);
$responseBody = uniqid();

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($provider['keyword'])
->times($provider['jobs_count'])->andReturnSelf();
$job->shouldReceive('setSource')->with($provider['source'])
->times($provider['jobs_count'])->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($provider['keyword']);
$this->client->shouldReceive('createJobObject')->times($provider['jobs_count'])->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($provider['format']);
$this->client->shouldReceive('getSource')->andReturn($provider['source']);
$this->client->shouldReceive('getListingsPath')->andReturn($provider['path']);
$this->client->shouldReceive('getParameters')->andReturn($provider['params']);
$this->client->shouldReceive('getUrl')->andReturn($provider['url']);
$this->client->shouldReceive('getVerb')->andReturn($provider['verb']);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive('getBody')->once()->andReturn($responseBody);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($provider['verb']))
->with($provider['url'], $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($provider['jobs_count'], $results);
}

public function testItCanNotGetJobsInvalidFormat()
{
$provider = $this->getProviderAttributes([
'path' => '', 'format' => uniqid(), 'jobs_count' => 0
]);
$responseBody = uniqid();

$job = m::mock($this->jobClass);
$job->shouldReceive('setQuery')->with($provider['keyword'])
->times($provider['jobs_count'])->andReturnSelf();
$job->shouldReceive('setSource')->with($provider['source'])
->times($provider['jobs_count'])->andReturnSelf();

$this->client->shouldReceive('getKeyword')->andReturn($provider['keyword']);
$this->client->shouldReceive('createJobObject')->times($provider['jobs_count'])->andReturn($job);
$this->client->shouldReceive('getFormat')->andReturn($provider['format']);
$this->client->shouldReceive('getSource')->andReturn($provider['source']);
$this->client->shouldReceive('getListingsPath')->andReturn($provider['path']);
$this->client->shouldReceive('getParameters')->andReturn($provider['params']);
$this->client->shouldReceive('getUrl')->andReturn($provider['url']);
$this->client->shouldReceive('getVerb')->andReturn($provider['verb']);

$response = m::mock('GuzzleHttp\Message\Response');
$response->shouldReceive('getBody')->once()->andReturn($responseBody);

$http = m::mock('GuzzleHttp\Client');
$http->shouldReceive(strtolower($provider['verb']))
->with($provider['url'], $this->client->getHttpClientOptions())
->once()
->andReturn($response);
$this->client->setClient($http);

$results = $this->client->getJobs();

$this->assertInstanceOf($this->collectionClass, $results);
$this->assertCount($provider['jobs_count'], $results);
}

private function getProviderAttributes($attributes = [])
{
$defaults = [
'url' => uniqid(),
'verb' => uniqid(),
'path' => uniqid(),
'format' => 'json',
'keyword' => uniqid(),
'source' => uniqid(),
'params' => [uniqid()],
'jobs_count' => rand(2,10),
];
return array_replace($defaults, $attributes);
}
}

0 comments on commit 861c532

Please sign in to comment.