Merge pull request #6 from DanielSiepmann/feature/provide-entity-getter

Add brands
This commit is contained in:
Markus Klein 2020-11-06 10:28:30 +01:00 committed by GitHub
commit 9b3ab12ec5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 414 additions and 24 deletions

View file

@ -76,6 +76,6 @@ class Skill
throw new \Exception('Did not get proper response for skill.', 1600694312);
}
return Entity::createFromJson((string) $result->getBody());
return Entity::createFromJson((string) $result->getBody(), $this->settings);
}
}

View file

@ -81,6 +81,6 @@ class SkillSet
throw new \Exception('Did not get proper response for SkillSet. SkillSet with id "' . $id . '" does probably not exist.', 1600694312);
}
return Entity::createFromJson($body);
return Entity::createFromJson($body, $this->settings);
}
}

86
src/Entity/Brand.php Normal file
View file

@ -0,0 +1,86 @@
<?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.
*/
use SkillDisplay\PHPToolKit\Configuration\Settings;
class Brand
{
/**
* @var array
*/
private $data;
/**
* @var Settings
*/
private $settings;
private function __construct(array $data, Settings $settings)
{
$this->data = $data;
$this->settings = $settings;
}
public function getId(): int
{
return $this->data['uid'];
}
public function getName(): string
{
return $this->data['name'] ?? '';
}
public function getUrl(): string
{
return $this->data['url'] ?? '';
}
public function getLogoPublicUrl(): string
{
$mediaUrl = $this->data['logoPublicUrl'] ?? '';
if ($mediaUrl === '') {
return '';
}
return $this->settings->getMySkillDisplayUrl() . '/' . $mediaUrl;
}
// In order to support frameworks / APIs that expect "getter".
public function getAsArray(): array
{
return $this->toArray();
}
public function toArray(): array
{
return $this->data;
}
public static function createFromJson(string $json, Settings $settings): Brand
{
return new Brand(json_decode($json, true), $settings);
}
}

View file

@ -23,6 +23,8 @@ namespace SkillDisplay\PHPToolKit\Entity;
* 02110-1301, USA.
*/
use SkillDisplay\PHPToolKit\Configuration\Settings;
class Skill
{
/**
@ -30,9 +32,20 @@ class Skill
*/
private $data;
private function __construct(array $data)
/**
* @var Settings
*/
private $settings;
/**
* @var array
*/
private $brands = [];
private function __construct(array $data, Settings $settings)
{
$this->data = $data;
$this->settings = $settings;
}
public function getId(): int
@ -55,13 +68,35 @@ class Skill
return $this->data['goals'] ?? '';
}
/**
* @return Brand[]
*/
public function getBrands(): array
{
if ($this->brands !== []) {
return $this->brands;
}
foreach ($this->data['brands'] as $brand) {
$this->brands[] = Brand::createFromJson(json_encode($brand), $this->settings);
}
return $this->brands;
}
// In order to support frameworks / APIs that expect "getter".
public function getAsArray(): array
{
return $this->toArray();
}
public function toArray(): array
{
return $this->data;
}
public static function createFromJson(string $json): Skill
public static function createFromJson(string $json, Settings $settings): Skill
{
return new Skill(json_decode($json, true));
return new Skill(json_decode($json, true), $settings);
}
}

View file

@ -23,6 +23,8 @@ namespace SkillDisplay\PHPToolKit\Entity;
* 02110-1301, USA.
*/
use SkillDisplay\PHPToolKit\Configuration\Settings;
class SkillSet
{
/**
@ -30,14 +32,25 @@ class SkillSet
*/
private $data;
/**
* @var Settings
*/
private $settings;
/**
* @var array
*/
private $skills = [];
private function __construct(array $data)
/**
* @var Brand|null
*/
private $brand = null;
private function __construct(array $data, Settings $settings)
{
$this->data = $data;
$this->settings = $settings;
}
public function getId(): int
@ -55,6 +68,27 @@ class SkillSet
return $this->data['description'] ?? '';
}
public function getBrand(): Brand
{
if ($this->brand instanceof Brand) {
return $this->brand;
}
$this->brand = Brand::createFromJson(json_encode($this->data['brand']), $this->settings);
return $this->brand;
}
public function getMediaPublicUrl(): string
{
$mediaUrl = $this->data['mediaPublicUrl'] ?? '';
if ($mediaUrl === '') {
return '';
}
return $this->settings->getMySkillDisplayUrl() . '/' . $mediaUrl;
}
/**
* @return Skill[]
*/
@ -65,19 +99,25 @@ class SkillSet
}
foreach ($this->data['skills'] as $skill) {
$this->skills[] = Skill::createFromJson(json_encode($skill));
$this->skills[] = Skill::createFromJson(json_encode($skill), $this->settings);
}
return $this->skills;
}
// In order to support frameworks / APIs that expect "getter".
public function getAsArray(): array
{
return $this->toArray();
}
public function toArray(): array
{
return $this->data;
}
public static function createFromJson(string $json): SkillSet
public static function createFromJson(string $json, Settings $settings): SkillSet
{
return new SkillSet(json_decode($json, true));
return new SkillSet(json_decode($json, true), $settings);
}
}

