diff --git a/CHANGELOG.md b/CHANGELOG.md index 1094db2..6fb2286 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Providers/AbstractProvider.php b/src/Providers/AbstractProvider.php index e0dedb0..581d035 100644 --- a/src/Providers/AbstractProvider.php +++ b/src/Providers/AbstractProvider.php @@ -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); @@ -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 * diff --git a/test/src/ProviderTest.php b/test/src/ProviderTest.php index 250a3ba..02a514c 100644 --- a/test/src/ProviderTest.php +++ b/test/src/ProviderTest.php @@ -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; @@ -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 = ""; + + for ($i = 0; $i < $provider['jobs_count']; $i++) { + $title = uniqid(); + $path = $provider['path']; + array_push($payload[$provider['path']], ['title' => $title]); + $responseBody .= "<$path>$title"; + } + + $responseBody .= ""; + + $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); + } }