浏览代码

Schema validation : various minor improvements

Olivier Massot 1 年之前
父节点
当前提交
b3d4562efe

+ 26 - 2
src/Service/Cron/Job/SchemaValidation.php

@@ -31,7 +31,7 @@ class SchemaValidation extends BaseCronJob
      */
     public function preview(): void
     {
-        $this->schemaValidationService->validateSchema();
+        $this->validateAndPrint();
     }
 
     /**
@@ -41,6 +41,30 @@ class SchemaValidation extends BaseCronJob
      */
     public function execute(): void
     {
-        $this->schemaValidationService->validateSchema();
+        $this->validateAndPrint();
+    }
+
+    protected function validateAndPrint() {
+        $diff = $this->schemaValidationService->validateSchema();
+
+        foreach ($diff as $entity => $value) {
+            $this->ui->print($entity);
+
+            if (!is_array($value)) {
+                $this->ui->print($value->getType()->value . " : " . $value->getMessage());
+            } else {
+                foreach ($value as $field => $difference) {
+                    $this->ui->print(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
+                }
+            }
+
+            $this->ui->print("\n");
+        }
+
+        if ($diff) {
+            $this->ui->print(count($diff) . " differences found");
+        } else {
+            $this->ui->print("No difference found");
+        }
     }
 }

+ 20 - 13
src/Service/Doctrine/SchemaValidation/SchemaValidationService.php

@@ -47,17 +47,13 @@ class SchemaValidationService
 
         $diff = $this->getDiff($schemaV1, $schemaV2);
 
-        foreach ($diff['Organization'] as $field => $diffEntry) {
-            var_dump($field . " : " . $diffEntry->value);
-        }
-
         return $diff;
     }
 
     /**
      * Retrieve the V2 schema
      *
-     * @return array<string, array<string | array<string|int>>
+     * @return array<string, array<string | array<string|int>>>
      * @throws MappingException
      */
     protected function getV2Schema(): array
@@ -83,7 +79,7 @@ class SchemaValidationService
     /**
      * Retrieve the V1 schema
      *
-     * @return array<string, array<string | array<string|int>>
+     * @return array<string, array<string | array<string|int>>>
      * @throws ClientExceptionInterface
      * @throws RedirectionExceptionInterface
      * @throws ServerExceptionInterface
@@ -98,7 +94,7 @@ class SchemaValidationService
     /**
      * Get a list of differences between V1 and V2 doctrine schemas
      *
-     * @param array<string, array<string | array<string|int>> $schemaV1
+     * @param array<string, array<string | array<string|int>>> $schemaV1
      * @param array<string, array<string| array<string|int>>> $schemaV2
      * @return array<string, Difference | array<Difference>>
      */
@@ -110,7 +106,7 @@ class SchemaValidationService
 
             if (!$this->isEntityInSchema($schemaV2, $entity)) {
                 // L'entité n'existe pas en V2
-                $diff[$entity] = new Difference(DiffTypeEnum::MISSING_ENTITY, "Entity $entity is missing in V2");
+                $diff[$entity] = new Difference(DiffTypeEnum::MISSING_ENTITY, "Entity `$entity` is missing in V2");
                 continue;
             }
 
@@ -120,7 +116,7 @@ class SchemaValidationService
 
                 if (!$this->isPropertyInSchema($schemaV2, $entity, $field)) {
                     // Le champ n'existe pas en V2
-                    $diff[$entity][$field] = new Difference(DiffTypeEnum::MISSING_PROPERTY, "Property $field is missing in V2");
+                    $diff[$entity][$field] = new Difference(DiffTypeEnum::MISSING_PROPERTY, "Property `$field` is missing in V2");
                     continue;
                 }
 
@@ -131,7 +127,7 @@ class SchemaValidationService
                     // Le champ n'est pas une relation en V1
                     if ($fieldTypeV2 !== $fieldTypeV1) {
                         // Le champ a un type différent en V2
-                        $diff[$entity][$field] = new Difference(DiffTypeEnum::DIFFERENT_TYPE, "Property $field has a different type (V1: $fieldTypeV1, V2: $fieldTypeV2)");
+                        $diff[$entity][$field] = new Difference(DiffTypeEnum::DIFFERENT_TYPE, "Property `$field` has a different type (V1: `$fieldTypeV1`, V2: `$fieldTypeV2`)");
                     }
                 } elseif (!$this->isRelationField($schemaV2, $entity, $field)) {
                     // Le champ est une relation en V1 mais pas en V2
@@ -152,7 +148,7 @@ class SchemaValidationService
     /**
      * Returns true if the given entity name exists in the doctrine schema
      *
-     * @param array<string, array<string | array<string|int>> $schema
+     * @param array<string, array<string | array<string|int>>> $schema
      * @param string $entity
      * @return bool
      */
@@ -163,7 +159,7 @@ class SchemaValidationService
     /**
      * Returns true if the given property name exists in the doctrine schema under this entity.
      *
-     * @param array<string, array<string | array<string|int>> $schema
+     * @param array<string, array<string | array<string|int>>> $schema
      * @param string $entity
      * @param string $property
      * @return bool
@@ -221,7 +217,7 @@ class SchemaValidationService
         }
 
         if (
-            $relationReference['targetEntity'] !== $relationCompared['targetEntity']
+            $this->fullNameToEntityName($relationReference['targetEntity']) !== $this->fullNameToEntityName($relationCompared['targetEntity'])
         ) {
             return new Difference(
                 DiffTypeEnum::DIFFERENT_RELATION_CONFIGURATION,
@@ -240,4 +236,15 @@ class SchemaValidationService
 
         return null;
     }
+
+    /**
+     * Extract an entity base name from a fully qualified name
+     *
+     * @param string $fullName
+     * @return string
+     */
+    protected function fullNameToEntityName(string $fullName): string {
+        $parts = explode('\\', $fullName);
+        return array_pop($parts);
+    }
 }