-
Notifications
You must be signed in to change notification settings - Fork 1
Add result object that holds response and has helper methods #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'); | ||
| } | ||
| } |
| 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()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code duplication
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']); | ||
|
|
||
| } | ||
|
|
||
| } | ||
| 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', | ||
| ] | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, revert this change
There was a problem hiding this comment.
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.