From 66f9f2bd5e4747d838a9dd7080a203c702c33aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20B=C3=B6hm?= Date: Fri, 20 Nov 2020 15:02:13 +0100 Subject: [PATCH] Add API call for existing campaigns of the user --- src/Api/Campaigns.php | 81 ++++++++++++++++++++++++++ src/Entity/Campaign.php | 65 +++++++++++++++++++++ tests/Unit/Api/CampaignsTest.php | 87 ++++++++++++++++++++++++++++ tests/Unit/Entity/CampaignTest.php | 93 ++++++++++++++++++++++++++++++ 4 files changed, 326 insertions(+) create mode 100644 src/Api/Campaigns.php create mode 100644 src/Entity/Campaign.php create mode 100644 tests/Unit/Api/CampaignsTest.php create mode 100644 tests/Unit/Entity/CampaignTest.php diff --git a/src/Api/Campaigns.php b/src/Api/Campaigns.php new file mode 100644 index 0000000..317c13a --- /dev/null +++ b/src/Api/Campaigns.php @@ -0,0 +1,81 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace SkillDisplay\PHPToolKit\Api; + +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Psr7\Request; +use SkillDisplay\PHPToolKit\Configuration\Settings; +use SkillDisplay\PHPToolKit\Entity\Campaign; + +class Campaigns +{ + /** + * @var Settings + */ + protected $settings; + + /** + * @var Client + */ + protected $client; + + public function __construct( + Settings $settings, + Client $client + ) { + $this->settings = $settings; + $this->client = $client; + } + + public function getForUser(): array + { + $url = $this->settings->getAPIUrl() . '/api/v1/campaigns'; + try { + $result = $this->client->send(new Request( + 'GET', + $url, + [ + 'Content-Type' => 'application/json', + 'x-api-key' => $this->settings->getApiKey() + ] + )); + } catch (ClientException $e) { + if ($e->getCode() === 400) { + throw new \InvalidArgumentException($e->getMessage(), 1605877581); + } + throw $e; + } + + if ($result->getStatusCode() !== 200) { + throw new \Exception('API key or user invalid.', 1605878128); + } + $body = json_decode($result->getBody(), true); + $campaigns = []; + foreach ($body['Campaigns'] as $campaign) { + $campaigns[] = new Campaign($campaign, $this->settings); + } + return $campaigns; + } +} diff --git a/src/Entity/Campaign.php b/src/Entity/Campaign.php new file mode 100644 index 0000000..27e4b08 --- /dev/null +++ b/src/Entity/Campaign.php @@ -0,0 +1,65 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +namespace SkillDisplay\PHPToolKit\Entity; + +use SkillDisplay\PHPToolKit\Configuration\Settings; + +class Campaign +{ + /** + * @var array + */ + private $data; + + /** + * @var Settings + */ + private $settings; + + public function __construct(array $data, Settings $settings) + { + $this->data = $data; + $this->settings = $settings; + } + + public function getId(): int + { + return $this->data['uid']; + } + + public function getTitle(): string + { + return $this->data['title'] ?? ''; + } + // In order to support frameworks / APIs that expect "getter". + public function getAsArray(): array + { + return $this->toArray(); + } + + public function toArray(): array + { + return $this->data; + } +} diff --git a/tests/Unit/Api/CampaignsTest.php b/tests/Unit/Api/CampaignsTest.php new file mode 100644 index 0000000..e2adb64 --- /dev/null +++ b/tests/Unit/Api/CampaignsTest.php @@ -0,0 +1,87 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use GuzzleHttp\Client; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use Prophecy\Argument; +use Prophecy\PhpUnit\ProphecyTrait; +use SkillDisplay\PHPToolKit\Api\Campaigns; +use PHPUnit\Framework\TestCase; +use SkillDisplay\PHPToolKit\Configuration\Settings; + +/** + * @covers SkillDisplay\PHPToolKit\Api\Campaigns + */ +class CampaignsTest extends TestCase +{ + use ProphecyTrait; + + /** + * @test + */ + public function instanceCanBeCreated() + { + $settings = $this->prophesize(Settings::class); + $client = $this->prophesize(Client::class); + + $subject = new Campaigns( + $settings->reveal(), + $client->reveal() + ); + + static::assertInstanceOf(Campaigns::class, $subject); + } + + /** + * @test + */ + public function fetchedResultFromApi() + { + $settings = $this->prophesize(Settings::class); + $client = $this->prophesize(Client::class); + $response = $this->prophesize(Response::class); + + $settings->getAPIUrl()->willReturn('https://example.com'); + $settings->getApiKey()->willReturn('none'); + + $client->send(Argument::that(function (Request $request) { + return (string)$request->getUri() === 'https://example.com/api/v1/campaigns' + && $request->getHeader('x-api-key') === ['none'] + && $request->getHeader('Content-Type') === ['application/json'] + && $request->getMethod() === 'GET'; + }))->willReturn($response->reveal()); + + $response->getStatusCode()->willReturn(200); + $response->getBody()->willReturn('{"Version": "1.0","ErrorMessage": "","Campaigns": [{"uid": 1},{"uid": 2}]}'); + + $subject = new Campaigns( + $settings->reveal(), + $client->reveal() + ); + $result = $subject->getForUser(); + static::assertSame(2, count($result)); + } +} diff --git a/tests/Unit/Entity/CampaignTest.php b/tests/Unit/Entity/CampaignTest.php new file mode 100644 index 0000000..9f98ea8 --- /dev/null +++ b/tests/Unit/Entity/CampaignTest.php @@ -0,0 +1,93 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use PHPUnit\Framework\TestCase; +use Prophecy\PhpUnit\ProphecyTrait; +use SkillDisplay\PHPToolKit\Configuration\Settings; +use SkillDisplay\PHPToolKit\Entity\Campaign; + +/** + * @covers SkillDisplay\PHPToolKit\Entity\Campaign + */ +class CampaignTest extends TestCase +{ + use ProphecyTrait; + + /** + * @test + */ + public function instanceCanBeCreatedFromJson() + { + $settings = $this->prophesize(Settings::class); + $subject = new Campaign([], $settings->reveal()); + static::assertInstanceOf(Campaign::class, $subject); + } + + /** + * @test + */ + public function instanceReturnsId() + { + $settings = $this->prophesize(Settings::class); + $subject = new Campaign(['uid' => 1], $settings->reveal()); + static::assertSame(1, $subject->getId()); + } + + /** + * @test + */ + public function instanceReturnsTitle() + { + $settings = $this->prophesize(Settings::class); + $subject = new Campaign(['title' => 'Example Campaign'], $settings->reveal()); + static::assertSame('Example Campaign', $subject->getTitle()); + } + + /** + * @test + */ + public function canReturnAsArray() + { + $settings = $this->prophesize(Settings::class); + $subject = new Campaign(['uid' => 1, 'title' => 'Example Campaign'], $settings->reveal()); + static::assertSame([ + 'uid' => 1, + 'title' => 'Example Campaign', + ], $subject->getAsArray()); + } + + /** + * @test + */ + public function canBeConvertedToArray() + { + $settings = $this->prophesize(Settings::class); + $subject = new Campaign(['uid' => 1, 'title' => 'Example Campaign'], $settings->reveal()); + static::assertSame([ + 'uid' => 1, + 'title' => 'Example Campaign', + ], $subject->toArray()); + } +}