From ae530d9ec865c2455bf503e64fb1585229ae680d Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Sat, 29 Aug 2015 14:13:28 +0200 Subject: [PATCH] Allow usage of user objects for invites, as mentioned in #13 --- README.md | 12 +++++ src/Mpociot/Teamwork/Teamwork.php | 21 ++++++-- tests/TeamworkTest.php | 79 ++++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ae69f54..360deff 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,18 @@ Teamwork::inviteToTeam( $email, $team, function( $invite ) }); ``` +You can also send invites by providing an object with an `email` property like: + +```php +$user = Auth::user(); + +Teamwork::inviteToTeam( $user , $team, function( $invite ) +{ + // Send email to user / let them know that they got invited +}); +``` + + This method will create a `TeamworkInvite` model and return it in the callable third parameter. This model has these attributes: diff --git a/src/Mpociot/Teamwork/Teamwork.php b/src/Mpociot/Teamwork/Teamwork.php index 399b98b..0c36bfb 100644 --- a/src/Mpociot/Teamwork/Teamwork.php +++ b/src/Mpociot/Teamwork/Teamwork.php @@ -36,13 +36,16 @@ public function user() /** * Invite an email adress to a team. + * Either provide a email address or an object with an email property. + * * If no team is given, the current_team_id will be used instead. * - * @param $email - * @param null|Team $team - * @param callable $success + * @param string|User $user + * @param null|Team $team + * @param callable $success + * @throws \Exception */ - public function inviteToTeam( $email, $team = null, callable $success = null ) + public function inviteToTeam( $user, $team = null, callable $success = null ) { if ( is_null( $team ) ) { @@ -55,6 +58,16 @@ public function inviteToTeam( $email, $team = null, callable $success = null ) $team = $team["id"]; } + if( is_object( $user ) && isset($user->email) ) + { + var_dump($user->email); + $email = $user->email; + } elseif( is_string($user) ) { + $email = $user; + } else { + throw new \Exception('The provided object has no "email" attribute and is not a string.'); + } + $invite = $this->app->make('Mpociot\Teamwork\TeamInvite'); $invite->user_id = $this->user()->getKey(); $invite->team_id = $team; diff --git a/tests/TeamworkTest.php b/tests/TeamworkTest.php index e62ac6a..72635ba 100644 --- a/tests/TeamworkTest.php +++ b/tests/TeamworkTest.php @@ -180,4 +180,81 @@ public function testHasPendingInviteTrue() $this->assertTrue( $teamwork->hasPendingInvite( $email, $team_id ) ); } -} \ No newline at end of file + public function testCanInviteToTeam() + { + /* + |------------------------------------------------------------ + | Set + |------------------------------------------------------------ + */ + $email = "asd@fake.com"; + $team_id = 1; + + $app = m::mock('App'); + $teamwork = new Teamwork($app); + $app->auth = m::mock('Auth'); + $user = m::mock('User'); + $user->shouldReceive('getKey')->once()->andReturn(1); + + $app->auth->shouldReceive('user') + ->andReturn($user) + ->once(); + $teaminvite = m::mock('Mpociot\Teamwork\TeamInvite'); + $app->shouldReceive('make')->with('Mpociot\Teamwork\TeamInvite')->once()->andReturn( $teaminvite ); + + /* + |------------------------------------------------------------ + | Expectation + |------------------------------------------------------------ + */ + $teaminvite->shouldReceive('setAttribute')->andReturnSelf(); + $teaminvite->shouldReceive('save')->once()->andReturnSelf(); + + $callback = m::mock('stdClass'); + $callback->shouldReceive('callback')->once() + ->with( $teaminvite )->andReturn(); + + $teamwork->inviteToTeam( $email, $team_id, array($callback,'callback') ); + + } + + public function testCanInviteToTeamWithObject() + { + /* + |------------------------------------------------------------ + | Set + |------------------------------------------------------------ + */ + $email = "asd@fake.com"; + $team_id = 1; + + $app = m::mock('App'); + $teamwork = new Teamwork($app); + $app->auth = m::mock('Auth'); + $user = m::mock('User'); + $user->email = "test@mail.de"; + $user->shouldReceive('getKey')->once()->andReturn(1); + + $app->auth->shouldReceive('user') + ->andReturn($user) + ->once(); + $teaminvite = m::mock('Mpociot\Teamwork\TeamInvite'); + $app->shouldReceive('make')->with('Mpociot\Teamwork\TeamInvite')->once()->andReturn( $teaminvite ); + + /* + |------------------------------------------------------------ + | Expectation + |------------------------------------------------------------ + */ + $teaminvite->shouldReceive('setAttribute')->andReturnSelf(); + $teaminvite->shouldReceive('save')->once()->andReturnSelf(); + + $callback = m::mock('stdClass'); + $callback->shouldReceive('callback')->once() + ->with( $teaminvite )->andReturn(); + + $teamwork->inviteToTeam( $user, $team_id, array($callback,'callback') ); + + } + +}