View file

@ -0,0 +1,135 @@
<?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 Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Brand;
use PHPUnit\Framework\TestCase;
/**
* @covers SkillDisplay\PHPToolKit\Entity\Brand
*/
class BrandTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
public function instanceCanNotBeCreatedViaConstructor()
{
$this->expectErrorMessage('Call to private SkillDisplay\PHPToolKit\Entity\Brand::__construct() from context \'SkillDisplay\PHPToolKit\Tests\Unit\Entity\BrandTest\'');
new Brand();
}
/**
* @test
*/
public function instanceCanBeCreatedFromJson()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{}', $settings->reveal());
static::assertInstanceOf(Brand::class, $subject);
}
/**
* @test
*/
public function instanceReturnsId()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{"uid":90}', $settings->reveal());
static::assertSame(90, $subject->getId());
}
/**
* @test
*/
public function instanceReturnsName()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{"name":"Example name"}', $settings->reveal());
static::assertSame('Example name', $subject->getName());
}
/**
* @test
*/
public function instanceReturnsUrl()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{"url":"https://example.com"}', $settings->reveal());
static::assertSame('https://example.com', $subject->getUrl());
}
/**
* @test
*/
public function instanceReturnsEmptyStringAsLogoPublicUrlPrefixedWithConfiguredMySkillDisplayUrl()
{
$settings = $this->prophesize(Settings::class);
$settings->getMySkillDisplayUrl()->willReturn('https://example.com');
$subject = Brand::createFromJson('{"logoPublicUrl":""}', $settings->reveal());
static::assertSame('', $subject->getLogoPublicUrl());
}
/**
* @test
*/
public function instanceReturnsLogoPublicUrlPrefixedWithConfiguredMySkillDisplayUrl()
{
$settings = $this->prophesize(Settings::class);
$settings->getMySkillDisplayUrl()->willReturn('https://example.com');
$subject = Brand::createFromJson('{"logoPublicUrl":"fileadmin/SkillSets/Images/TYPO3/TCCD_10LTS.jpg"}', $settings->reveal());
static::assertSame('https://example.com/fileadmin/SkillSets/Images/TYPO3/TCCD_10LTS.jpg', $subject->getLogoPublicUrl());
}
/**
* @test
*/
public function canReturnAsArray()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{"uid":90,"name":"Example name"}', $settings->reveal());
static::assertSame([
'uid' => 90,
'name' => 'Example name',
], $subject->getAsArray());
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$settings = $this->prophesize(Settings::class);
$subject = Brand::createFromJson('{"goals":"<p>Example goals</p>","uid":90,"title":"Example title"}', $settings->reveal());
static::assertSame([
'goals' => '<p>Example goals</p>',
'uid' => 90,
'title' => 'Example title',
], $subject->toArray());
}
}

View file

