Skip to content

Commit

Permalink
lots of work on tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jul 13, 2012
1 parent 2bfd038 commit f6e2170
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Expand Up @@ -245,7 +245,7 @@ public function belongsTo($related, $foreignKey = null)
{
list(, $caller) = debug_backtrace(false);

$foreignKey = "{$caller['function']}_id";
$foreignKey = $this->snakeCase($caller['function']).'_id';
}

// Once we have the foreign key name, we'll just create a new Eloquent query
Expand Down Expand Up @@ -575,6 +575,11 @@ public static function addConnection($name, Connection $connection)
*/
public static function getDefaultConnection()
{
if (is_null(static::$defaultConnection))
{
throw new \RuntimeException("No default connection is registered.");
}

return static::$connections[static::$defaultConnection];
}

Expand All @@ -597,6 +602,8 @@ public static function setDefaultConnectionName($name)
public static function clearConnections()
{
static::$connections = array();

static::$defaultConnection = null;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Expand Up @@ -138,8 +138,8 @@ public function match(array $models, Collection $results, $relation)
}

// Once we have the dictionary constructed, we can loop through all the parents
// and match back onto their children using the keys of the dictionary and
// the primary key of the children to map them onto the right instance.
// and match back onto their children using these keys of the dictionary and
// the primary key of the children to map them onto the correct instances.
foreach ($models as $model)
{
if (isset($dictionary[$model->$foreign]))
Expand All @@ -161,4 +161,14 @@ public function getResults()
return $this->query->first();
}

/**
* Get the foreign key of the relationship.
*
* @return string
*/
public function getForeignKey()
{
return $this->foreignKey;
}

}
14 changes: 12 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Expand Up @@ -379,7 +379,7 @@ public function withPivot($columns)
*
* @return string
*/
protected function getForeignKey()
public function getForeignKey()
{
return $this->table.'.'.$this->foreignKey;
}
Expand All @@ -389,7 +389,7 @@ protected function getForeignKey()
*
* @return string
*/
protected function getOtherKey()
public function getOtherKey()
{
return $this->table.'.'.$this->otherKey;
}
Expand All @@ -408,4 +408,14 @@ protected function getBothKeys()
}, array($this->foreignKey, $this->otherKey));
}

/**
* Get the intermediate table for the relationship.
*
* @return string
*/
public function getTable()
{
return $this->table;
}

}
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Expand Up @@ -87,4 +87,14 @@ protected function buildDictionary(Collection $results)
return $dictionary;
}

/**
* Get the foreign key for the relationship.
*
* @return string
*/
public function getForeignKey()
{
return $this->foreignKey;
}

}
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Expand Up @@ -109,6 +109,16 @@ public function getQuery()
return $this->query;
}

/**
* Get the parent model of the relation.
*
* @return Illuminate\Database\Eloquent\Model
*/
public function getParent()
{
return $this->parent;
}

/**
* Handle dynamic method calls to the relationship.
*
Expand Down
91 changes: 90 additions & 1 deletion tests/EloquentModelTest.php
Expand Up @@ -7,6 +7,7 @@ class EloquentModelTest extends PHPUnit_Framework_TestCase {
public function tearDown()
{
m::close();
EloquentModelStub::clearConnections();
}


Expand Down Expand Up @@ -176,6 +177,86 @@ public function testFillable()
$this->assertEquals('bar', $model->age);
}


public function testHasOneCreatesProperRelation()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->hasOne('EloquentModelSaveStub');
$this->assertEquals('eloquent_model_stub_id', $relation->getForeignKey());
EloquentModelStub::clearConnections();

$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->hasOne('EloquentModelSaveStub', 'foo');
$this->assertEquals('foo', $relation->getForeignKey());
$this->assertTrue($relation->getParent() === $model);
$this->assertTrue($relation->getQuery()->getModel() instanceof EloquentModelSaveStub);
}


public function testHasManyCreatesProperRelation()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->hasMany('EloquentModelSaveStub');
$this->assertEquals('eloquent_model_stub_id', $relation->getForeignKey());
EloquentModelStub::clearConnections();

$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->hasMany('EloquentModelSaveStub', 'foo');
$this->assertEquals('foo', $relation->getForeignKey());
$this->assertTrue($relation->getParent() === $model);
$this->assertTrue($relation->getQuery()->getModel() instanceof EloquentModelSaveStub);
}


public function testBelongsToCreatesProperRelation()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToStub();
$this->assertEquals('belongs_to_stub_id', $relation->getForeignKey());
$this->assertTrue($relation->getParent() === $model);
$this->assertTrue($relation->getQuery()->getModel() instanceof EloquentModelSaveStub);
EloquentModelStub::clearConnections();

$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToExplicitKeyStub();
$this->assertEquals('foo', $relation->getForeignKey());
}


public function testBelongsToManyCreatesProperRelation()
{
$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToMany('EloquentModelSaveStub');
$this->assertEquals('eloquent_model_save_stub_eloquent_model_stub.eloquent_model_stub_id', $relation->getForeignKey());
$this->assertEquals('eloquent_model_save_stub_eloquent_model_stub.eloquent_model_save_stub_id', $relation->getOtherKey());
$this->assertTrue($relation->getParent() === $model);
$this->assertTrue($relation->getQuery()->getModel() instanceof EloquentModelSaveStub);
EloquentModelStub::clearConnections();

$model = new EloquentModelStub;
$this->addMockConnection($model);
$relation = $model->belongsToMany('EloquentModelSaveStub', 'table', 'foreign', 'other');
$this->assertEquals('table.foreign', $relation->getForeignKey());
$this->assertEquals('table.other', $relation->getOtherKey());
$this->assertTrue($relation->getParent() === $model);
$this->assertTrue($relation->getQuery()->getModel() instanceof EloquentModelSaveStub);
}


protected function addMockConnection($model)
{
$model->addConnection('main', m::mock('Illuminate\Database\Connection'));
$model->getConnection()->shouldReceive('getQueryGrammar')->andReturn(m::mock('Illuminate\Database\Query\Grammars\Grammar'));
$model->getConnection()->shouldReceive('getPostProcessor')->andReturn(m::mock('Illuminate\Database\Query\Processors\Processor'));
}

}

class EloquentModelStub extends Illuminate\Database\Eloquent\Model {
Expand All @@ -188,10 +269,18 @@ public function setListItems($value)
{
return json_encode($value);
}
public function belongsToStub()
{
return $this->belongsTo('EloquentModelSaveStub');
}
public function belongsToExplicitKeyStub()
{
return $this->belongsTo('EloquentModelSaveStub', 'foo');
}
}

class EloquentModelSaveStub extends Illuminate\Database\Eloquent\Model {
protected $table = 'stub';
protected $table = 'save_stub';
public function save() { $_SERVER['__eloquent.saved'] = true; }
}

Expand Down

0 comments on commit f6e2170

Please sign in to comment.