Skip to content

Commit

Permalink
Tests and a found bugfix: laravel sorts alphabetical to deduce many-t…
Browse files Browse the repository at this point in the history
…o-many join tables
  • Loading branch information
Guido Contreras Woda committed Jan 30, 2015
1 parent 4e3748b commit b1a9ed0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Configuration/LaravelNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,14 @@ public function joinColumnName($propertyName)
*/
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{
return $this->classToFieldName($sourceEntity) . '_' . $this->classToFieldName($targetEntity);
$names = [
$this->classToFieldName($sourceEntity),
$this->classToFieldName($targetEntity)
];

sort($names);

return implode('_', $names);
}

/**
Expand Down Expand Up @@ -103,7 +110,7 @@ private function classToFieldName($className)
*
* @return string
*/
function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
{
return $propertyName.'_'.$embeddedColumnName;
}
Expand Down
120 changes: 120 additions & 0 deletions tests/Configuration/LaravelNamingStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php namespace Tests\Configuration;

use Illuminate\Support\Str;
use Mitch\LaravelDoctrine\Configuration\LaravelNamingStrategy;

class LaravelNamingStrategyTest extends \PHPUnit_Framework_TestCase
{
/**
* @type LaravelNamingStrategy
*/
private $laravelNamingStrategy;

public function setUp()
{
// Mocking Str would be overkill
$this->laravelNamingStrategy = new LaravelNamingStrategy(new Str());
}

public function testProperTableName()
{
// Singular namespaced StudlyCase class
$className = 'Acme\\ClassName';

$tableName = $this->laravelNamingStrategy->classToTableName($className);

// Plural, snake_cased table name
$this->assertEquals('class_names', $tableName);
}

public function testProperColumnName()
{
// Columns derive from snakeCased fields
$field = 'createdAt';

$columnName = $this->laravelNamingStrategy->propertyToColumnName($field);

// And columns are just the snake_cased field
$this->assertEquals('created_at', $columnName);
}

public function testProperColumnNameWithClassName()
{
// Columns derive from snakeCased fields
$field = 'createdAt';

// Singular namespaced StudlyCase class
$className = 'Acme\\ClassName';

$columnName = $this->laravelNamingStrategy->propertyToColumnName($field, $className);

// Class name shouldn't affect how the column is called
$this->assertEquals('created_at', $columnName);
}

public function testEmbeddedColumnName()
{
// Laravel doesn't have embeddeds
$embeddedField = 'address';
$field = 'street1';

$columnName = $this->laravelNamingStrategy->embeddedFieldToColumnName($embeddedField, $field);

// So this is just like Doctrine's default naming strategy
$this->assertEquals('address_street1', $columnName);
}

public function testReferenceColumn()
{
// Laravel's convention is just 'id', like the default Doctrine
$columnName = $this->laravelNamingStrategy->referenceColumnName();

$this->assertEquals('id', $columnName);
}

public function testJoinColumnName()
{
// Given a User -> belongsTo -> Group
$field = 'group';

$columnName = $this->laravelNamingStrategy->joinColumnName($field);

// We expect to have a group_id in the users table
$this->assertEquals('group_id', $columnName);
}

public function testBelongsToManyJoinTable()
{
// Laravel doesn't do as Doctrine's default here
$sourceModel = 'Acme\\ClassName';

// We don't care about "source" or "target"
$targetModel = 'Acme\\AnotherClass';

// We should have it sorted by alphabetical order
$tableName = $this->laravelNamingStrategy->joinTableName($sourceModel, $targetModel);
$this->assertEquals('another_class_class_name', $tableName);

// Let's test swapping parameters, just in case...
$tableName = $this->laravelNamingStrategy->joinTableName($targetModel, $sourceModel);
$this->assertEquals('another_class_class_name', $tableName);
}

public function testJoinKeyColumnName()
{
// This case is similar to Doctrine's default as well
$className = 'Acme\\Foo';

// If no reference name is given, we use 'id'
$columnName = $this->laravelNamingStrategy->joinKeyColumnName($className);

// And expect singular_snake_id column
$this->assertEquals('foo_id', $columnName);

// Given a reference name
$columnName = $this->laravelNamingStrategy->joinKeyColumnName($className, 'reference');

// Same thing, but with that reference instead of 'id'
$this->assertEquals('foo_reference', $columnName);
}
}

0 comments on commit b1a9ed0

Please sign in to comment.