Merge pull request #2 from DanielSiepmann/feature/improve-api

Improve API
This commit is contained in:
Markus Klein 2020-10-08 16:33:03 +02:00 committed by GitHub
commit 4d401081e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 1227 additions and 40 deletions

View file

@ -14,6 +14,8 @@ jobs:
strategy: strategy:
matrix: matrix:
php-version: php-version:
- 7.2
- 7.3
- 7.4 - 7.4
steps: steps:
- name: Checkout - name: Checkout
@ -27,6 +29,29 @@ jobs:
- name: PHP lint - name: PHP lint
run: "find *.php src tests -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l" run: "find *.php src tests -name '*.php' -print0 | xargs -0 -n 1 -P 4 php -l"
check-cgl:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
- name: Check CGL
run: ./vendor/bin/phpcs
tests: tests:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View file

@ -10,9 +10,9 @@
} }
], ],
"require": { "require": {
"guzzlehttp/guzzle": "^6.5",
"ext-json": "*", "ext-json": "*",
"php": "^7.4" "guzzlehttp/guzzle": "^6.5",
"php": "7.2.* || 7.3.* || 7.4.*"
}, },
"autoload": { "autoload": {
"files": [ "src/Constants/VerificationTypes.php" ], "files": [ "src/Constants/VerificationTypes.php" ],
@ -27,6 +27,7 @@
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^9.3", "phpunit/phpunit": "^9.3",
"phpspec/prophecy-phpunit": "^2.0" "phpspec/prophecy-phpunit": "^2.0",
"squizlabs/php_codesniffer": "^3.5"
} }
} }

55
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "5ddae281cdc978a7b2312629a0fec326", "content-hash": "20a8d4087070dc229b18c15160e7bf79",
"packages": [ "packages": [
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
@ -2481,6 +2481,57 @@
], ],
"time": "2020-06-26T12:18:43+00:00" "time": "2020-06-26T12:18:43+00:00"
}, },
{
"name": "squizlabs/php_codesniffer",
"version": "3.5.6",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "e97627871a7eab2f70e59166072a6b767d5834e0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/e97627871a7eab2f70e59166072a6b767d5834e0",
"reference": "e97627871a7eab2f70e59166072a6b767d5834e0",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcs",
"bin/phpcbf"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "lead"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "https://github.com/squizlabs/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2020-08-10T04:50:15+00:00"
},
{ {
"name": "symfony/polyfill-ctype", "name": "symfony/polyfill-ctype",
"version": "v1.18.1", "version": "v1.18.1",
@ -2660,7 +2711,7 @@
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"ext-json": "*", "ext-json": "*",
"php": "^7.4" "php": "7.2.* || 7.3.* || 7.4.*"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "1.1.0" "plugin-api-version": "1.1.0"

14
phpcs.xml.dist Normal file
View file

@ -0,0 +1,14 @@
<?xml version="1.0"?>
<ruleset name="project">
<description>This project coding standard</description>
<file>src/</file>
<file>tests/</file>
<arg name="encoding" value="utf-8" />
<arg name="extensions" value="php" />
<rule ref="PSR12">
<exclude name="Generic.Files.LineLength.TooLong" />
</rule>
</ruleset>

81
src/Api/Skill.php Normal file
View file

@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Api;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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\Skill as Entity;
class Skill
{
/**
* @var Settings
*/
protected $settings;
/**
* @var Client
*/
protected $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 Skill has to be a positive integer.', 1600764849);
}
$url = $this->settings->getAPIUrl() . '/api/v1/skill/' . $id;
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 Skill with id "' . $id . '" not available.', 1601881616);
}
throw $e;
}
if ($result->getStatusCode() !== 200) {
throw new \Exception('Did not get proper response for skill.', 1600694312);
}
return Entity::createFromJson((string) $result->getBody());
}
}

86
src/Api/SkillSet.php Normal file
View file

