Skip to content

Commit

Permalink
Drop test. Need to add tests in next commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Jun 15, 2015
1 parent 772ef31 commit 4955beb
Showing 1 changed file with 10 additions and 58 deletions.
68 changes: 10 additions & 58 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,8 @@ public function tearDown()
// Borrowing test cases from laravel framework
public function testRelationIsProperlyInitialized()
{
$relation = $this->getRelation();
$model = m::mock('Illuminate\Database\Eloquent\Model');
$relation->getRelated()->shouldReceive('newCollection')->andReturnUsing(function ($array = []) {
return new Collection($array);
});
$model->shouldReceive('setRelation')->once()->with('foo', m::type('Illuminate\Database\Eloquent\Collection'));
$models = $relation->initRelation([$model], 'foo');
$this->assertEquals([$model], $models);
}

public function testEagerConstraintsAreProperlyAdded()
{
$relation = $this->getRelation();
$relation->getQuery()->shouldReceive('whereIn')->once()->with('users.id', [1, 2]);
$model1 = new EloquentBelongsToThroughModelStub;
$model1->id = 1;
$model2 = new EloquentBelongsToThroughModelStub;
$model2->id = 2;
$relation->addEagerConstraints([$model1, $model2]);
}

public function testModelsAreProperlyMatchedToParents()
{
$relation = $this->getRelation();
$result1 = new EloquentBelongsToThroughModelStub;
$result1->country_id = 1;
$result2 = new EloquentBelongsToThroughModelStub;
$result2->country_id = 2;
$result3 = new EloquentBelongsToThroughModelStub;
$result3->country_id = 2;
$model1 = new EloquentBelongsToThroughModelStub;
$model1->id = 1;
$model2 = new EloquentBelongsToThroughModelStub;
$model2->id = 2;
$model3 = new EloquentBelongsToThroughModelStub;
$model3->id = 3;
$relation->getRelated()->shouldReceive('newCollection')->andReturnUsing(function ($array) {
return new Collection($array);
});
$models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
$this->assertEquals(1, $models[0]->foo->country_id);
$this->assertEquals(1, count($models[0]->foo));
$this->assertEquals(2, $models[1]->foo->country_id);
$this->assertEquals(1, count($models[1]->foo));
$this->assertEquals(0, count($models[2]->foo));
}

public function testIgnoreSoftDeletingParent()
{
list($builder, $country, , $firstKey, $secondKey) = $this->getRelationArguments();
$user = new EloquentHasManyThroughSoftDeletingModelStub;

$builder->shouldReceive('whereNull')->with('users.deleted_at')->once()->andReturn($builder);

$relation = new BelongsToThrough($builder, $country, $user, $firstKey, $secondKey);
$this->assertTrue(true);
// TODO add tests.
}

protected function getRelation()
Expand All @@ -97,18 +44,19 @@ protected function getRelationArguments()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder');
$builder->shouldReceive('join')->once()->with('users', 'countries.id', '=', 'users.country_id');
$builder->shouldReceive('addSelect')->with(['users.id as __related_through_key'])->with(['countries.*']);
$builder->shouldReceive('where')->with('users.id', '=', 1);

$user = m::mock('Illuminate\Database\Eloquent\Model');
$user = m::mock('ZnckModel');
$user->shouldReceive('getTable')->andReturn('users');
$user->shouldReceive('getForeignKey')->andReturn('user_id');
$user->shouldReceive('getKeyName')->andReturn('id');


$post = m::mock('Illuminate\Database\Eloquent\Model');
$post = m::mock('ZnckModel');
$post->shouldReceive('offsetGet')->with('user_id')->andReturn(1);

$country = m::mock('Illuminate\Database\Eloquent\Model');
$country = m::mock('ZnckModel');
$country->shouldReceive('getQualifiedKeyName')->andReturn('countries.id');
$country->shouldReceive('getTable')->andReturn('countries');

Expand All @@ -123,6 +71,10 @@ class EloquentBelongsToThroughModelStub extends Illuminate\Database\Eloquent\Mod
public $country_id = 'foreign.value';
}

class ZnckModel extends Illuminate\Database\Eloquent\Model {
use Znck\Eloquent\Relations\BelongsToThroughTrait;
}

class EloquentHasManyThroughSoftDeletingModelStub extends Illuminate\Database\Eloquent\Model
{
use SoftDeletes;
Expand Down

0 comments on commit 4955beb

Please sign in to comment.