diff --git a/src/Traits/TeamworkTeamTrait.php b/src/Traits/TeamworkTeamTrait.php index 0693894..ee68518 100644 --- a/src/Traits/TeamworkTeamTrait.php +++ b/src/Traits/TeamworkTeamTrait.php @@ -49,6 +49,6 @@ public function owner() */ public function hasUser(Model $user) { - return $this->users()->where($user->getKeyName(), '=', $user->getKey())->first() ? true : false; + return $this->users()->where($user->getTable() . '.' . $user->getKeyName(), '=', $user->getKey())->first() ? true : false; } } diff --git a/tests/Feature/TeamworkTeamTraitTest.php b/tests/Feature/TeamworkTeamTraitTest.php index a271f62..5dc73d8 100644 --- a/tests/Feature/TeamworkTeamTraitTest.php +++ b/tests/Feature/TeamworkTeamTraitTest.php @@ -75,18 +75,23 @@ public function testHasUser() { $stub = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUserTeamTraitStub[users,first]'); - $user = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUser[getKey]'); + $user = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUser[getKey, getTable]'); + $user->shouldReceive('getKey') ->once() ->andReturn('key'); + $user->shouldReceive('getTable') + ->once() + ->andReturn('users'); + $stub->shouldReceive('first') ->once() ->andReturn(true); $stub->shouldReceive('where') ->once() - ->with('user_id', '=', 'key') + ->with('users.user_id', '=', 'key') ->andReturnSelf(); $stub->shouldReceive('users') @@ -99,18 +104,23 @@ public function testHasUserReturnsFalse() { $stub = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUserTeamTraitStub[users,first]'); - $user = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUser[getKey]'); + $user = m::mock('\Mpociot\Teamwork\Tests\Feature\TestUser[getKey, getTable]'); + $user->shouldReceive('getKey') ->once() ->andReturn('key'); + $user->shouldReceive('getTable') + ->once() + ->andReturn('users'); + $stub->shouldReceive('first') ->once() ->andReturn(false); $stub->shouldReceive('where') ->once() - ->with('user_id', '=', 'key') + ->with('users.user_id', '=', 'key') ->andReturnSelf(); $stub->shouldReceive('users') @@ -126,6 +136,16 @@ public function getKeyName() { return 'user_id'; } + + public function getTable() + { + return 'users'; + } + + public function getKey() + { + return 'key'; + } } class TestUserTeamTraitStub extends \Illuminate\Database\Eloquent\Model