@ -0,0 +1,86 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Api;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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 SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\SkillSet as Entity;
class SkillSet
{
/**
* @var Settings
*/
protected $settings;
/**
* @var Client
*/
protected $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.', 1600764811);
}
$url = $this->settings->getAPIUrl() . '/api/v1/skillset/' . $id;
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.', 1601881616);
}
throw $e;
}
if ($result->getStatusCode() !== 200) {
throw new \Exception('Did not get proper response for SkillSet.', 1600694312);
}
$body = (string) $result->getBody();
if (strpos($body, 'Oops, an error occurred') !== false) {
throw new \Exception('Did not get proper response for SkillSet. SkillSet with id "' . $id . '" does probably not exist.', 1600694312);
}
return Entity::createFromJson($body);
}
}

View file

@ -1,15 +1,35 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Configuration; namespace SkillDisplay\PHPToolKit\Configuration;
class Settings class Settings
{ {
private string $user_secret; /**
private string $apiKey; * @var string
private int $verifierID; */
private string $APIUrl; private $user_secret = '';
private string $mySkillDisplayUrl;
/**
* @var string
*/
private $apiKey = '';
/**
* @var int
*/
private $verifierID = 0;
/**
* @var string
*/
private $APIUrl = '';
/**
* @var string
*/
private $mySkillDisplayUrl = '';
public function getVerifierID(): int public function getVerifierID(): int
{ {
@ -49,7 +69,7 @@ class Settings
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->verifierID = $verifierID; $this->verifierID = $verifierID;
$this->user_secret = $user_secret; $this->user_secret = $user_secret;
$this->APIUrl = is_null($domain) ? 'https://www.skilldisplay.eu' : 'https://' . $domain; $this->APIUrl = (is_null($domain)) ? 'https://www.skilldisplay.eu' : 'https://' . $domain;
$this->mySkillDisplayUrl = is_null($domain) ? 'https://my.skilldisplay.eu' : 'https://my.' . $domain; $this->mySkillDisplayUrl = (is_null($domain)) ? 'https://my.skilldisplay.eu' : 'https://my.' . $domain;
} }
} }

View file

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
define('VERIFICATION_SELF', 'self'); define('VERIFICATION_SELF', 'self');
define('VERIFICATION_EDUCATIONAL', 'education'); define('VERIFICATION_EDUCATIONAL', 'education');
define('VERIFICATION_BUSINESS', 'business'); define('VERIFICATION_BUSINESS', 'business');

67
src/Entity/Skill.php Normal file
View file

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Entity;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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.
*/
class Skill
{
/**
* @var array
*/
private $data;
private function __construct(array $data)
{
$this->data = $data;
}
public function getId(): int
{
return $this->data['uid'];
}
public function getTitle(): string
{
return $this->data['title'] ?? '';
}
public function getDescription(): string
{
return $this->data['description'] ?? '';
}
public function getGoals(): string
{
return $this->data['goals'] ?? '';
}
public function toArray(): array
{
return $this->data;
}
public static function createFromJson(string $json): Skill
{
return new Skill(json_decode($json, true));
}
}

