Skip to content

Commit

Permalink
CreateSchemaListener not updating inheritance tables (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryuk authored Dec 5, 2022
1 parent 1e2e880 commit 342b4c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
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;
Expand All @@ -49,6 +50,7 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
foreach ($metadata->subClasses as $subClass) {
if ($this->provider->isAuditable($subClass)) {
$audited = true;
$targetEntity = $subClass;

break;
}
Expand All @@ -68,7 +70,7 @@ public function postGenerateSchemaTable(GenerateSchemaTableEventArgs $eventArgs)
&& 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);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Provider/Doctrine/Issues/Issue132Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

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;

/**
Expand All @@ -33,6 +38,21 @@ public function testIssue132(): void
}));
}

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([
Expand Down

0 comments on commit 342b4c5

Please sign in to comment.