diff --git a/src/Models/ClassInfo.php b/src/Models/ClassInfo.php index 1e8a2fb22ea19bae3bbc604d59b89a804ad4cf69..669d828734d49045430ef68483c195976fcf6438 100644 --- a/src/Models/ClassInfo.php +++ b/src/Models/ClassInfo.php @@ -94,16 +94,6 @@ final class ClassInfo implements ClassName return $this->class->getFullName(); } - /** - * Get class. - * - * @return ClassName - */ - public function getClass(): ClassName - { - return $this->class; - } - /** * Get properties. * @@ -141,7 +131,7 @@ final class ClassInfo implements ClassName */ public function getParent(): ?ClassInfo { - return $this->parent; + return $this->parent ?: null; } /** @@ -154,6 +144,9 @@ final class ClassInfo implements ClassName if ($parent === $this->parent) { return; } + if ($parent === $this) { + throw new DomainException('A class cannot be its own parent'); + } if ($this->parent !== false) { throw new DomainException('Can only set parent once'); } @@ -235,13 +228,13 @@ final class ClassInfo implements ClassName public function jsonSerialize() { return [ - 'class' => $this->class, + 'class' => $this->class->getFullName(), 'type' => $this->type, 'abstract' => $this->abstract, - 'parent' => $this->parent ? $this->parent->class : null, + 'parent' => $this->parent ? $this->parent->getFullName() : null, 'children' => array_map( function (ClassInfo $ci) { - return $ci->class; + return $ci->getFullName(); }, $this->children ), @@ -281,7 +274,7 @@ final class ClassInfo implements ClassName */ public function __toString() { - return $this->getClass()->getFullName(); + return $this->getFullName(); } /** diff --git a/tests/Models/ClassInfoTest.php b/tests/Models/ClassInfoTest.php index ca239a206fd048d6166c288c8c89b9c8f2a84fb5..5b747abec427957ce417bc5076e53758f47b7e9d 100644 --- a/tests/Models/ClassInfoTest.php +++ b/tests/Models/ClassInfoTest.php @@ -42,6 +42,28 @@ final class ClassInfoTest extends TestCase self::assertArrayKeys(['id', 'name'], $ci->getVirtualProperties()); } + /** + * @expectedException \Irstea\NgModelGeneratorBundle\Exceptions\DomainException + */ + public function testCannotBeItsOwnParent(): void + { + $ci = $this->create('\\namespace\\bla', ['id', 'name'], false); + $ci->setParent($ci); + } + + /** + * @expectedException \Irstea\NgModelGeneratorBundle\Exceptions\DomainException + */ + public function testSelfCannotBeReparented(): void + { + $ci = $this->create('\\namespace\\bla', ['id', 'name'], false); + $parent = $this->create('\\namespace\\bar'); + $otherParent = $this->create('\\namespace\\quz'); + + $ci->setParent($parent); + $ci->setParent($otherParent); + } + public function testRearrangeSingleClass(): void { $ci = $this->create('\\namespace\\bla', ['id', 'name'], false);