80
src/Entity/SkillSet.php Normal file
View file

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Entity;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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.
*/
class SkillSet
{
/**
* @var array
*/
private $data;
private function __construct(array $data)
{
$this->data = $data;
}
public function getId(): int
{
return $this->data['uid'];
}
public function getName(): string
{
return $this->data['name'] ?? '';
}
public function getDescription(): string
{
return $this->data['description'] ?? '';
}
/**
* @return Skill[]
*/
public function getSkills(): array
{
static $skills = [];
if ($skills !== []) {
return $skills;
}
foreach ($this->data['skills'] as $skill) {
$skills[] = Skill::createFromJson(json_encode($skill));
}
return $skills;
}
public function toArray(): array
{
return $this->data;
}
public static function createFromJson(string $json): SkillSet
{
return new SkillSet(json_decode($json, true));
}
}

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Example\APIKeyRequired; namespace SkillDisplay\PHPToolKit\Example\APIKeyRequired;

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Example\APIKeyRequired; namespace SkillDisplay\PHPToolKit\Example\APIKeyRequired;

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use SkillDisplay\PHPToolKit\Configuration\Settings; use SkillDisplay\PHPToolKit\Configuration\Settings;

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Example\NoSettingsRequired; namespace SkillDisplay\PHPToolKit\Example\NoSettingsRequired;

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Example\NoSettingsRequired; namespace SkillDisplay\PHPToolKit\Example\NoSettingsRequired;

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Verification; namespace SkillDisplay\PHPToolKit\Verification;
@ -9,8 +10,15 @@ use SkillDisplay\PHPToolKit\Configuration\Settings;
class Issuer class Issuer
{ {
private Settings $settings; /**
private string $apislug = '/api/v1/verification/create'; * @var Settings
*/
private $settings;
/**
* @var string
*/
private $apislug = '/api/v1/verification/create';
/** /**
* @param int $ID ID of the Skill or SkillSet * @param int $ID ID of the Skill or SkillSet
@ -24,8 +32,7 @@ class Issuer
string $useremail, string $useremail,
string $vtype, string $vtype,
bool $isSkillSet = false bool $isSkillSet = false
): array ): array {
{
if ($isSkillSet) { if ($isSkillSet) {
$requestData['SkillSetId'] = $ID; $requestData['SkillSetId'] = $ID;
} else { } else {
@ -72,8 +79,7 @@ class Issuer
string $useremail, string $useremail,
string $vtype, string $vtype,
bool $isSkillSet = false bool $isSkillSet = false
): ResponseInterface ): ResponseInterface {
{
$requestData = $this->generateSignedRequestData($skillID, $useremail, $vtype, $isSkillSet); $requestData = $this->generateSignedRequestData($skillID, $useremail, $vtype, $isSkillSet);
$client = new \GuzzleHttp\Client(); $client = new \GuzzleHttp\Client();

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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\SkillSet;
use PHPUnit\Framework\TestCase;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\SkillSet as Entity;
/**
* @covers SkillDisplay\PHPToolKit\Api\SkillSet
*/
class SkillSetTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanBeCreated()
{
$settings = $this->prophesize(Settings::class);
$client = $this->prophesize(Client::class);
$subject = new SkillSet(
$settings->reveal(),
$client->reveal()
);
static::assertInstanceOf(SkillSet::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'
&& $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('{"uid":10}');
$subject = new SkillSet(
$settings->reveal(),
$client->reveal()
);
$result = $subject->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);
$subject = new SkillSet(
$settings->reveal(),
$client->reveal()
);
$this->expectExceptionMessage('Did not get proper response for SkillSet.');
$this->expectExceptionCode(1600694312);
$result = $subject->getById(10);
}
/**
* @test
* @dataProvider nonePositiveIds
*/
public function throwsExceptionIfSkillSetIdIsNotPositive(int $skillId)
{
$settings = $this->prophesize(Settings::class);
$client = $this->prophesize(Client::class);
$subject = new SkillSet(
$settings->reveal(),
$client->reveal()
);
$this->expectExceptionMessage('ID of SkillSet has to be a positive integer.');
$this->expectExceptionCode(1600764811);
$subject->getById($skillId);
}
public function nonePositiveIds(): array
{
return [
'zero' => [
'skillId' => 0,
],
'negative' => [
'skillId' => -1,
],
];
}
}

View file

@ -0,0 +1,148 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Api;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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\Skill;
use PHPUnit\Framework\TestCase;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Skill as Entity;
/**
* @covers SkillDisplay\PHPToolKit\Api\Skill
*/
class SkillTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanBeCreated()
{
$settings = $this->prophesize(Settings::class);
$client = $this->prophesize(Client::class);
$subject = new Skill(
$settings->reveal(),
$client->reveal()
);
static::assertInstanceOf(Skill::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/skill/10'
&& $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('{"uid":10}');
$subject = new Skill(
$settings->reveal(),
$client->reveal()
);
$result = $subject->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);
$subject = new Skill(
$settings->reveal(),
$client->reveal()
);
$this->expectExceptionMessage('Did not get proper response for skill.');
$this->expectExceptionCode(1600694312);
$result = $subject->getById(10);
}
/**
* @test
* @dataProvider nonePositiveIds
*/
public function throwsExceptionIfSkillIdIsNotPositive(int $skillId)
{
$settings = $this->prophesize(Settings::class);
$client = $this->prophesize(Client::class);
$subject = new Skill(
$settings->reveal(),
$client->reveal()
);
$this->expectExceptionMessage('ID of Skill has to be a positive integer.');
$this->expectExceptionCode(1600764849);
$subject->getById($skillId);
}
public function nonePositiveIds(): array
{
return [
'zero' => [
'skillId' => 0,
],
'negative' => [
'skillId' => -1,
],
];
}
}

