Skip to content

Commit

Permalink
mapper: fixed striping "id" suffix for foreign keys [closes #73]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 17, 2015
1 parent bf92fd1 commit 2df9bd0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
Expand Up @@ -28,7 +28,10 @@ public function formatEntityKey($key)

public function formatEntityForeignKey($key)
{
return $this->formatEntityKey(substr($key, 0, -2)); // remove Id suffix
if (substr($key, -2) === 'Id'){
$key = substr($key, 0, -2);
}
return $this->formatEntityKey($key);
}

}
Expand Up @@ -30,7 +30,10 @@ public function formatEntityKey($key)

public function formatEntityForeignKey($key)
{
return $this->formatEntityKey(substr($key, 0, -3)); // remove _id suffix
if (substr($key, -3) === '_id') {
$key = substr($key, 0, -3);
}
return $this->formatEntityKey($key);
}

}
57 changes: 57 additions & 0 deletions tests/cases/unit/Mapper/Dbal/StorageReflectionTest.phpt
Expand Up @@ -8,6 +8,7 @@ namespace NextrasTests\Orm\Mapper\Dbal;

use Mockery;
use Nette\Caching\Storages\DevNullStorage;
use Nextras\Orm\Mapper\Dbal\StorageReflection\CamelCaseStorageReflection;
use Nextras\Orm\Mapper\Dbal\StorageReflection\UnderscoredStorageReflection;
use NextrasTests\Orm\TestCase;
use Tester\Assert;
Expand Down Expand Up @@ -43,6 +44,62 @@ class StorageReflectionTest extends TestCase
}, 'Nextras\Orm\InvalidStateException', 'Mismatch count of entity primary key (id) with storage primary key (user_id, group_id).');
}


public function testForeignKeysMappingUnderscored()
{
$platform = Mockery::mock('Nextras\Dbal\Platform\IPlatform');
$platform->shouldReceive('getForeignKeys')->once()->with('table_name')->andReturn([
'user_id' => [],
'group' => [],
]);
$platform->shouldReceive('getColumns')->once()->with('table_name')->andReturn([
'id' => ['is_primary' => TRUE],
'user_id' => ['is_primary' => FALSE],
'group' => ['is_primary' => FALSE],
]);

$connection = Mockery::mock('Nextras\Dbal\Connection');
$connection->shouldReceive('getConfig')->once()->andReturn(['a']);
$connection->shouldReceive('getPlatform')->twice()->andReturn($platform);

$cacheStorage = new DevNullStorage();
$reflection = new UnderscoredStorageReflection($connection, 'table_name', ['id'], $cacheStorage);

Assert::same('user', $reflection->convertStorageToEntityKey('user_id'));
Assert::same('group', $reflection->convertStorageToEntityKey('group'));

Assert::same('user_id', $reflection->convertEntityToStorageKey('user'));
Assert::same('group', $reflection->convertEntityToStorageKey('group'));
}


public function testForeignKeysMappingCamelized()
{
$platform = Mockery::mock('Nextras\Dbal\Platform\IPlatform');
$platform->shouldReceive('getForeignKeys')->once()->with('table_name')->andReturn([
'userId' => [],
'group' => [],
]);
$platform->shouldReceive('getColumns')->once()->with('table_name')->andReturn([
'id' => ['is_primary' => TRUE],
'userId' => ['is_primary' => FALSE],
'group' => ['is_primary' => FALSE],
]);

$connection = Mockery::mock('Nextras\Dbal\Connection');
$connection->shouldReceive('getConfig')->once()->andReturn(['a']);
$connection->shouldReceive('getPlatform')->twice()->andReturn($platform);

$cacheStorage = new DevNullStorage();
$reflection = new CamelCaseStorageReflection($connection, 'table_name', ['id'], $cacheStorage);

Assert::same('user', $reflection->convertStorageToEntityKey('userId'));
Assert::same('group', $reflection->convertStorageToEntityKey('group'));

Assert::same('userId', $reflection->convertEntityToStorageKey('user'));
Assert::same('group', $reflection->convertEntityToStorageKey('group'));
}

}


Expand Down

0 comments on commit 2df9bd0

Please sign in to comment.