@ -24,6 +24,9 @@ namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
*/
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Brand;
use SkillDisplay\PHPToolKit\Entity\SkillSet;
/**
@ -31,6 +34,8 @@ use SkillDisplay\PHPToolKit\Entity\SkillSet;
*/
class SkillSetTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
@ -45,7 +50,8 @@ class SkillSetTest extends TestCase
*/
public function instanceCanBeCreatedFromJson()
{
$subject = SkillSet::createFromJson('{}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{}', $settings->reveal());
static::assertInstanceOf(SkillSet::class, $subject);
}
@ -54,7 +60,8 @@ class SkillSetTest extends TestCase
*/
public function instanceReturnsId()
{
$subject = SkillSet::createFromJson('{"uid":90}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"uid":90}', $settings->reveal());
static::assertSame(90, $subject->getId());
}
@ -63,7 +70,8 @@ class SkillSetTest extends TestCase
*/
public function instanceReturnsName()
{
$subject = SkillSet::createFromJson('{"name":"Example name"}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"name":"Example name"}', $settings->reveal());
static::assertSame('Example name', $subject->getName());
}
@ -72,16 +80,50 @@ class SkillSetTest extends TestCase
*/
public function instanceReturnsDescription()
{
$subject = SkillSet::createFromJson('{"description":"<p>Example description</p>"}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"description":"<p>Example description</p>"}', $settings->reveal());
static::assertSame('<p>Example description</p>', $subject->getDescription());
}
/**
* @test
*/
public function instanceReturnsBrand()
{
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"brand":{"uid":"10"}}', $settings->reveal());
static::assertInstanceOf(Brand::class, $subject->getBrand());
}
/**
* @test
*/
public function instanceReturnsEmptyStringAsMediaPublicUrlPrefixedWithConfiguredMySkillDisplayUrl()
{
$settings = $this->prophesize(Settings::class);
$settings->getMySkillDisplayUrl()->willReturn('https://example.com');
$subject = SkillSet::createFromJson('{"mediaPublicUrl":""}', $settings->reveal());
static::assertSame('', $subject->getMediaPublicUrl());
}
/**
* @test
*/
public function instanceReturnsMediaPublicUrlPrefixedWithConfiguredMySkillDisplayUrl()
{
$settings = $this->prophesize(Settings::class);
$settings->getMySkillDisplayUrl()->willReturn('https://example.com');
$subject = SkillSet::createFromJson('{"mediaPublicUrl":"fileadmin/SkillSets/Images/TYPO3/TCCD_10LTS.jpg"}', $settings->reveal());
static::assertSame('https://example.com/fileadmin/SkillSets/Images/TYPO3/TCCD_10LTS.jpg', $subject->getMediaPublicUrl());
}
/**
* @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"}]}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}', $settings->reveal());
$skills = $subject->getSkills();
static::assertCount(2, $skills);
@ -94,7 +136,8 @@ class SkillSetTest extends TestCase
*/
public function instanceReturnsSameSkillInstancesA2ndTime()
{
$subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}', $settings->reveal());
$skillsFirstCall = $subject->getSkills();
$skillsSecondCall = $subject->getSkills();
@ -106,19 +149,34 @@ class SkillSetTest extends TestCase
*/
public function twoDifferentInstancesProvideTheirOwnSkills()
{
$subject1 = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}');
$subject2 = SkillSet::createFromJson('{"skills":[{"uid":80,"title":"Example title 10"},{"goals":"<p>Example goals</p>","uid":81,"title":"Example title 11"}]}');
$settings = $this->prophesize(Settings::class);
$subject1 = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"<p>Example goals</p>","uid":91,"title":"Example title 2"}]}', $settings->reveal());
$subject2 = SkillSet::createFromJson('{"skills":[{"uid":80,"title":"Example title 10"},{"goals":"<p>Example goals</p>","uid":81,"title":"Example title 11"}]}', $settings->reveal());
static::assertSame(90, $subject1->getSkills()[0]->getId());
static::assertSame(80, $subject2->getSkills()[0]->getId());
}
/**
* @test
*/
public function canReturnAsArray()
{
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"uid":90,"name":"Example name"}', $settings->reveal());
static::assertSame([
'uid' => 90,
'name' => 'Example name',
], $subject->getAsArray());
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$subject = SkillSet::createFromJson('{"uid":90,"name":"Example name"}');
$settings = $this->prophesize(Settings::class);
$subject = SkillSet::createFromJson('{"uid":90,"name":"Example name"}', $settings->reveal());
static::assertSame([
'uid' => 90,
'name' => 'Example name',

View file

@ -24,6 +24,9 @@ namespace SkillDisplay\PHPToolKit\Tests\Unit\Entity;
*/
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\PHPToolKit\Entity\Brand;
use SkillDisplay\PHPToolKit\Entity\Skill;
/**
@ -31,6 +34,8 @@ use SkillDisplay\PHPToolKit\Entity\Skill;
*/
class SkillTest extends TestCase
{
use ProphecyTrait;
/**
* @test
*/
@ -45,7 +50,8 @@ class SkillTest extends TestCase
*/
public function instanceCanBeCreatedFromJson()
{
$subject = Skill::createFromJson('{}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{}', $settings->reveal());
static::assertInstanceOf(Skill::class, $subject);
}
@ -54,7 +60,8 @@ class SkillTest extends TestCase
*/
public function instanceReturnsId()
{
$subject = Skill::createFromJson('{"uid":90}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"uid":90}', $settings->reveal());
static::assertSame(90, $subject->getId());
}
@ -63,7 +70,8 @@ class SkillTest extends TestCase
*/
public function instanceReturnsTitle()
{
$subject = Skill::createFromJson('{"title":"Example title"}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"title":"Example title"}', $settings->reveal());
static::assertSame('Example title', $subject->getTitle());
}
@ -72,7 +80,8 @@ class SkillTest extends TestCase
*/
public function instanceReturnsDescription()
{
$subject = Skill::createFromJson('{"description":"<p>Example description</p>"}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"description":"<p>Example description</p>"}', $settings->reveal());
static::assertSame('<p>Example description</p>', $subject->getDescription());
}
@ -81,16 +90,43 @@ class SkillTest extends TestCase
*/
public function instanceReturnsGoals()
{
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>"}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>"}', $settings->reveal());
static::assertSame('<p>Example goals</p>', $subject->getGoals());
}
/**
* @test
*/
public function instanceReturnsBrands()
{
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"brands":[{"uid":10}]}', $settings->reveal());
static::assertCount(1, $subject->getBrands());
static::assertInstanceOf(Brand::class, $subject->getBrands()[0]);
}
/**
* @test
*/
public function canReturnAsArray()
{
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>","uid":90,"title":"Example title"}', $settings->reveal());
static::assertSame([
'goals' => '<p>Example goals</p>',
'uid' => 90,
'title' => 'Example title',
], $subject->getAsArray());
}
/**
* @test
*/
public function canBeConvertedToArray()
{
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>","uid":90,"title":"Example title"}');
$settings = $this->prophesize(Settings::class);
$subject = Skill::createFromJson('{"goals":"<p>Example goals</p>","uid":90,"title":"Example title"}', $settings->reveal());
static::assertSame([
'goals' => '<p>Example goals</p>',
'uid' => 90,