From e8c46b7dd90b5a29564a4686892a280ca5d0bf9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Bl=C3=A4ttermann?= Date: Sun, 17 Jul 2016 20:50:53 +0200 Subject: [PATCH 1/2] Add result object that holds response and has helper methods --- Api/CurrentWeatherData.php | 33 +++++----- Api/CurrentWeatherDataResult.php | 74 ++++++++++++++++++++++ Tests/Api/CurrentWeatherDataResultTest.php | 59 +++++++++++++++++ Tests/Api/CurrentWeatherDataTest.php | 46 +------------- Tests/Api/FakeWeatherDataTrait.php | 54 ++++++++++++++++ 5 files changed, 207 insertions(+), 59 deletions(-) create mode 100644 Api/CurrentWeatherDataResult.php create mode 100644 Tests/Api/CurrentWeatherDataResultTest.php create mode 100644 Tests/Api/FakeWeatherDataTrait.php diff --git a/Api/CurrentWeatherData.php b/Api/CurrentWeatherData.php index 08ce68f..e93e871 100644 --- a/Api/CurrentWeatherData.php +++ b/Api/CurrentWeatherData.php @@ -41,7 +41,8 @@ public function __construct(string $apiKey, Client $client) * @throws BadRequestException * @throws GuzzleException */ - private function sendRequest(array $params) : ResponseInterface { + private function sendRequest(array $params) : ResponseInterface + { $params = array_merge(['appid' => $this->apiKey], $params); try { @@ -84,15 +85,15 @@ private function validateCountryCode(string $countryCode) :bool * * @param string $cityName * @param string $countryCode optional country code in ISO 3166-1 alpha-2 format - * @return \stdClass + * @return CurrentWeatherDataResult * @throws GuzzleException on request exception * @throws BadRequestException * @throws InvalidCountryCodeException on incorrect country code */ - public function loadByCityName(string $cityName, string $countryCode = null) : \stdClass + public function loadByCityName(string $cityName, string $countryCode = null) : CurrentWeatherDataResult { - return json_decode( - $this->sendRequest(['q' => $this->addCountryCodeToBase($cityName, $countryCode)])->getBody() + return CurrentWeatherDataResult::fromApiResponse( + $this->sendRequest(['q' => $this->addCountryCodeToBase($cityName, $countryCode)]) ); } @@ -100,27 +101,27 @@ public function loadByCityName(string $cityName, string $countryCode = null) : \ * Call by city id * * @param string $cityCode - * @return \stdClass + * @return CurrentWeatherDataResult * @throws GuzzleException on request exception * @throws BadRequestException */ - public function loadByCityId(string $cityCode) : \stdClass + public function loadByCityId(string $cityCode) : CurrentWeatherDataResult { - return json_decode($this->sendRequest(['id' => $cityCode])->getBody()); + return CurrentWeatherDataResult::fromApiResponse($this->sendRequest(['id' => $cityCode])); } /** * Call by geographic coordinates * - * @param float $lat location latitude + * @param float $lat location latitude * @param float $lon location longitude - * @return \stdClass + * @return CurrentWeatherDataResult * @throws GuzzleException on request exception * @throws BadRequestException */ - public function loadByGeographicCoordinates(float $lat, float $lon) : \stdClass + public function loadByGeographicCoordinates(float $lat, float $lon) : CurrentWeatherDataResult { - return json_decode($this->sendRequest(['lat' => $lat, 'lon' => $lon])->getBody()); + return CurrentWeatherDataResult::fromApiResponse($this->sendRequest(['lat' => $lat, 'lon' => $lon])); } /** @@ -128,15 +129,15 @@ public function loadByGeographicCoordinates(float $lat, float $lon) : \stdClass * * @param string $zipCode zip code * @param string $countryCode optional country code in ISO 3166-1 alpha-2 format - * @return \stdClass + * @return CurrentWeatherDataResult * @throws GuzzleException on request exception * @throws BadRequestException * @throws InvalidCountryCodeException on incorrect country code */ - public function loadByZipCode(string $zipCode, string $countryCode = null) : \stdClass + public function loadByZipCode(string $zipCode, string $countryCode = null) : CurrentWeatherDataResult { - return json_decode( - $this->sendRequest(['zip' => $this->addCountryCodeToBase($zipCode, $countryCode)])->getBody() + return CurrentWeatherDataResult::fromApiResponse( + $this->sendRequest(['zip' => $this->addCountryCodeToBase($zipCode, $countryCode)]) ); } diff --git a/Api/CurrentWeatherDataResult.php b/Api/CurrentWeatherDataResult.php new file mode 100644 index 0000000..29b5aea --- /dev/null +++ b/Api/CurrentWeatherDataResult.php @@ -0,0 +1,74 @@ +data = $data; + + if ($response) { + $this->response = $response; + } + + } + + /** + * @param string $jsonString + * @return CurrentWeatherDataResult + */ + public static function fromJsonString($jsonString) + { + return new self(json_decode($jsonString)); + } + + public static function fromApiResponse(ResponseInterface $response) + { + return new self(json_decode($response->getBody()), $response); + } + + /** + * @return ResponseInterface + */ + public function getResponse() + { + if ($this->response) { + return $this->response; + } + } + + /** + * @return array + */ + public function asArray() + { + return json_decode($this->asJson(), true); + } + + /** + * @return string + */ + public function asJson() + { + return json_encode($this->data); + } + + public function __get($name) + { + if (property_exists($this->data, $name)) { + return $this->data->{$name}; + } + + throw new \InvalidArgumentException('Key not found in result'); + } +} \ No newline at end of file diff --git a/Tests/Api/CurrentWeatherDataResultTest.php b/Tests/Api/CurrentWeatherDataResultTest.php new file mode 100644 index 0000000..d60238b --- /dev/null +++ b/Tests/Api/CurrentWeatherDataResultTest.php @@ -0,0 +1,59 @@ +response = $this->prophesize(Response::class); + } + + /** + * @test + */ + public function It_should_save_the_response_when_constructed_from_response() + { + + $this->response->getBody()->willReturn($this->getWeatherData()); + + $sut = CurrentWeatherDataResult::fromApiResponse($this->response->reveal()); + $this->assertSame('London', $sut->name); + $this->assertSame($this->response->reveal(), $sut->getResponse()); + + } + + /** + * @test + */ + public function The_result_can_be_rendered_as_array() + { + + $this->response->getBody()->willReturn($this->getWeatherData()); + + $sut = CurrentWeatherDataResult::fromApiResponse($this->response->reveal()); + $this->assertSame($this->getWeatherData(), $sut->asJson()); + + } + + /** + * @test + */ + public function The_result_can_be_rendered_as_json() + { + + $this->response->getBody()->willReturn($this->getWeatherData()); + $sut = CurrentWeatherDataResult::fromApiResponse($this->response->reveal()); + $this->assertSame('London', $sut->asArray()['name']); + + } + +} diff --git a/Tests/Api/CurrentWeatherDataTest.php b/Tests/Api/CurrentWeatherDataTest.php index 0989f85..0849e1e 100644 --- a/Tests/Api/CurrentWeatherDataTest.php +++ b/Tests/Api/CurrentWeatherDataTest.php @@ -10,6 +10,9 @@ class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase { + + use FakeWeatherDataTrait; + /** * @var Client|\PHPUnit_Framework_MockObject_MockObject */ @@ -19,49 +22,6 @@ class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase */ protected $currentWeatherData; - /** - * Get OpenWeatherMap response example - * - * @return string - */ - protected function getWeatherData() - { - return json_encode([ - 'coord' => [ - 'lon' => -0.13, - 'lat' => 51.51, - ], - 'weather' => [[ - 'id' => 800, - 'main' => 'Clear', - 'description' => 'clear sky', - 'icon' => '01n', - ]], - 'main' => [ - 'temp' => 282.94, - 'pressure' => 1007, - 'humidity' => 71, - 'temp_min' => 281.15, - 'temp_max' => 285.25, - ], - 'wind' => [ - 'speed' => 3.6, - 'deg' => 100, - ], - 'clouds' => [ - 'all' => 0, - ], - 'dt' => 1460493742, - 'sys' => [ - 'country' => 'GB', - 'sunrise' => 1460437723, - 'sunset' => 1460487258, - ], - 'id' => 2643743, - 'name' => 'London', - ]); - } - /** * {@inheritdoc} */ diff --git a/Tests/Api/FakeWeatherDataTrait.php b/Tests/Api/FakeWeatherDataTrait.php new file mode 100644 index 0000000..e69d4c8 --- /dev/null +++ b/Tests/Api/FakeWeatherDataTrait.php @@ -0,0 +1,54 @@ + [ + 'lon' => -0.13, + 'lat' => 51.51, + ], + 'weather' => [ + [ + 'id' => 800, + 'main' => 'Clear', + 'description' => 'clear sky', + 'icon' => '01n', + ], + ], + 'main' => [ + 'temp' => 282.94, + 'pressure' => 1007, + 'humidity' => 71, + 'temp_min' => 281.15, + 'temp_max' => 285.25, + ], + 'wind' => [ + 'speed' => 3.6, + 'deg' => 100, + ], + 'clouds' => [ + 'all' => 0, + ], + 'dt' => 1460493742, + 'sys' => [ + 'country' => 'GB', + 'sunrise' => 1460437723, + 'sunset' => 1460487258, + ], + 'id' => 2643743, + 'name' => 'London', + ] + ); + } +} From 337661336a0b75496c3d33fad48a7910c0b70b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Bl=C3=A4ttermann?= Date: Sun, 17 Jul 2016 21:09:26 +0200 Subject: [PATCH 2/2] Fix .scruntinizer.yml config --- .scrutinizer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 590b477..d9e801e 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -8,7 +8,7 @@ filter: build: environment: php: - version: 7.0 + version: 7.0.8 dependencies: before: - composer require symfony/symfony:3.0.*