Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

Commit

Permalink
Add new convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sagebind committed Nov 16, 2015
1 parent dacdbdf commit 3bb81c8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 11 deletions.
53 changes: 43 additions & 10 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,26 @@ public function getDMById($id)
});
}

/**
* Gets a direct message channel for a given user.
*
* @param User $user The user to get a DM for.
*
* @return \React\Promise\PromiseInterface A promise for a DM object.
*/
public function getDMByUser(User $user)
{
return $this->getDMByUserId($user->getId());
}

/**
* Gets a direct message channel by user's ID.
*
* @param string $id A user ID.
*
* @return \React\Promise\PromiseInterface A promise for a DM object.
*/
public function getDMbyUserId($id)
public function getDMByUserId($id)
{
return $this->apiCall('im.open', [
'user' => $id,
Expand All @@ -261,6 +273,23 @@ public function getDMbyUserId($id)
});
}

/**
* Gets all users in the Slack team.
*
* @return \React\Promise\PromiseInterface A promise for an array of users.
*/
public function getUsers()
{
// get the user list
return $this->apiCall('users.list')->then(function (Payload $response) {
$users = [];
foreach ($response['members'] as $member) {
$users[] = new User($this, $member);
}
return $users;
});
}

/**
* Gets a user by its ID.
*
Expand All @@ -278,19 +307,23 @@ public function getUserById($id)
}

/**
* Gets all users in the Slack team.
* Gets a user by username.
*
* @return \React\Promise\PromiseInterface A promise for an array of users.
* If the user could not be found, the returned promise is rejected with a
* `UserNotFoundException` exception.
*
* @return \React\Promise\PromiseInterface A promise for a user object.
*/
public function getUsers()
public function getUserByName($username)
{
// get the user list
return $this->apiCall('users.list')->then(function (Payload $response) {
$users = [];
foreach ($response['members'] as $member) {
$users[] = new User($this, $member);
return $this->getUsers()->then(function (array $users) use ($username) {
foreach ($users as $user) {
if ($user->getUsername() === $username) {
return $user;
}
}
return $users;

throw new UserNotFoundException("The user \"$username\" does not exist.");
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/UserNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Slack;

class UserNotFoundException extends \RuntimeException implements Exception
{
}
51 changes: 50 additions & 1 deletion tests/ApiClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Slack\ApiClient;
use Slack\Payload;
use Slack\Team;
use Slack\User;

class ApiClientTest extends TestCase
{
Expand Down Expand Up @@ -38,7 +39,55 @@ public function testGetUsers()
$this->assertLastRequestUrl(ApiClient::BASE_URL.'users.list');
$this->assertInternalType('array', $users);
$this->assertCount(1, $users);
$this->assertInstanceOf(\Slack\User::class, $users[0]);
$this->assertInstanceOf(User::class, $users[0]);
});

$this->watchPromise($users);
}

public function testGetUserById()
{
$id = $this->faker->uuid;

$this->mockResponse(200, null, [
'ok' => true,
'user' => [
'id' => $id,
],
]);

$users = $this->client->getUserById($id)->then(function (User $user) use ($id) {
$this->assertEquals($id, $user->getId());
});

$this->watchPromise($users);
}

public function testGetUserByName()
{
$id = $this->faker->uuid;
$username = $this->faker->userName;

$this->mockResponse(200, [], [
'ok' => true,
'members' => [
[
'id' => $this->faker->uuid,
'name' => $this->faker->userName,
],
[
'id' => $id,
'name' => $username,
],
[
'id' => $this->faker->uuid,
'name' => $this->faker->userName,
],
],
]);

$users = $this->client->getUserByName($username)->then(function (User $user) use ($id) {
$this->assertEquals($id, $user->getId());
});

$this->watchPromise($users);
Expand Down

0 comments on commit 3bb81c8

Please sign in to comment.