From 59f1ffab4ae307c850f55c06bc59afa8db84efd4 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Mon, 19 Oct 2020 15:35:58 +0200 Subject: [PATCH] Prevent skill sets from sharing their skills Each SkillSet should provide his own skills and should not share the skills with other skill sets. Therefore use an instance property instead of static. --- src/Entity/SkillSet.php | 15 +++++++++------ tests/Unit/Entity/SkillSetTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Entity/SkillSet.php b/src/Entity/SkillSet.php index 18a261b..09ea395 100644 --- a/src/Entity/SkillSet.php +++ b/src/Entity/SkillSet.php @@ -30,6 +30,11 @@ class SkillSet */ private $data; + /** + * @var array + */ + private $skills = []; + private function __construct(array $data) { $this->data = $data; @@ -55,17 +60,15 @@ class SkillSet */ public function getSkills(): array { - static $skills = []; - - if ($skills !== []) { - return $skills; + if ($this->skills !== []) { + return $this->skills; } foreach ($this->data['skills'] as $skill) { - $skills[] = Skill::createFromJson(json_encode($skill)); + $this->skills[] = Skill::createFromJson(json_encode($skill)); } - return $skills; + return $this->skills; } public function toArray(): array diff --git a/tests/Unit/Entity/SkillSetTest.php b/tests/Unit/Entity/SkillSetTest.php index 8641d50..2b51226 100644 --- a/tests/Unit/Entity/SkillSetTest.php +++ b/tests/Unit/Entity/SkillSetTest.php @@ -101,6 +101,18 @@ class SkillSetTest extends TestCase static::assertSame($skillsFirstCall, $skillsSecondCall); } + /** + * @test + */ + 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"}]}'); + + static::assertSame(90, $subject1->getSkills()[0]->getId()); + static::assertSame(80, $subject2->getSkills()[0]->getId()); + } + /** * @test */