Repository: DamienHarper/auditor (php)

Issue:
exception while creating audit table schema when using single table inheritance
| Q                    | A
| ---------------------| -----------------------
| `auditor` version    | 2.3.0
| PHP version          | 8.1.8
| Database             | MySQL / SQLite

#### Summary

I have an abstract parent entity which is not audited, but uses doctrine/orm inheritance type "SINGLE_TABLE" and defines the database table for its child entities. Some child entities are configured to be audited.

With auditor 2.3.0, creating the audit database schema throws an exception.

#### Current behavior

When the database schema is created for this abstract parent entity, the CreateSchemaListener wants to create the corresponding audit table. This throws an exception while trying to determine the table name from the configuration with the new logic introduced in auditor 2.3.0, as the parent entity does not have any configuration: `Undefined array key "App\AbstractParentEntity"`

https://github.com/DamienHarper/auditor/blob/19ff16a658e7bf268dbea7b88c08118da5caf1ae/src/Provider/Doctrine/Persistence/Schema/SchemaManager.php#L173

In versions before 2.3.0, the table name from the event was being used, which is independent of the configuration.

#### How to reproduce

Define an abstract parent entity and an audited child entity:

```
namespace App;

/**
 * @ORM\Table(name="entities")
 * @ORM\Entity()
 * @ORM\InheritanceType("SINGLE_TABLE")
 */
abstract class AbstractParentEntity {}

/**
 * @ORM\Entity
 */
class Entity {}
```

Configure auditing for the child entity:

```
dh_auditor:
    enabled: true
    providers:
        doctrine:
            table_prefix: ~
            table_suffix: '_audit'
            entities:
                App\Entity: ~
```

Create the database schema using `Doctrine\ORM\Tools\SchemaTool::createSchema` for the parent entity to create an `Doctrine\ORM\Tools\ToolEvents::postGenerateSchemaTable` event.

#### Expected behavior

Creating the database schema for the abstract parent entity and its child entities does not throw an exception and creates the corresponding audit table schema.


Pull request description:
CreateSchemaListener not updating inheritance tables
Sorry, but locally I found another bug with CreateSchemaListener.php and issue #132 
Fixes #132 

Patch:
diff --git a/src/Provider/Doctrine/Persistence/Event/CreateSchemaListener.php b/src/Provider/Doctrine/Persistence/Event/CreateSchemaListener.php
index 1345133..5dbbd1e 100644
--- a/src/Provider/Doctrine/Persistence/Event/CreateSchemaListener.php
+++ b/src/Provider/Doctrine/Persistence/Event/CreateSchemaListener.php
@@ -39,6 +39,7 @@ class CreateSchemaListener implements EventSubscriber
             throw new Exception(sprintf('Inheritance type "%s" is not yet supported', $metadata->inheritanceType));
         }
 
+        $targetEntity = $metadata->name;
         // check if entity or its children are audited
         if (!$this->provider->isAuditable($metadata->name)) {
             $audited = false;
@@ -49,6 +50,7 @@ class CreateSchemaListener implements EventSubscriber
                 foreach ($metadata->subClasses as $subClass) {
                     if ($this->provider->isAuditable($subClass)) {
                         $audited = true;
+                        $targetEntity = $subClass;
 
                         break;
                     }
@@ -68,7 +70,7 @@ class CreateSchemaListener implements EventSubscriber
             && array_values($auditingServices)[0]->getEntityManager() === array_values($storageServices)[0]->getEntityManager();
 
         $updater = new SchemaManager($this->provider);
-        $updater->createAuditTable($metadata->name, $isSameEntityManager ? $eventArgs->getSchema() : null);
+        $updater->createAuditTable($targetEntity, $isSameEntityManager ? $eventArgs->getSchema() : null);
     }
 
     /**


Test patch:
diff --git a/tests/Provider/Doctrine/Issues/Issue132Test.php b/tests/Provider/Doctrine/Issues/Issue132Test.php
index 8dfd2cc..fd78782 100644
--- a/tests/Provider/Doctrine/Issues/Issue132Test.php
+++ b/tests/Provider/Doctrine/Issues/Issue132Test.php
@@ -4,9 +4,14 @@ declare(strict_types=1);
 
 namespace DH\Auditor\Tests\Provider\Doctrine\Issues;
 
+use DH\Auditor\Provider\Doctrine\Persistence\Event\CreateSchemaListener;
 use DH\Auditor\Provider\Doctrine\Persistence\Schema\SchemaManager;
+use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue132\AbstractParentEntity;
 use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Issue132\DummyEntity;
 use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\DefaultSchemaSetupTrait;
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\DBAL\Schema\Table;
+use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
 use PHPUnit\Framework\TestCase;
 
 /**
@@ -33,6 +38,21 @@ final class Issue132Test extends TestCase
         }));
     }
 
+    public function testIssue132SchemaListener(): void
+    {
+        $em = array_values($this->provider->getAuditingServices())[0]->getEntityManager();
+        $meta = $em->getClassMetadata(AbstractParentEntity::class);
+        $listener = new CreateSchemaListener($this->provider);
+        $schema = new Schema();
+        $tableName = $meta->getTableName();
+        $args = new GenerateSchemaTableEventArgs($meta, $schema, new Table($tableName));
+        $listener->postGenerateSchemaTable($args);
+
+        $manager = new SchemaManager($this->provider);
+        $auditTableName = $manager->computeAuditTablename($tableName, $this->provider->getConfiguration());
+        self::assertTrue($schema->hasTable($auditTableName));
+    }
+
     private function configureEntities(): void
     {
         $this->provider->getConfiguration()->setEntities([