From 95a53e019fddd81f780164964801219436b3213f Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Tue, 16 Apr 2024 13:40:40 +0200 Subject: [PATCH] Fix getting table name --- src/AuditConfiguration.php | 2 +- tests/AuditConfigurationTest.php | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/AuditConfigurationTest.php diff --git a/src/AuditConfiguration.php b/src/AuditConfiguration.php index 9a4f8c68..8b37f1f8 100644 --- a/src/AuditConfiguration.php +++ b/src/AuditConfiguration.php @@ -74,7 +74,7 @@ public function getTableName(ClassMetadataInfo $metadata) { $tableName = $metadata->getTableName(); - if (null !== $metadata->getSchemaName()) { + if (null !== $metadata->getSchemaName() && '' !== $metadata->getSchemaName()) { $tableName = $metadata->getSchemaName().'.'.$tableName; } diff --git a/tests/AuditConfigurationTest.php b/tests/AuditConfigurationTest.php new file mode 100644 index 00000000..f197806e --- /dev/null +++ b/tests/AuditConfigurationTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\EntityAuditBundle\Tests; + +use Doctrine\ORM\Mapping\ClassMetadata; +use PHPUnit\Framework\TestCase; +use SimpleThings\EntityAudit\AuditConfiguration; +use Sonata\EntityAuditBundle\Tests\Fixtures\Core\ArticleAudit; + +final class AuditConfigurationTest extends TestCase +{ + public function testItReturnsCorrectTableNameWhenTableSchemaIsNull(): void + { + $auditConfig = new AuditConfiguration(); + + $metadata = new ClassMetadata(ArticleAudit::class); + $metadata->setPrimaryTable([ + 'name' => 'foo', + 'schema' => null, + ]); + + static::assertSame('foo_audit', $auditConfig->getTableName($metadata)); + } + + public function testItReturnsCorrectTableNameWhenTableSchemaIsEmptyString(): void + { + $auditConfig = new AuditConfiguration(); + + $metadata = new ClassMetadata(ArticleAudit::class); + $metadata->setPrimaryTable([ + 'name' => 'foo', + 'schema' => '', + ]); + + static::assertSame('foo_audit', $auditConfig->getTableName($metadata)); + } + + public function testItReturnsCorrectTableNameWhenTableSchemaIsSet(): void + { + $auditConfig = new AuditConfiguration(); + + $metadata = new ClassMetadata(ArticleAudit::class); + $metadata->setPrimaryTable([ + 'name' => 'foo', + 'schema' => 'xyz', + ]); + + static::assertSame('xyz.foo_audit', $auditConfig->getTableName($metadata)); + } +}