Преглед на файлове

add missing relation diff type and change difference key for class name

Olivier Massot преди 1 година
родител
ревизия
7b564b54b5
променени са 2 файла, в които са добавени 30 реда и са изтрити 8 реда
  1. 1 0
      src/Service/Doctrine/SchemaValidation/DiffTypeEnum.php
  2. 29 8
      src/Service/Doctrine/SchemaValidation/SchemaValidationService.php

+ 1 - 0
src/Service/Doctrine/SchemaValidation/DiffTypeEnum.php

@@ -7,6 +7,7 @@ enum DiffTypeEnum: string
 {
     case MISSING_ENTITY = 'MISSING_ENTITY';
     case MISSING_PROPERTY = 'MISSING_PROPERTY';
+    case MISSING_RELATION = 'MISSING_RELATION';
     case DIFFERENT_TYPE = 'DIFFERENT_TYPE';
     case DIFFERENT_RELATION_TYPE = 'DIFFERENT_RELATION_TYPE';
     case DIFFERENT_RELATION_CONFIGURATION = 'DIFFERENT_RELATION_CONFIGURATION';

+ 29 - 8
src/Service/Doctrine/SchemaValidation/SchemaValidationService.php

@@ -40,12 +40,12 @@ class SchemaValidationService
      * @throws ServerExceptionInterface
      * @throws TransportExceptionInterface
      */
-    public function validateSchema(): array
+    public function validateSchema(?DiffTypeEnum $filter = null): array
     {
         $schemaV1 = $this->getV1Schema();
         $schemaV2 = $this->getV2Schema();
 
-        $diff = $this->getDiff($schemaV1, $schemaV2);
+        $diff = $this->getDiff($schemaV1, $schemaV2, $filter);
 
         return $diff;
     }
@@ -62,20 +62,27 @@ class SchemaValidationService
         $schema = [];
 
         foreach ($metadata as $entityMetadata) {
-            $schema[$entityMetadata->getTableName()] = [];
+            $entityClassName = $this->extractClassName($entityMetadata->getName());
+
+            $schema[$entityClassName] = [];
 
             foreach ($entityMetadata->getFieldNames() as $field) {
-                $schema[$entityMetadata->getTableName()][$field] = $entityMetadata->getTypeOfField($field);
+                $schema[$entityClassName][$field] = $entityMetadata->getTypeOfField($field);
             }
 
             foreach ($entityMetadata->getAssociationNames() as $association) {
-                $schema[$entityMetadata->getTableName()][$association] = $entityMetadata->getAssociationMapping($association);
+                $schema[$entityClassName][$association] = $entityMetadata->getAssociationMapping($association);
             }
         }
 
         return $schema;
     }
 
+    protected function extractClassName(string $entity): string {
+        $parts = explode('\\', $entity);
+        return array_pop($parts);
+    }
+
     /**
      * Retrieve the V1 schema
      *
@@ -98,7 +105,7 @@ class SchemaValidationService
      * @param array<string, array<string| array<string|int>>> $schemaV2
      * @return array<string, Difference | array<Difference>>
      */
-    protected function getDiff(array $schemaV1, array $schemaV2): array {
+    protected function getDiff(array $schemaV1, array $schemaV2, ?DiffTypeEnum $filter = null): array {
         $diff = [
         ];
 
@@ -106,7 +113,9 @@ 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");
+                if (!$filter || $filter === DiffTypeEnum::MISSING_ENTITY) {
+                    $diff[$entity] = new Difference(DiffTypeEnum::MISSING_ENTITY, "Entity `$entity` is missing in V2");
+                }
                 continue;
             }
 
@@ -116,7 +125,11 @@ 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");
+                    if ($this->isRelationField($schemaV1, $entity, $field)) {
+                        $diff[$entity][$field] = new Difference(DiffTypeEnum::MISSING_RELATION, "Relation " . $this->getRelationTypeLabel($fieldTypeV1) . " `$field` is missing in V2");
+                    } else {
+                        $diff[$entity][$field] = new Difference(DiffTypeEnum::MISSING_PROPERTY, "Property `$field` is missing in V2");
+                    }
                     continue;
                 }
 
@@ -140,6 +153,14 @@ class SchemaValidationService
                     }
                 }
             }
+
+            // Si filter est non null, on ne conserve que les différences du type donné
+            if ($filter !== null) {
+                $diff[$entity] = array_filter(
+                    $diff[$entity],
+                    function (Difference $difference) use ($filter) { return $difference->getType() === $filter; }
+                );
+            }
         }
 
         return $diff;