diff --git a/src/Mpociot/Teamwork/Traits/UserHasTeams.php b/src/Mpociot/Teamwork/Traits/UserHasTeams.php index 6cdbc74..7ba9205 100644 --- a/src/Mpociot/Teamwork/Traits/UserHasTeams.php +++ b/src/Mpociot/Teamwork/Traits/UserHasTeams.php @@ -144,6 +144,10 @@ public function attachTeam( $team ) $this->current_team_id = $team; $this->save(); } + + // Reload relation + $this->load('teams'); + if( !$this->teams->contains( $team ) ) { $this->teams()->attach( $team ); diff --git a/tests/UserHasTeamsTraitTest.php b/tests/UserHasTeamsTraitTest.php index fac8087..45b3e0c 100644 --- a/tests/UserHasTeamsTraitTest.php +++ b/tests/UserHasTeamsTraitTest.php @@ -156,7 +156,7 @@ public function testAttachTeamSetsCurrentTeamId() { $team = 1; - $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach]' ); + $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach,load]' ); $stub->teams = m::mock('stdClass'); $stub->teams->shouldReceive('contains') ->with( $team ) @@ -167,6 +167,11 @@ public function testAttachTeamSetsCurrentTeamId() $stub->shouldReceive('teams') ->once() ->andReturnSelf(); + + $stub->shouldReceive('load') + ->once() + ->with('teams'); + $stub->shouldReceive('attach') ->once() ->with($team); @@ -181,7 +186,7 @@ public function testAttachTeamDoesNotOverrideCurrentTeamId() { $team = 1; - $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach]' ); + $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach,load]' ); $stub->teams = m::mock('stdClass'); $stub->teams->shouldReceive('contains') ->with( $team ) @@ -192,6 +197,11 @@ public function testAttachTeamDoesNotOverrideCurrentTeamId() $stub->shouldReceive('teams') ->once() ->andReturnSelf(); + + $stub->shouldReceive('load') + ->once() + ->with('teams'); + $stub->shouldReceive('attach') ->once() ->with($team); @@ -206,7 +216,7 @@ public function testAttachTeamDoesNotAttachTeamIdWhenItExists() { $team = 1; - $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach]' ); + $stub = m::mock( 'TestUserHasTeamsTraitStub[teams,save,attach,load]' ); $stub->teams = m::mock('stdClass'); $stub->teams->shouldReceive('contains') ->with( $team ) @@ -217,6 +227,11 @@ public function testAttachTeamDoesNotAttachTeamIdWhenItExists() $stub->shouldReceive('teams') ->never() ->andReturnSelf(); + + $stub->shouldReceive('load') + ->once() + ->with('teams'); + $stub->shouldReceive('attach') ->never();