From 03f16aa5c5bae2ccc345ffae58905839312a64e4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Wed, 26 Sep 2018 14:28:50 +0200
Subject: [PATCH] =?UTF-8?q?Tests=20et=20corrections=20suppl=C3=A9mentaires?=
 =?UTF-8?q?=20pour=20ClassInfo.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/Models/ClassInfo.php       | 23 ++++++++---------------
 tests/Models/ClassInfoTest.php | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/Models/ClassInfo.php b/src/Models/ClassInfo.php
index 1e8a2fb..669d828 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 ca239a2..5b747ab 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);
-- 
GitLab