View file

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Configuration; namespace SkillDisplay\PHPToolKit\Tests\Unit\Configuration;

View file

@ -0,0 +1,115 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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 SkillDisplay\PHPToolKit\Entity\SkillSet;
/**
* @covers SkillDisplay\PHPToolKit\Entity\SkillSet
*/
class SkillSetTest extends TestCase
{
/**
* @test
*/
public function instanceCanNotBeCreatedViaConstructor()
{
$this->expectErrorMessage('Call to private SkillDisplay\PHPToolKit\Entity\SkillSet::__construct() from context \'SkillDisplay\PHPToolKit\Tests\Unit\Entity\SkillSetTest\'');
new SkillSet();
}
/**
* @test
*/
public function instanceCanBeCreatedFromJson()
{
$subject = SkillSet::createFromJson('{}');
static::assertInstanceOf(SkillSet::class, $subject);
}
/**
* @test
*/
public function instanceReturnsId()
{
$subject = SkillSet::createFromJson('{"uid":90}');
static::assertSame(90, $subject->getId());
}
/**
* @test
*/
public function instanceReturnsName()
{
$subject = SkillSet::createFromJson('{"name":"Example name"}');
static::assertSame('Example name', $subject->getName());
}
/**
* @test
*/
public function instanceReturnsDescription()
{
$subject = SkillSet::createFromJson('{"description":"<p>Example description</p>"}');
static::assertSame('<p>Example description</p>', $subject->getDescription());
}
/**
* @test
*/
public function instanceReturnsSkills()
{
$subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}');
$skills = $subject->getSkills();
static::assertCount(2, $skills);
static::assertSame(90, $skills[0]->getId());
static::assertSame(91, $skills[1]->getId());
}
/**
* @test
*/
public function instanceReturnsSameSkillInstancesA2ndTime()
{
$subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}');
$skillsFirstCall = $subject->getSkills();
$skillsSecondCall = $subject->getSkills();
static::assertSame($skillsFirstCall, $skillsSecondCall);
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$subject = SkillSet::createFromJson('{"uid":90,"name":"Example name"}');
static::assertSame([
'uid' => 90,
'name' => 'Example name',
], $subject->toArray());
}
}

View file

@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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 SkillDisplay\PHPToolKit\Entity\Skill;
/**
* @covers SkillDisplay\PHPToolKit\Entity\Skill
*/
class SkillTest extends TestCase
{
/**
* @test
*/
public function instanceCanNotBeCreatedViaConstructor()
{
$this->expectErrorMessage('Call to private SkillDisplay\PHPToolKit\Entity\Skill::__construct() from context \'SkillDisplay\PHPToolKit\Tests\Unit\Entity\SkillTest\'');
new Skill();
}
/**
* @test
*/
public function instanceCanBeCreatedFromJson()
{
$subject = Skill::createFromJson('{}');
static::assertInstanceOf(Skill::class, $subject);
}
/**
* @test
*/
public function instanceReturnsId()
{
$subject = Skill::createFromJson('{"uid":90}');
static::assertSame(90, $subject->getId());
}
/**
* @test
*/
public function instanceReturnsTitle()
{
$subject = Skill::createFromJson('{"title":"Example title"}');
static::assertSame('Example title', $subject->getTitle());
}
/**
* @test
*/
public function instanceReturnsDescription()
{
$subject = Skill::createFromJson('{"description":"<p>Example description</p>"}');
static::assertSame('<p>Example description</p>', $subject->getDescription());
}
/**
* @test
*/
public function instanceReturnsGoals()
{
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>"}');
static::assertSame('<p>Example goals</p>', $subject->getGoals());
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>","uid":90,"title":"Example title"}');
static::assertSame([
'goals' => '<p>Example goals</p>',
'uid' => 90,
'title' => 'Example title',
], $subject->toArray());
}
}

File diff suppressed because one or more lines are too long