diff --git a/lib/Abstracts/Table.php b/lib/Abstracts/Table.php index 0d2131e..608df70 100644 --- a/lib/Abstracts/Table.php +++ b/lib/Abstracts/Table.php @@ -42,11 +42,22 @@ public function __construct( */ public function getName(): string { - return Str::append($this->globalPrefixProvider->getGlobalDatabasePrefix(), '_') - . Str::append($this->localPrefixProvider->getLocalDatabasePrefix(), '_') + return $this->formatPrefix($this->globalPrefixProvider->getGlobalDatabasePrefix()) + . $this->formatPrefix($this->localPrefixProvider->getLocalDatabasePrefix()) . $this->getUnprefixedName(); } + /** + * Adds a separator to non-empty table prefixes. + * + * @param string $prefix + * @return string + */ + private function formatPrefix(string $prefix): string + { + return $prefix === '' ? '' : Str::append($prefix, '_'); + } + /** @inheritdoc */ abstract public function getUnprefixedName(): string; diff --git a/tests/Unit/Abstracts/TableTest.php b/tests/Unit/Abstracts/TableTest.php new file mode 100644 index 0000000..d9f4cd8 --- /dev/null +++ b/tests/Unit/Abstracts/TableTest.php @@ -0,0 +1,81 @@ +assertSame('glob_loc_name', $this->makeTable('glob', 'loc')->getName()); + } + + public function testGetNameOmitsAnEmptyGlobalPrefix(): void + { + $this->assertSame('loc_name', $this->makeTable('', 'loc')->getName()); + } + + public function testGetNameOmitsAnEmptyLocalPrefix(): void + { + $this->assertSame('glob_name', $this->makeTable('glob', '')->getName()); + } + + public function testGetNameOmitsBothEmptyPrefixes(): void + { + $this->assertSame('name', $this->makeTable('', '')->getName()); + } + + private function makeTable(string $globalPrefix, string $localPrefix): Table + { + $globalPrefixProvider = $this->createMock(HasGlobalDatabasePrefix::class); + $globalPrefixProvider->method('getGlobalDatabasePrefix')->willReturn($globalPrefix); + + $localPrefixProvider = $this->createMock(HasLocalDatabasePrefix::class); + $localPrefixProvider->method('getLocalDatabasePrefix')->willReturn($localPrefix); + + return new class( + $localPrefixProvider, + $globalPrefixProvider, + $this->createMock(HasCharsetProvider::class), + $this->createMock(HasCollateProvider::class), + $this->createMock(TableSchemaService::class) + ) extends Table { + public function getUnprefixedName(): string + { + return 'name'; + } + + public function getAlias(): string + { + return 'name'; + } + + public function getTableVersion(): string + { + return '1'; + } + + public function getColumns(): array + { + return []; + } + + public function getIndices(): array + { + return []; + } + + public function getSingularUnprefixedName(): string + { + return 'name'; + } + }; + } +}