Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/Abstracts/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/Abstracts/TableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace PHPNomad\Database\Tests\Unit\Abstracts;

use PHPNomad\Database\Abstracts\Table;
use PHPNomad\Database\Interfaces\HasCharsetProvider;
use PHPNomad\Database\Interfaces\HasCollateProvider;
use PHPNomad\Database\Interfaces\HasGlobalDatabasePrefix;
use PHPNomad\Database\Interfaces\HasLocalDatabasePrefix;
use PHPNomad\Database\Services\TableSchemaService;
use PHPNomad\Database\Tests\TestCase;

class TableTest extends TestCase
{
public function testGetNameIncludesBothPrefixes(): void
{
$this->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';
}
};
}
}
Loading