mirror of
https://github.com/SkillDisplay/PHPToolKit.git
synced 2024-11-21 15:36:10 +01:00
added SkillSetProgress with Tests
This commit is contained in:
parent
55ced21f7d
commit
9a335a7486
6 changed files with 401 additions and 0 deletions
80
src/Api/SkillSetProgress.php
Normal file
80
src/Api/SkillSetProgress.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Api;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2023 Julian Zangl <julian.zangl@outlook.com>
|
||||
*
|
||||
* 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\Exception\ClientException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\PHPToolKit\Entity\SkillSetProgress as Entity;
|
||||
|
||||
class SkillSetProgress
|
||||
{
|
||||
protected Settings $settings;
|
||||
protected Client $client;
|
||||
|
||||
public function __construct(
|
||||
Settings $settings,
|
||||
Client $client
|
||||
) {
|
||||
$this->settings = $settings;
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
public function getById(int $id): Entity
|
||||
{
|
||||
if ($id <= 0) {
|
||||
throw new \Exception('ID of SkillSet has to be a positive integer.', 1688639724754);
|
||||
}
|
||||
|
||||
$url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id . '/progress';
|
||||
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() === 404) {
|
||||
throw new \InvalidArgumentException('Given SkillSet with id "' . $id . '" not available.', 1688639748718);
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($result->getStatusCode() !== 200) {
|
||||
throw new \Exception('Did not get proper response for SkillSetProgress.', 1688639840720);
|
||||
}
|
||||
|
||||
$body = (string)$result->getBody();
|
||||
|
||||
if (strpos($body, 'Oops, an error occurred') !== false) {
|
||||
throw new \Exception('Did not get proper response for SkillSetProgress. SkillSet with id "' . $id . '" does probably not exist.', 1688639873065);
|
||||
}
|
||||
|
||||
return Entity::createFromJson($body, $this->settings);
|
||||
}
|
||||
}
|
|
@ -56,6 +56,11 @@ class SkillSet
|
|||
return $this->data['description'] ?? '';
|
||||
}
|
||||
|
||||
public function getProgressPercentage(): SkillSetProgress
|
||||
{
|
||||
return SkillSetProgress::createFromJson(json_encode($this->data['progressPercentage']), $this->settings);
|
||||
}
|
||||
|
||||
public function getBrand(): Brand
|
||||
{
|
||||
if ($this->brand instanceof Brand) {
|
||||
|
|
63
src/Entity/SkillSetProgress.php
Normal file
63
src/Entity/SkillSetProgress.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Entity;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2023 Julian Zangl <julian.zangl@outlook.com>
|
||||
*
|
||||
* 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 SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
|
||||
class SkillSetProgress {
|
||||
private array $data;
|
||||
|
||||
private Settings $settings;
|
||||
|
||||
private function __construct(array $data, Settings $settings)
|
||||
{
|
||||
$this->data = $data;
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
public function getTier1(): float
|
||||
{
|
||||
return $this->data['tier1'] ?? 0;
|
||||
}
|
||||
|
||||
public function getTier2(): float
|
||||
{
|
||||
return $this->data['tier2'] ?? 0;
|
||||
}
|
||||
|
||||
public function getTier3(): float
|
||||
{
|
||||
return $this->data['tier3'] ?? 0;
|
||||
}
|
||||
|
||||
public function getTier4(): float
|
||||
{
|
||||
return $this->data['tier4'] ?? 0;
|
||||
}
|
||||
|
||||
public static function createFromJson(string $json, Settings $settings): SkillSetProgress
|
||||
{
|
||||
return new SkillSetProgress(json_decode($json, true), $settings);
|
||||
}
|
||||
}
|
150
tests/Unit/Api/SkillSetProgressTest.php
Normal file
150
tests/Unit/Api/SkillSetProgressTest.php
Normal file
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2023 Julian Zangl <julian.zangl@outlook.com>
|
||||
*
|
||||
* 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\SkillSetProgress;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\PHPToolKit\Entity\SkillSetProgress as Entity;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\PHPToolKit\Api\SkillSetProgress
|
||||
*/
|
||||
|
||||
class SkillSetProgressTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreated()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
|
||||
$subject = new SkillSetProgress(
|
||||
$settings->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
|
||||
static::assertInstanceOf(SkillSetProgress::class, $subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function fetchedEntityFromApi()
|
||||
{
|
||||
$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/skillset/10/progress'
|
||||
&& $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('{"tier3":44.44444444444444,"tier2":40.74074074074074,"tier1":0,"tier4":0}');
|
||||
|
||||
$progress = new SkillSetProgress(
|
||||
$settings->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
$result = $progress->getById(10);
|
||||
static::assertInstanceOf(Entity::class, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function throwsExceptionIfStatusCodeIsNot200()
|
||||
{
|
||||
$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::any())->willReturn($response->reveal());
|
||||
|
||||
$response->getStatusCode()->willReturn(500);
|
||||
|
||||
$progress = new SkillSetProgress(
|
||||
$settings->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
|
||||
$this->expectExceptionMessage('Did not get proper response for SkillSetProgress.');
|
||||
$this->expectExceptionCode(1688639840720);
|
||||
$progress->getById(10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider nonePositiveIds
|
||||
*/
|
||||
public function throwsExceptionIfSkillSetIdIsNotPositive(int $skillId)
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$client = $this->prophesize(Client::class);
|
||||
|
||||
$progress = new SkillSetProgress(
|
||||
$settings->reveal(),
|
||||
$client->reveal()
|
||||
);
|
||||
|
||||
$this->expectExceptionMessage('ID of SkillSet has to be a positive integer.');
|
||||
$this->expectExceptionCode(1688639724754);
|
||||
|
||||
$progress->getById($skillId);
|
||||
}
|
||||
|
||||
public function nonePositiveIds(): array
|
||||
{
|
||||
return [
|
||||
'zero' => [
|
||||
'skillId' => 0,
|
||||
],
|
||||
'negative' => [
|
||||
'skillId' => -1,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
}
|
88
tests/Unit/Entity/SkillSetProgressTest.php
Normal file
88
tests/Unit/Entity/SkillSetProgressTest.php
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2023 Julian Zangl <julian.zangl@outlook.com>
|
||||
*
|
||||
* 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 Prophecy\PhpUnit\ProphecyTrait;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use SkillDisplay\PHPToolKit\Configuration\Settings;
|
||||
use SkillDisplay\PHPToolKit\Entity\SkillSetProgress;
|
||||
|
||||
/**
|
||||
* @covers SkillDisplay\PHPToolKit\Entity\SkillSetProgress
|
||||
*/
|
||||
class SkillSetProgressTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceCanBeCreatedFromJson()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$progress = SkillSetProgress::createFromJson('{}', $settings->reveal());
|
||||
static::assertInstanceOf(SkillSetProgress::class, $progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsTier1()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$progress = SkillSetProgress::createFromJson('{"tier1": 44.444444}', $settings->reveal());
|
||||
static::assertSame(44.444444, $progress->getTier1());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsTier2()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$progress = SkillSetProgress::createFromJson('{"tier2": 44.444444}', $settings->reveal());
|
||||
static::assertSame(44.444444, $progress->getTier2());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsTier3()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$progress = SkillSetProgress::createFromJson('{"tier3": 44}', $settings->reveal());
|
||||
static::assertSame(44.0, $progress->getTier3());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsTier4()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$progress = SkillSetProgress::createFromJson('{"tier4": 0}', $settings->reveal());
|
||||
static::assertSame(0.0, $progress->getTier4());
|
||||
}
|
||||
|
||||
}
|
|
@ -85,6 +85,21 @@ class SkillSetTest extends TestCase
|
|||
static::assertSame('<p>Example description</p>', $subject->getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function instanceReturnsProgressPercentage()
|
||||
{
|
||||
$settings = $this->prophesize(Settings::class);
|
||||
$subject = SkillSet::createFromJson('{"progressPercentage":{"tier3":44.44444444444444,"tier2":40.74074074074074,"tier1":0,"tier4":23}}', $settings->reveal());
|
||||
$progress = \SkillDisplay\PHPToolKit\Entity\SkillSetProgress::createFromJson('{"tier3":44.44444444444444,"tier2":40.74074074074074,"tier1":0,"tier4":23}', $settings->reveal());
|
||||
static::assertEquals($progress, $subject->getProgressPercentage());
|
||||
static::assertSame(0.0, $subject->getProgressPercentage()->getTier1());
|
||||
static::assertSame(40.74074074074074, $subject->getProgressPercentage()->getTier2());
|
||||
static::assertSame(44.44444444444444, $subject->getProgressPercentage()->getTier3());
|
||||
static::assertSame(23.0, $subject->getProgressPercentage()->getTier4());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
|
Loading…
Reference in a new issue