Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ filter:
build:
environment:
php:
version: 7.0
version: 7.0.8
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, revert this change

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. It was just a blind guess. Scruntinizer still does not work.

dependencies:
before:
- composer require symfony/symfony:3.0.*
Expand Down
33 changes: 17 additions & 16 deletions Api/CurrentWeatherData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -84,59 +85,59 @@ 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)])
);
}

/**
* 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]));
}

/**
* Call by city name or city name and country code
*
* @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)])
);
}

Expand Down
74 changes: 74 additions & 0 deletions Api/CurrentWeatherDataResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Savchenko\Bundle\OpenWeatherMapBundle\Api;

use Psr\Http\Message\ResponseInterface;

class CurrentWeatherDataResult
{
private $data;

/**
* @var ResponseInterface
*/
private $response;

private function __construct($data, ResponseInterface $response = null)
{
$this->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');
}
}
59 changes: 59 additions & 0 deletions Tests/Api/CurrentWeatherDataResultTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Savchenko\Bundle\OpenWeatherMapBundle\Tests\Api;

use GuzzleHttp\Psr7\Response;
use Savchenko\Bundle\OpenWeatherMapBundle\Api\CurrentWeatherDataResult;

class CurrentWeatherDataResultTest extends \PHPUnit_Framework_TestCase
{
use FakeWeatherDataTrait;

private $response;

public function setUp()
{

$this->response = $this->prophesize(Response::class);
}

/**
* @test
*/
public function It_should_save_the_response_when_constructed_from_response()
{

$this->response->getBody()->willReturn($this->getWeatherData());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplication

Copy link
Author

@mablae mablae Jul 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean mocking the response? Yeah, stupid since they are immutable. Thanks for your valuable feedback. Better not double it at all.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are right :)


$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']);

}

}
46 changes: 3 additions & 43 deletions Tests/Api/CurrentWeatherDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class CurrentWeatherDataTest extends \PHPUnit_Framework_TestCase
{

use FakeWeatherDataTrait;

/**
* @var Client|\PHPUnit_Framework_MockObject_MockObject
*/
Expand All @@ -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}
*/
Expand Down
54 changes: 54 additions & 0 deletions Tests/Api/FakeWeatherDataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Savchenko\Bundle\OpenWeatherMapBundle\Tests\Api;

trait FakeWeatherDataTrait
{

/**
* 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',
]
);
}
}