From f6e217006af871014b66ee42902fb404968c035f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 12 Jul 2012 22:05:39 -0500 Subject: [PATCH] lots of work on tests. --- src/Illuminate/Database/Eloquent/Model.php | 9 +- .../Database/Eloquent/Relations/BelongsTo.php | 14 ++- .../Eloquent/Relations/BelongsToMany.php | 14 ++- .../Eloquent/Relations/HasOneOrMany.php | 10 ++ .../Database/Eloquent/Relations/Relation.php | 10 ++ tests/EloquentModelTest.php | 91 ++++++++++++++++++- 6 files changed, 142 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 67643338f..8a8ddce26 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -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 @@ -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]; } @@ -597,6 +602,8 @@ public static function setDefaultConnectionName($name) public static function clearConnections() { static::$connections = array(); + + static::$defaultConnection = null; } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index dc8108f57..8800e7e2c 100644 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -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])) @@ -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; + } + } \ No newline at end of file diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index c757f3dee..f04676104 100644 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -379,7 +379,7 @@ public function withPivot($columns) * * @return string */ - protected function getForeignKey() + public function getForeignKey() { return $this->table.'.'.$this->foreignKey; } @@ -389,7 +389,7 @@ protected function getForeignKey() * * @return string */ - protected function getOtherKey() + public function getOtherKey() { return $this->table.'.'.$this->otherKey; } @@ -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; + } + } \ No newline at end of file diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 13344c407..d4a96e2a5 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -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; + } + } \ No newline at end of file diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 0e51836a3..5cd37a83d 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -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. * diff --git a/tests/EloquentModelTest.php b/tests/EloquentModelTest.php index f3132b9a2..3b163fb47 100644 --- a/tests/EloquentModelTest.php +++ b/tests/EloquentModelTest.php @@ -7,6 +7,7 @@ class EloquentModelTest extends PHPUnit_Framework_TestCase { public function tearDown() { m::close(); + EloquentModelStub::clearConnections(); } @@ -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 { @@ -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; } }