diff --git a/src/Api/Skill.php b/src/Api/Skill.php index 44ec213..5ae0a50 100644 --- a/src/Api/Skill.php +++ b/src/Api/Skill.php @@ -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); } } diff --git a/src/Api/SkillSet.php b/src/Api/SkillSet.php index 1bb29f1..0425bf5 100644 --- a/src/Api/SkillSet.php +++ b/src/Api/SkillSet.php @@ -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); } } diff --git a/src/Entity/Brand.php b/src/Entity/Brand.php new file mode 100644 index 0000000..1dbbf0d --- /dev/null +++ b/src/Entity/Brand.php @@ -0,0 +1,86 @@ + + * + * 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); + } +} diff --git a/src/Entity/Skill.php b/src/Entity/Skill.php index 864c143..72e22b6 100644 --- a/src/Entity/Skill.php +++ b/src/Entity/Skill.php @@ -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); } } diff --git a/src/Entity/SkillSet.php b/src/Entity/SkillSet.php index 09ea395..7e79c36 100644 --- a/src/Entity/SkillSet.php +++ b/src/Entity/SkillSet.php @@ -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); } } diff --git a/tests/Unit/Entity/BrandTest.php b/tests/Unit/Entity/BrandTest.php new file mode 100644 index 0000000..2ff442b --- /dev/null +++ b/tests/Unit/Entity/BrandTest.php @@ -0,0 +1,135 @@ + + * + * 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":"

Example goals

","uid":90,"title":"Example title"}', $settings->reveal()); + static::assertSame([ + 'goals' => '

Example goals

', + 'uid' => 90, + 'title' => 'Example title', + ], $subject->toArray()); + } +} diff --git a/tests/Unit/Entity/SkillSetTest.php b/tests/Unit/Entity/SkillSetTest.php index 2b51226..9a82035 100644 --- a/tests/Unit/Entity/SkillSetTest.php +++ b/tests/Unit/Entity/SkillSetTest.php @@ -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":"

Example description

"}'); + $settings = $this->prophesize(Settings::class); + $subject = SkillSet::createFromJson('{"description":"

Example description

"}', $settings->reveal()); static::assertSame('

Example description

', $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":"

Example goals

","uid":91,"title":"Example title 2"}]}'); + $settings = $this->prophesize(Settings::class); + $subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"

Example goals

","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":"

Example goals

","uid":91,"title":"Example title 2"}]}'); + $settings = $this->prophesize(Settings::class); + $subject = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"

Example goals

","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":"

Example goals

","uid":91,"title":"Example title 2"}]}'); - $subject2 = SkillSet::createFromJson('{"skills":[{"uid":80,"title":"Example title 10"},{"goals":"

Example goals

","uid":81,"title":"Example title 11"}]}'); + $settings = $this->prophesize(Settings::class); + $subject1 = SkillSet::createFromJson('{"skills":[{"uid":90,"title":"Example title 1"},{"goals":"

Example goals

","uid":91,"title":"Example title 2"}]}', $settings->reveal()); + $subject2 = SkillSet::createFromJson('{"skills":[{"uid":80,"title":"Example title 10"},{"goals":"

Example goals

","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', diff --git a/tests/Unit/Entity/SkillTest.php b/tests/Unit/Entity/SkillTest.php index 24c839f..da30117 100644 --- a/tests/Unit/Entity/SkillTest.php +++ b/tests/Unit/Entity/SkillTest.php @@ -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":"

Example description

"}'); + $settings = $this->prophesize(Settings::class); + $subject = Skill::createFromJson('{"description":"

Example description

"}', $settings->reveal()); static::assertSame('

Example description

', $subject->getDescription()); } @@ -81,16 +90,43 @@ class SkillTest extends TestCase */ public function instanceReturnsGoals() { - $subject = Skill::createFromJson('{"goals":"

Example goals

"}'); + $settings = $this->prophesize(Settings::class); + $subject = Skill::createFromJson('{"goals":"

Example goals

"}', $settings->reveal()); static::assertSame('

Example goals

', $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":"

Example goals

","uid":90,"title":"Example title"}', $settings->reveal()); + static::assertSame([ + 'goals' => '

Example goals

', + 'uid' => 90, + 'title' => 'Example title', + ], $subject->getAsArray()); + } + /** * @test */ public function canBeConvertedToArray() { - $subject = Skill::createFromJson('{"goals":"

Example goals

","uid":90,"title":"Example title"}'); + $settings = $this->prophesize(Settings::class); + $subject = Skill::createFromJson('{"goals":"

Example goals

","uid":90,"title":"Example title"}', $settings->reveal()); static::assertSame([ 'goals' => '

Example goals

', 'uid' => 90,