Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Aug 29, 2015
2 parents a2cc1bb + ae530d9 commit da45327
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 5 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 17 additions & 4 deletions src/Mpociot/Teamwork/Teamwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
{
Expand All @@ -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;
Expand Down
79 changes: 78 additions & 1 deletion tests/TeamworkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,81 @@ public function testHasPendingInviteTrue()
$this->assertTrue( $teamwork->hasPendingInvite( $email, $team_id ) );
}

}
public function testCanInviteToTeam()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$email = "[email protected]";
$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 = "[email protected]";
$team_id = 1;

$app = m::mock('App');
$teamwork = new Teamwork($app);
$app->auth = m::mock('Auth');
$user = m::mock('User');
$user->email = "[email protected]";
$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') );

}

}

0 comments on commit da45327

Please sign in to comment.